For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!
Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class DisabledGlassPane extends JComponent implements KeyListener { private final static Color DEFAULT_BACKGROUND = new Color(128, 128, 128, 128); private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10); private JLabel message = new JLabel(); public DisabledGlassPane() { setOpaque( false ); setBackground( DEFAULT_BACKGROUND ); setLayout( new GridBagLayout() ); add(message, new GridBagConstraints()); message.setOpaque(true); message.setBorder(MESSAGE_BORDER); // Disable Mouse, Key and Focus events for the glass pane addMouseListener( new MouseAdapter() {} ); addMouseMotionListener( new MouseMotionAdapter() {} ); addKeyListener( this ); setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET ); setFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET ); } protected void paintComponent(Graphics g) { g.setColor( getBackground() ); g.fillRect(0, 0, getSize().width, getSize().height); } public void setBackground(Color background) { super.setBackground( background ); Color messageBackground = new Color(background.getRed(), background.getGreen(), background.getBlue()); message.setBackground( messageBackground ); } public void keyPressed(KeyEvent e) { e.consume(); } public void keyTyped(KeyEvent e) { e.consume(); } public void keyReleased(KeyEvent e) { e.consume(); } public void activate(String text) { if (text != null && text.length() > 0) { message.setVisible( true ); message.setText( text ); message.setForeground( getForeground() ); } else message.setVisible( false ); setVisible( true ); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); requestFocusInWindow(); } public void deactivate() { setCursor(null); setVisible( false ); } public static void main(String[] args) { final DisabledGlassPane glassPane = new DisabledGlassPane(); glassPane.setBackground( new Color(255, 128, 128, 128) ); glassPane.setForeground( Color.WHITE ); final JButton button = new JButton( "Click Me" ); button.setMnemonic('c'); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { glassPane.activate("Please Wait..."); Thread thread = new Thread() { public void run() { try { this.sleep(5000); } catch (InterruptedException ie) {} glassPane.deactivate(); } }; thread.start(); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setGlassPane( glassPane ); frame.getContentPane().add(new JLabel("NORTH"), BorderLayout.NORTH ); frame.getContentPane().add( button ); frame.getContentPane().add(new JTextField(), BorderLayout.SOUTH); frame.setSize(300, 300); frame.setLocationRelativeTo( null ); frame.setVisible(true); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DimOverlay { DimmerPanel dimmerPanel; private JPanel getContent() { dimmerPanel = new DimmerPanel(); JPanel content = new JPanel(new BorderLayout()); JLabel label = new JLabel("graphical data", JLabel.CENTER); content.add(label); JPanel panel = new JPanel(); OverlayLayout overlay = new OverlayLayout(panel); panel.setLayout(overlay); panel.add(dimmerPanel); panel.add(content); return panel; } private void reload() { Thread thread = new Thread(new Runnable() { public void run() { int count = 0; boolean workAway = true; while(workAway && count <= 10) { try { Thread.sleep(1000); } catch(InterruptedException e) { workAway = false; } System.out.println("count = " + count++); } dimmerPanel.clearView(); } }); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); dimmerPanel.dimView(); } private JPanel getControl() { JButton reload = new JButton("reload"); reload.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reload(); } }); JPanel panel = new JPanel(); panel.add(reload); return panel; } public static void main(String[] args) { DimOverlay test = new DimOverlay(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.getContentPane().add(test.getControl(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class DimmerPanel extends JPanel { float alpha = 0.0f; public DimmerPanel() { setOpaque(false); } public void dimView() { alpha = 0.25f; repaint(); } public void clearView() { alpha = 0.0f; repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); g2.setComposite(ac); g2.setPaint(Color.gray); g2.fillRect(0, 0, getWidth(), getHeight()); } }