Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble adding a GridBagLayout inside an ActionListener

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.

like image 911
halfmike Avatar asked Oct 22 '12 02:10

halfmike


Video Answer


1 Answers

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.

like image 73
Conarh Avatar answered Nov 09 '22 22:11

Conarh