Trouble adding a GridBagLayout inside an ActionListener
Asked Answered
G

2

10

I have a JMenuItem with an ActionListener, in this ActionListener I want to add a GridBagLayout to my frame (which might or might not have a content pane yet added - for testing purposes it doesn't) and then add components to that frame. The design of the frame works on it's own but I want to trigger it from an ActionListener on a JMenuItem and here is where I am running into a problem. It won't display from inside of an ActionListener. I have tried the running the same code from a different method in the class from the AL and that didn't work either.

When I comment out the ActionListener completely, the JLabel I want to test adds to the GBL in the correct place, and the system prints my debug lines here and here2. No syntax errors are picked up by the compiler. This produces a desired result, and the label is printed. (See below for an image of what happens when I comment out the AL completely.) An snippet of the code in question (in which frame is my JFrame) follows:

enter image description here

// (frame created, menus added, etc.) ...
JMenuItem vPoke1Item = new JMenuItem("Pokemon 1");
vPoke1Item.setActionCommand("poke1");
viewMenu.add(vPoke1Item);

//Setup GBL to view stats for Pokemon 1
  vPoke1Item.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
//        debug output
          System.out.println("here");

//        Set up the content pane
          frame.getContentPane().removeAll();
          GridBagLayout gbl = new GridBagLayout();
          GridBagConstraints gbc = new GridBagConstraints();
          Container pane = frame.getContentPane();
          pane.setLayout(gbl);

//        Make a StatCalcObject (all my labels/fields are already initialized)
          StatCalc1 sc1 = new StatCalc1();

//        Add it to pane
          gbc.gridx = 0;gbc.gridy = 0;gbl.setConstraints(sc1.speciesL, gbc);
          pane.add(sc1.speciesL);
          frame.revalidate();
          frame.repaint();

//        debug output
          System.out.println("here2");
      }
  });
// (etc.)

Now when I run this code, I still get the debug lines "here" and "here2" to print so it tells me the ActionListener is running fine. But the Label is not showing up. Still no syntax errors picked up by the compiler. So I am scratching my head here. What am I doing wrong? I hope this code snippet is enough to understand the problem but if you would like the full code I can provide it.

Grouse answered 22/10, 2012 at 2:51 Comment(4)
Since you're radically changing the JFrame, I wonder if you have to repeat a call to pack() on it. If this doesn't help, and you don't get decent help here soon, you may wish to create and post an sscce (please read the link) so that we can run, modify and try to help you fix your problem and code.Tepic
Wow. the extra call to pack() worked! I had called it right after the AL anyway. But now it works, it updates the pane! Thanks a million!Grouse
madbean.com/anim/totallygridbagDecagon
Is there a reason that you are changing an existing frame rather than creating a new one? Alternatively, you should consider refactoring much of your AL code to a class which extends JPanel. Then you can simply create a new instance of the JPanel to add to a JFrame.Unblinking
B
1

Providing you are using fixed size window, everything will work if you replace

frame.revalidate();
frame.repaint();

with

pane.invalidate();
pane.validate();

or

pack();

if you do not have fixed-sized frame. Please note that revalidate is not supported by JFrame or Container. It is also better to replace

gbl.setConstraints(sc1.speciesL, gbc);
pane.add(sc1.speciesL);

with

pane.add(sc1, gbc);

for better code style.

Brandt answered 15/2, 2013 at 1:46 Comment(0)
I
0

Look what is going on when you call method pane.add(sc1.speciesL);

Container.add(sc1.speciesL, null, -1) invoked next

Then Container.addImpl( Component comp, Object constraints, int index )

Then constraints you put before by gbl.setConstraints(sc1.speciesL, gbc); got replaced by null.

  if (layoutMgr instanceof LayoutManager2) {
    ((LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints);
  }

Then panel did not show your new added Components, becouse GridBagConstraints is null now

And you dont actually need to force

  frame.revalidate();
  frame.repaint();
  frame.pack();

All you need to properly add new components to container with proper method :

pane.add(sc1.speciesL, gbc);

And remove uselsess

gbl.setConstraints(sc1.speciesL, gbc);
Ise answered 16/11, 2012 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.