dynamically creating a jpanel

hello again, I made a class that dynamically creates a JPanel and then an action listener to create it in the MAINPanel class which is in flowlayout and lefttoright in component orientation.... however it doesnt show the jpanel taht is created.. any ideas on what im missing?

public JPanel addPanel(){

JPanel newPanel=new JPanel();

newPanel.setSize(89,192);

getMainPanel().add(newPanel,null);

GridLayout blah =new GridLayout();

blah.setRows(8);

JLabel lol =new JLabel();

lol.setText("test");

newPanel.setVisible(true);

newPanel.setLayout(blah);

//action performed method

if ((arg0.getActionCommand().equals("blah"))){

System.out.print("Called");

//JPanel nuevo=new JPanel();

//nuevo.setPreferredSize(new java.awt.Dimension(89,192));

addPanel();

}

thank a bunch

[1355 byte] By [h2opologirly69a] at [2007-11-27 6:04:49]
# 1

I sometimes create an addPanel routine where I make a panel just the way I like in a method but don't add this panel to any superior panel while I'm in my method. I return the panel from the method and then add that to the parent panel. Something like so:

// create a panel here but don't add it to anything

private JPanel addPanel()

{

JPanel newPanel = new JPanel();

newPanel.setPreferredSize(new Dimension(89, 192));

newPanel.setLayout(new GridLayout(8, 0));

for (int i = 0; i < 16; i++)

{

newPanel.add(new JLabel("test " + i));

}

// I don't add this panel to anything just yet,

// rather I return it from the method here

return newPanel;

}

Here is where I call and add this new panel:

class BriefSwing extends JFrame implements ActionListener

{

private JPanel myPane = new JPanel();

.........

public BriefSwing()

{

super("Brief Swing App");

createWidgets();

getContentPane().add(myPane);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setVisible(true);

}

private void createWidgets()

{

...........

// add my returned panel here

myPane.add(addPanel(), BorderLayout.EAST);

}

I'm no swing expert, but this works for me. Perhaps it might work for you.

petes1234a at 2007-7-12 16:49:35 > top of Java-index,Desktop,Core GUI APIs...
# 2
It seems you have forgotten this from the addPanel() method:newPanel.add(lol);Please also bear in mind no repaints will be made while processing events (e.g. until your actionPerformed() is finished).
sztyopeka at 2007-7-12 16:49:35 > top of Java-index,Desktop,Core GUI APIs...
# 3
When you add component to panel when the GUI is already visible then you need to revalidate() the panel so that the layout manager gets invoked. Sometimes you also need to repaint() the panel.
camickra at 2007-7-12 16:49:35 > top of Java-index,Desktop,Core GUI APIs...
# 4
newPanel.setVisible(true); won't do this job :(
sztyopeka at 2007-7-12 16:49:35 > top of Java-index,Desktop,Core GUI APIs...