Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Objects

Status
Not open for further replies.

jsulman

Programmer
Jun 14, 2001
85
US
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);
}
 
The swing components like panel etc. have several methods to get any childobjects. If that doesn't help, declare some private global variables at the beginning of your class and point them to the various objects in your code so you can reference them directly. Not very pretty but it works.

Kris Simonis
Topdesk Information Systems,
Application Server Development

"You haven't seen the last of Meeaaaarrrrghh!!!"
- Several bad guys in several bad movies

 
Kris,
Thanks for your response. I was also wondering if you (or anybody) thought this was a good way to program. Would it just be easier to have one large class where all the objects, layouts and events are in that class? Or is it better to divide them up like I am currently doing?

Thanks

Jeff
 
I would think it's better to break it down into classes, since you don't have so much code in one file and you will be better able to create pluggable compotents so the application can be modified easier by future programmers. As for the problem with accessing the compotents, make the compotents belong to the instance of the class and create accessor methods that can set and get the values like the text of a particular compotent.

public MainPanel {
private JTextField t;

public MainPanel() {
t = new JTextField("Hello");
getContentPane().add(t);
}
public void setFieldText(String theString) {
t.setText(theString);
}
public String getFieldText()
return(t.getText());
}
}

OOP is your friend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top