java – Using a GlassPane in a JPanel

java – Using a GlassPane in a JPanel

In my application i have no access to the frame. I just want to deactivate a single panel in the frame.

You can always use:

Window frame = SwingUtilities.windowForComponent(...);

I just want to deactivate a single panel in the frame.

Or you could try using the Disabled Panel. It allows you to simply disable the components on a specific panel.

The second approach which paints a disabled look may be a problem since you need to use the disable panel when you create the GUI.

One possibility — use a JLayeredPane to wrap around your JPanel, and then activate a JPanel layer that is held above it, having it steal the focus and the MouseListener. For example:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;

import javax.swing.*;

public class MyGlassPaneWrapper extends JLayeredPane {
   private JPanel glassPanel = new JPanel();

   public MyGlassPaneWrapper(JComponent myPanel) {
      glassPanel.setOpaque(false);
      glassPanel.setVisible(false);
      glassPanel.addMouseListener(new MouseAdapter() {});
      glassPanel.setFocusable(true);

      myPanel.setSize(myPanel.getPreferredSize());
      add(myPanel, JLayeredPane.DEFAULT_LAYER);
      add(glassPanel, JLayeredPane.PALETTE_LAYER);

      glassPanel.setPreferredSize(myPanel.getPreferredSize());
      glassPanel.setSize(myPanel.getPreferredSize());
      setPreferredSize(myPanel.getPreferredSize());
   }

   public void activateGlassPane(boolean activate) {
      glassPanel.setVisible(activate);
      if (activate) {
         glassPanel.requestFocusInWindow();
         glassPanel.setFocusTraversalKeysEnabled(false);
      } 
   }

   private static void createAndShowGui() {
      final MyPanel myPanel = new MyPanel();
      final MyGlassPaneWrapper myGlassPaneWrapper = new MyGlassPaneWrapper(myPanel);
      final String activateGlassPane = Activate GlassPane;
      final String deactivateGlassPane = Deactivate GlassPane;

      JToggleButton toggleButton = new JToggleButton(activateGlassPane);
      toggleButton.addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent e) {
            AbstractButton source = (AbstractButton) e.getSource();
            if (e.getStateChange() == ItemEvent.SELECTED) {
               source.setText(deactivateGlassPane);
               myGlassPaneWrapper.activateGlassPane(true);
            } else {
               source.setText(activateGlassPane);
               myGlassPaneWrapper.activateGlassPane(false);
            }
         }
      });
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(toggleButton);

      JFrame frame = new JFrame(MyGlassPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(myGlassPaneWrapper);
      frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyPanel extends JPanel {

   public MyPanel() {
      add(new JTextField(5));
      add(new JComboBox<String>(new String[]{Monday, Tuesday, Wednesday}));
      add(new JButton(new AbstractAction(Push Me) {

         @Override
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(MyPanel.this, Button Pushed);
         }
      }));
   }
}

java – Using a GlassPane in a JPanel

Its possible to add a glasspane to a JPanel by wrapping it in the content pane of a JRootPane. You can then use a semi transparent glasspane catching the mouse and keyboard events to deactivate it during data loading, see https://tips4java.wordpress.com/2008/11/07/disabled-glass-pane/

Leave a Reply

Your email address will not be published.