dbausnnd
dbausnnd

Reputation: 59

Using a GlassPane in a JPanel

i want to use a glassPane function to deactivate Components in a Panel during the loading of data. I know the the glasspane is only available in frames. In my application i have no access to the frame. I just want to deactivate a single panel in the frame.

I tried to draw a ractangle over the panel by overriding the paint or paintComponent method of the panel. The problem is that the underlaying components are still available.

could anyone give me an advice how i can solve this problem?

regards

dbausnnd

Upvotes: 2

Views: 11700

Answers (3)

Emmanuel Bourg
Emmanuel Bourg

Reputation: 11048

It's 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/

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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");
         }
      }));
   }
}

Upvotes: 6

camickr
camickr

Reputation: 324147

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.

Upvotes: 4

Related Questions