Interaction between Panels in Java Swings
hai, i m new to Java swings and i dont know how to interact between various panels present in single frame window.the following code is a program in which i need to interact between three panels.Can some one tell me how to do this.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JP extends JFrame
{
JPanel p= new JPanel(new FlowLayout());
public static void main(String a[])
{
JP j= new JP();
}
public JP()
{
Container c=getContentPane();
c.setBackground(Color.black);
JTextFieldj= new JTextField(20);
j.setText("Program");
c.add(j);
//adding panel1 and panel2
p.add(new jp1());
p.add(new jp2());
c.add(p,BorderLayout.EAST);
setVisible(true);
pack();
}
};
class jp1 extends JPanel implements ActionListener
{
JTextField jtf1;
public jp1()
{
JButton b1=new JButton("Connect");
b1.addActionListener(this);
b1.setActionCommand("Connect");
add(b1);
JButton b2=new JButton("Disconnect");
b2.addActionListener(this);
b2.setActionCommand("Disconnect");
add(b2);
jtf1=new JTextField(30);
jtf1.setText("hai");
add(jtf1);
}
public void actionPerformed(ActionEvent ae)
{
String str1=ae.getActionCommand();
System.out.println(str1);
jtf1.setText(str1);
}
};
class jp2 extends JPanel implements ItemListener
{
public jp2()
{
JCheckBox cb1=new JCheckBox("Pt-Pt");
cb1.setSelected(true);
cb1.addItemListener(this);
add(cb1);
JCheckBox cb2=new JCheckBox("Multipoint");
cb2.setSelected(true);
cb2.addItemListener(this);
add(cb2);
}
public void itemStateChanged(ItemEvent ie)
{
JCheckBox str2=(JCheckBox)ie.getItem();
System.out.println(str2.getText());
}
};
I need to display details in the main frame "JP" when performing some actions on panel1(jp1) and panel2(jp2)....
Plz help me with this.....
Thanks to all.
[2180 byte] By [
senthil4ua] at [2007-11-26 14:24:29]

# 1
1. Use code formatting when posting code.2. Explain in more details what do you mean by "display details in the main frame".3. Place jp1 and jp2 as inner classes inside JP then you can access the local members of JP from the inner classes.
# 2
Rodney_McKay thanks for ur reply.
i have aligned the code and the details i need are given below...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JP extends JFrame
{
JPanel p= new JPanel(new FlowLayout());
public static void main(String a[])
{
JP j= new JP();
}
public JP()
{
Container c=getContentPane();
c.setBackground(Color.black);
JTextField j= new JTextField(20);
j.setText("Program");
c.add(j);
//adding panel1 and panel2
p.add(new jp1());
p.add(new jp2());
c.add(p,BorderLayout.EAST);
setVisible(true);
pack();
}
};
class jp1 extends JPanel implements ActionListener
{
JTextField jtf1;
public jp1()
{
JButton b1=new JButton("Connect");
b1.addActionListener(this);
b1.setActionCommand("Connect");
add(b1);
JButton b2=new JButton("Disconnect");
b2.addActionListener(this);
b2.setActionCommand("Disconnect");
add(b2);
jtf1=new JTextField(30);
jtf1.setText("hai");
add(jtf1);
}
public void actionPerformed(ActionEvent ae)
{
String str1=ae.getActionCommand();
System.out.println(str1);
jtf1.setText(str1); // Code required to settext in textbox of main panel
}
};
class jp2 extends JPanel implements ItemListener
{
public jp2()
{
JCheckBox cb1=new JCheckBox("Pt-Pt");
cb1.setSelected(true);
cb1.addItemListener(this);
add(cb1);
JCheckBox cb2=new JCheckBox("Multipoint");
cb2.setSelected(true);
cb2.addItemListener(this);
add(cb2);
}
public void itemStateChanged(ItemEvent ie)
{
JCheckBox str2=(JCheckBox)ie.getItem();
System.out.println(str2.getText()); // Code required to settext in textbox of main panel
}
};
When i click the button in panel1 the message should be displayed in the textbox of main panel.
Similarly the panel2 event should display a message in main panel.
i will try with inner class, u also give me any possible solutions.
# 3
Why not make the JTextField in the JP class a global, private variable, and then either pass it into the constructor of your jp1 and jp2 classes, or use a method to set the reference after they're instantiated?
Here's one example of how you could get it to work (but don't expect this to compile! Use it in your existing code)
public class JP extends JFrame{
private JTextField j;
//your code here
public JP(){
//more of your code here
j = new JTextField(20);
j.setText("Program");
c.add(j);
//adding panel1 and panel2
p.add(new jp1(j));
p.add(new jp2(j));
//more of your code here
}
}
class jp1 extends JPanel implements ActionListener{
JTextField jtf1;
JTextField main_textfield;
public jp1(JTextField maintxt){
//your code
main_textfield = maintxt;
//your code
}
public void actionPerformed(ActionEvent ae){
//your code
maintxt.setText(str1);
}
//similar stuff for jp2
}
If you decide to do it with inner classes instead, you're still going to need to make the JTextField of the main panel global in order to access it outside of the main panel's constructor.
# 4
Thanks MatthewJMurray for the reply.. its working fine....
# 5
This is not a good approach passing GUI object to other classes.
If you don't want to make the jp1 and jp2 inner classes, than the next option would be to let JP implement ActionListenerand pass JP to the other classes and add it as a listener to the button and checkboxs.
Option 1:
public class JP extends JFrame{
private JTextField mainTextField;
//your code here
public JP(){
//more of your code here
mainTextField = new JTextField(20);
mainTextField.setText("Program");
c.add(mainTextField);
//adding panel1 and panel2
p.add(new jp1());
p.add(new jp2());
//more of your code here
}
class jp1 extends JPanel implements ActionListener{
//your code
public void actionPerformed(ActionEvent ae){
//your code
mainTextField.setText(str);
}
//similar stuff for jp2
}
}
Option 2:
public class JP extends JFrame implements ActionListener{
private JTextField mainTextField;
//your code here
public JP(){
//more of your code here
mainTextField = new JTextField(20);
mainTextField.setText("Program");
c.add(mainTextField);
//adding panel1 and panel2
p.add(new jp1(this));
p.add(new jp2(this));
//more of your code here
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("cb1")) {
....
}
else if (ae.getActionCommand().equals("cb2")) {
....
}
else ...
}
}
public class jp1 extends JPanel implements ActionListener{
public jp1(ActionListener listener) {
//your code
b1.addActionListener(listener);
b2.addActionListener(listener);
}
//your code
}
public class jp2 extends JPanel implements ActionListener{
public jp2(ActionListener listener) {
//your code
cb1.setActionCommand("cb1");
cb1.addActionListener(listener);
cb2.setActionCommand("cb2");
cb2.addActionListener(listener);
}
//your code
}
# 6
Thanks for ur replies....i tried and i got it.....