I am currently writing my first Swing Program and have was wondering if any body could help me.
One of my screens has about 40 different objects and as I added the various objects, layouts and events I noticed that the class was getting to be rather large.
What I hoped to do was to group the various objects into JPanel classes and instantiate them in the main class. Then all the code for the objects, layouts and events would be in separate classes and easier to code (or so I thought). However, I am having a hard time accessing the objects on the class panels from the main class.
For example if I have a Class FirstPanel and on that panel I add a JTextField object and then add the FirstPanel to the MainPanel, how do I access the events in JTextField from the MainPanel?
I use to work in PowerBuilder and there we could simply say MainPanel.FirstPanel.JTextField.setText(“We are Here”). Not so in Java.
Below is some pseudo code to explain what I am doing.
Is what I want to do possible, or am I going about it all wrong?
Thanks in advance.
Jeff
Class MainPanel extends JFrame
{
JPanel FirstPanel = FirstPanel();
JPanel SecondPanel = SecondPanel();
public MainPanel
{
FirstPanel = FirstPanel();
SecondPanel = SecondPanel();
getContentPane.add(FirstPanel);
getContentPane.add(SecondPanel);
}
public static void main(String[] args)
{
MainPanel f = new OpenDialog(MainPanel);
f.pack();
f.visible(true);
} //public static void main(String[] args)
}
Class FirstPanel extends JPanel
{
JLabel lbl1 = new JLabel("First Label"
JTextField txf1 = new JTextField();
getContentPane.add(lbl1);
getContentPane.add(txf1);
}
Class FirstPanel extends JPanel
{
JLabel lbl2 = new JLabel("First Label"
JTextField txf2 = new JTextField();
getContentPane.add(lbl2);
getContentPane.add(txf2);
}
One of my screens has about 40 different objects and as I added the various objects, layouts and events I noticed that the class was getting to be rather large.
What I hoped to do was to group the various objects into JPanel classes and instantiate them in the main class. Then all the code for the objects, layouts and events would be in separate classes and easier to code (or so I thought). However, I am having a hard time accessing the objects on the class panels from the main class.
For example if I have a Class FirstPanel and on that panel I add a JTextField object and then add the FirstPanel to the MainPanel, how do I access the events in JTextField from the MainPanel?
I use to work in PowerBuilder and there we could simply say MainPanel.FirstPanel.JTextField.setText(“We are Here”). Not so in Java.
Below is some pseudo code to explain what I am doing.
Is what I want to do possible, or am I going about it all wrong?
Thanks in advance.
Jeff
Class MainPanel extends JFrame
{
JPanel FirstPanel = FirstPanel();
JPanel SecondPanel = SecondPanel();
public MainPanel
{
FirstPanel = FirstPanel();
SecondPanel = SecondPanel();
getContentPane.add(FirstPanel);
getContentPane.add(SecondPanel);
}
public static void main(String[] args)
{
MainPanel f = new OpenDialog(MainPanel);
f.pack();
f.visible(true);
} //public static void main(String[] args)
}
Class FirstPanel extends JPanel
{
JLabel lbl1 = new JLabel("First Label"
JTextField txf1 = new JTextField();
getContentPane.add(lbl1);
getContentPane.add(txf1);
}
Class FirstPanel extends JPanel
{
JLabel lbl2 = new JLabel("First Label"
JTextField txf2 = new JTextField();
getContentPane.add(lbl2);
getContentPane.add(txf2);
}