0

I have problem with glass pane. When I first show my panel everything is OK. But when I want to repaint this panel because components should change (method createPanel() ) then glass panel dissapear. When I resize frame then it is again visible. I have no idea why this happens my panel look like :

package GUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TurnajPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private GridBagConstraints c = new GridBagConstraints();
    private JFrame frame;

    public TurnajPanel(JFrame frame) throws Exception {
        this.frame = frame;
        createPanel();
    }

    public void createPanel() throws Exception {
        removeAll();
        setLayout(new GridBagLayout());

        addMenu();

        revalidate();
        repaint();
    }

    private void addMenu() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 20));
        panel.setBorder(BorderFactory.createTitledBorder("aaaaaaaaaaaaaa"));
        JPanel innerPanelTlacidiel = new JPanel(new GridBagLayout());
        panel.add(innerPanelTlacidiel);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.weighty = 0;
        c.anchor = GridBagConstraints.FIRST_LINE_END;
        c.insets = new Insets(0, 5, 0, 0);
        final JPanel panelTlacidiel = new JPanel(new GridLayout(0, 1, 5, 5));
        innerPanelTlacidiel.add(panelTlacidiel, c);
        frame.setGlassPane(panel);
        frame.getGlassPane().setVisible(true);

    }

    public static void main(String[] args) throws Exception {

        JFrame frame = new JFrame();
        final TurnajPanel turnaj = new TurnajPanel(frame);
        JScrollPane scrolovaciPanel = new JScrollPane(turnaj);
        frame.add(scrolovaciPanel);

        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("????????????????");
                try {
                    turnaj.createPanel();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub

            }
        });
    }
}

when I add one line to method createPanel()

public void createPanel() throws Exception {
    removeAll();
    FrameFactory.turnajFrame.getGlassPane().setVisible(false); // new line
    setLayout(new GridBagLayout());

    addMenu();

    revalidate();
    repaint();
}

then it works like a charm. Can someone explain me this behaviour ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
hudi
  • 15,555
  • 47
  • 142
  • 246
  • What exactly do you want the glass pane to do? – MadProgrammer Apr 11 '13 at 09:49
  • in glass pane is menu ? But why do you want to know it. – hudi Apr 11 '13 at 09:51
  • Just try to figure out what it is you're trying to do – MadProgrammer Apr 11 '13 at 09:52
  • It would be a lot easier to help you if you posted an [SSCCE](http://sscce.org) – Guillaume Polet Apr 11 '13 at 09:56
  • and can you be more specific ? What I miss – hudi Apr 11 '13 at 09:58
  • Your code is not complete, not self-contained, not compilable, nor executable. It's pretty hard to guess what the '...' contains and the content of the classes you have not posted. – Guillaume Polet Apr 11 '13 at 09:59
  • I've done a quick test and I have no issues, there might be something else in your code which is causing issues. A small, runnable example that demonstrates you issues would be of significant help – MadProgrammer Apr 11 '13 at 10:07
  • What I can tell you is that your code looks very weird to me: in the constructor of `TurnajPanel` you call `removeAll()` (but you are still in the constructor, so nothing gets removed), you also call revalidate() and repaint(), but since your `TurnajPanel` has not been added to the hierarchy, it won't do anything. I don't even understand why you extend `JPanel`. – Guillaume Polet Apr 11 '13 at 10:08
  • yes for the first time removeAll has no sence but when someone else want to repaint this component then it remove all component – hudi Apr 11 '13 at 10:14
  • @MadProgrammer update. Now when you run this example and hit key then glass pane disappear – hudi Apr 11 '13 at 10:20
  • @GuillaumePolet now you can run this example. I extend my own JPanel but for the short question I change it. – hudi Apr 11 '13 at 10:23

2 Answers2

1

If you simple add frame.revalidate() after frame.getGlassPane().setVisible(true) it'll work.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • hm maybe I am really blind but frame doesnt have this method – hudi Apr 11 '13 at 10:56
  • @hudi it only appeared in Java7. Instead call `frame.getRootPane().revalidate();` – Guillaume Polet Apr 11 '13 at 11:01
  • but frame.validate() helps too. But I still dont understand why glass pane disapear. I call removeAll on panel which I want to add to frame. In this panel I also creating menu which I want to set to glass pane of frame but I dont remove it. Probably I just dont know what removeAll is exactly doing – hudi Apr 11 '13 at 11:03
  • 1
    @hudi `removeAll` removes all the children components of a Container. The problem is that you never add your `TurnajPanel` to the hierarchy. All you do is set the glass pane to a panel you create in the method createPanel. Actually, you should not need to extends `JPanel` in your class TurnajPanel. As for the call to validate/revalidate, it is necessary to do that because the glasspane has not been laid out by the LayoutManager of the root pane (ie, the size of the glasspane is (0,0)). When you call validate()/revalidate(), the glasspane gets an appropriate size and becomes visible – Guillaume Polet Apr 11 '13 at 11:10
  • So why it also remove glassPane which isnt child of this panel.I am adding scroll panel to frame which contains TurnajPanel. SO I hope turnajPanel is in the hierarchy. and then panel cannot have glassPane. I am adding it to frame – hudi Apr 11 '13 at 11:16
  • You have a instance of turnajPanel, then you create a separate JPanel and set it as the glass pane, neither are connected in any way – MadProgrammer Apr 11 '13 at 18:43
1
  • should be

    1. frame.rootPane.setGlassPane()

    2. add LayoutManager to GlassPane or override getPreferredSize for JPanel

  • shouldn't be

    1. KeyListener to JFrame, JFrame by default missine notifiers in API for KeyEvents

    2. why you overload void(s), create parent JPanel as local variable,

    3. why you overload void(s), child JPanel should be create only once time, then to play only with setVisible(true/false), reset value in JComponents on setVisible(false)

  • see my question

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • why I need to add layoutManager ? default is flow and I am satisfied with it. I add KeyListner just for short example. I didnt use it. – hudi Apr 11 '13 at 11:52
  • just btw. What should I use instead of KeyListener ? KeyStroke ? – hudi Apr 11 '13 at 11:59
  • use KeyBindings, not instead of KeyListener, use that exclusivelly :-) – mKorbel Apr 11 '13 at 12:01
  • and mouseListener to frame is also bad solution ? – hudi Apr 11 '13 at 12:08
  • @mKorbel no need to override getPreferredSize. The LayoutManager of the rootpane (RootLayout) automatically stretches the glasspane to the size of the frame. I don't think it even calls getPreferredSize() on the glasspane. – Guillaume Polet Apr 11 '13 at 12:09
  • all mouse events could be consume() by JComponent GlassPane, GlassPane doesn't to consume() KeyEvents – mKorbel Apr 11 '13 at 12:19
  • @Guillaume Polet this only aka question(my view), everything here, code v.s. comments are opposed, Layout Manager can to possitioning active part of GlassPane to any Rectangle of JFrame's coordinates, then JPanel not covering whole JFrame (as I understrand from this question) – mKorbel Apr 11 '13 at 12:23