Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding JPanels from other objects  RSS feed

 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make an application that delegates some of the JFrame components to other classes, as in the simple example below, but only an empty JPanel is returned.

public class RunTest{

JFrame frame = new JFrame();
Container cont = frame.getContentPane();
ArrayList<Test> tests = new ArrayList<Test>();
Test test1 = new Test();
Test test2 = new Test();
Test test3 = new Test();
Test test4 = new Test();

public static void main(String[] args){

new RunTest().makeGUI();
}

public void makeGUI(){

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

for (Test t: tests){

t.makePanel();
cont.add(t.getPanel);
}

frame.setVisible(true);
}
}




public class Test{

JPanel panel;
private JLabel label;

public void makePanel(){

panel = new JPanel();

JButton option1 = new JButton("Option 1");
JButton option2 = new JButton("Option 2");
label = new JLabel();

panel.add(option1);
panel.add(option2);
panel.add(label);
}

public JPanel addPanel(){

return JPanel;
}
}

I have also tried rewriting the addPanel() method as below:

public void addPanel(RunTest r){

r.cont.add(panel);
}

but it makes no difference.

Am I doing something wrong, or is there no way to delegate parts of the GUI to other objects?
 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FORGIVE ME - I MADE A STUPID MISTAKE AND FORGOT TO ADD THE TEST OBJECTS TO THE ARRAYLIST. THERE IS STILL A PROBLEM, BUT I WILL START A NEW THREAD TO DISCUSS IT. SORRY!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!