aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Not able to read the value of a component on one JPanel from a different JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Not able to read the value of a component on one JPanel from a different JPanel" Watch "Not able to read the value of a component on one JPanel from a different JPanel" New topic
Author

Not able to read the value of a component on one JPanel from a different JPanel

mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
I have three JPanels. One consists of JComboBox, Second JPanel consists of Labels and textfields and the third panel consists of JButtons. All the three panels are attached to a a single panel and a displayed on a frame.Now Ad I enter values on the textfield and click the add button on the third panel, the value in the textfield should get added to the JComboBox on the first JPanel.

I am not able to retrive the combobox and set the value to it . The code is in the button.addActionListener().

I need a code which adds the persons last name to JCombobox. It is at line Number 173.

The code is Pasted below. I could not attach .java ot .txt file.

for(int i=0; i<srhPanel.getComponentCount(); i++)
{
////////////////////I need the code here //if(srhPanel.getComponent(i).getClass().equals(JComboBox.class))
// Component[] comparr = srhPanel.getComponents();
// System.out.println("Yesssss //"+srhPanel.getComponent(i).getClass().equals(JComboBox.class));
}
}
});


package com.pattern.addressbook;


import javax.swing.event.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;


public class GenerateGUI extends JFrame {
//Main Pane
JPanel mainPane = new JPanel();
ArrayList personList = new ArrayList();
Person person;

//Search Pane
private JPanel srhPanel = new JPanel();
private JLabel lblSearch = new JLabel("Click the search element");
private JComboBox cmbLname = new JComboBox();


//Form pane
private JPanel frmPanel = new JPanel();
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
private JLabel label6;
private JLabel label7;
private JLabel label8;

private JTextField txtFname;
private JTextField txtLname;
private JTextField txtAge;
private JTextField txtHno;
private JTextField txtStreet;
private JTextField txtCity;
private JTextField txtState;
private JTextField txtZip;

//ButtonPane
private JPanel btnPanel = new JPanel();
private JButton btnAdd;
private JButton btnEdit;
private JButton btnDelete;
private JButton btnClear;
private JButton btnPrevious;
private JButton btnNext;

public void initiate() {

//Search Pane
generateSearchPane();


//Form Pane
gererateFormPane();

//Button Pane
generateButtonPane();

mainPane.setLayout(new BoxLayout(mainPane,BoxLayout.Y_AXIS));
mainPane.add(srhPanel);
mainPane.add(frmPanel);
mainPane.add(btnPanel);

getContentPane().add(mainPane);
//setSize(400,400);
pack();
setVisible(true);
}

public void generateSearchPane()
{
srhPanel.setLayout(new FlowLayout());
srhPanel.add(lblSearch);
srhPanel.add(cmbLname);
lblSearch.setLabelFor(cmbLname);
}
public void gererateFormPane()
{
frmPanel.setLayout(new GridLayout(4,4));
label1 = new JLabel("First Name");
label2 = new JLabel("Last Name");
label3 = new JLabel("Age");
label4 = new JLabel("HNo:");
label5 = new JLabel("Street");
label6 = new JLabel("City");
label7 = new JLabel("State");
label8 = new JLabel("Zip");

txtFname = new JTextField();
txtLname = new JTextField();
txtAge = new JTextField();
txtHno = new JTextField();
txtStreet = new JTextField();
txtCity = new JTextField();
txtState = new JTextField();
txtZip = new JTextField();

frmPanel.add(label1);
frmPanel.add(txtFname);
frmPanel.add(label2);
frmPanel.add(txtLname);
frmPanel.add(label3);
frmPanel.add(txtAge);
frmPanel.add(label4);
frmPanel.add(txtHno);
frmPanel.add(label5);
frmPanel.add(txtStreet);
frmPanel.add(label6);
frmPanel.add(txtCity);
frmPanel.add(label7);
frmPanel.add(txtState);
frmPanel.add(label8);
frmPanel.add(txtZip);

label1.setLabelFor(txtFname);
label2.setLabelFor(txtLname);
label3.setLabelFor(txtAge);
label4.setLabelFor(txtHno);
label5.setLabelFor(txtStreet);
label6.setLabelFor(txtCity);
label7.setLabelFor(txtState);
label8.setLabelFor(txtZip);
}

public void generateButtonPane()
{
btnPanel.setLayout(new FlowLayout());
btnAdd = new JButton("Add");
btnEdit = new JButton("Edit");
btnDelete = new JButton("Delete");
btnClear = new JButton("Clear");
btnPrevious = new JButton("<<");
btnNext = new JButton(">>");
btnPanel.add(btnAdd);
btnPanel.add(btnEdit);
btnPanel.add(btnDelete);
btnPanel.add(btnClear);
btnPanel.add(btnPrevious);
btnPanel.add(btnNext);


btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
person = new Person();
person.setFirstName(txtFname.getText());
person.setLastName(txtLname.getText());
person.setAge(txtAge.getText());
person.setHno(txtHno.getText());
person.setStreet(txtStreet.getText());
person.setCity(txtCity.getText());
person.setState(txtState.getText());
person.setZip(txtZip.getText());
personList.add(person);
for(int j=0;j< personList.size();j++)
{
System.out.println("Person "+j+" LastName = " + person.getLastName());
System.out.println("Search Panel Component count is "+srhPanel.getComponentCount());

}

for(int i=0; i<srhPanel.getComponentCount(); i++)
{
////////////////////I need the code here //if(srhPanel.getComponent(i).getClass().equals(JComboBox.class))
// Component[] comparr = srhPanel.getComponents();
// System.out.println("Yesssss //"+srhPanel.getComponent(i).getClass().equals(JComboBox.class));
}
}
});
}

public ArrayList getPersons()
{
return personList;
}

public Person getPerson()
{
return person;
}

}


Prasad Regula
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
mvPrasad Regula wrote:I have three JPanels. One consists of JComboBox, Second JPanel consists of Labels and textfields and the third panel consists of JButtons. All the three panels are attached to a a single panel and a displayed on a frame.Now Ad I enter values on the textfield and click the add button on the third panel, the value in the textfield should get added to the JComboBox on the first JPanel. ...


You probably want to use a DefaultComboBoxModel as your combobox's model. If this variable is given class scope, then you can easily update it from your action listener.

Other tips: when trying to fix a problem like this, it is best to try solving it in a very simple program first, say one that has one JTextField, one combo box and one button. Then if you run into problems you can post this very small program here (which will be easier for us to work with too). Next, you'll want to use code tags when posting code, so that it will be easier to read. The forum FAQ will explain how.

For example:


Best of luck!
mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
Here both are in the same Panel. I am getting problem when they both are in different panels.
mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
When I say
srhPanel.getComponent(i) I am getting the component Object not the JComboBox object
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
mvPrasad Regula wrote:Here both are in the same Panel. I am getting problem when they both are in different panels.


This really shouldn't matter. Components don't care if they are placed in separate JPanels:


But if these examples don't help you, I recommend that you create yourself a small compilable program that demonstrates your problem and that we can run, an SSCCE. The link will show you how to create one of these.

Much luck!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
mvPrasad Regula wrote:When I say
srhPanel.getComponent(i) I am getting the component Object not the JComboBox object


Again, you want to use a DefaultComboBoxModel. You don't want to get a reference to your combobox or its model by getComponent(). It's a very brittle design. Make your combo box and its model a class field and refer to it directly, not indirectly.
mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
for(int i=0; i<srhPanel.getComponentCount(); i++) //This line searches the other panel for the components
{
if(srhPanel.getComponent(i).getClass().equals(JComboBox.class)) // Here I need to see if the component is
// JComboBox in case if it is so, I need to set
// the value to it. To set the value I need to
//get the JComboBox API. Component API
//wont work as it doesnt have the method
//related to setting of value.
Component[] comparr = srhPanel.getComponents();
System.out.println("Yesssss "+srhPanel.getComponent(i).getClass().equals(JComboBox.class));
}
mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
got it. I never knew that different panels doesnt matter. Thanks for the reply.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
edit: nevermind
mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
Yes I did , but the only mistake that happened was when you replied earlier I was typing on my side so I checked the solution after posting. At the same time I have one quick question. If the person class has firstname lastname, address in it where address is a variable of Address class which has house number, street, city, state and zip. As I try to get the value as person.getAddress().setCity(txtCity.getText()) it is throwing a Nullpointer error. This code was written in the inner class of

btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Code was written here.

}
});

So I was forced to declare housenumber, street, city,... as part of Person class. Could you explain why it is so.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
mvPrasad Regula wrote:Yes I did , but the only mistake that happened was when you replied earlier I was typing on my side so I checked the solution after posting.

No problem.

At the same time I have one quick question. If the person class has firstname lastname, address in it where address is a variable of Address class which has house number, street, city, state and zip. As I try to get the value as person.getAddress().setCity(txtCity.getText()) it is throwing a Nullpointer error. This code was written in the inner class of


So I was forced to declare housenumber, street, city,... as part of Person class. Could you explain why it is so.

Again, please use code tags. It's painful to try to read unformatted code.

You'll need to do some debugging by first finding out which object is null. The NPE will tell you which line throws the error, then modify the code with a few println statements to help you out. e.g.,



Then you'll need to go through your code to figure out why that object is null.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

mvPrasad Regula, please Use Code Tags from now on. Look at Pete's posts and how much easier his code is to read.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Not able to read the value of a component on one JPanel from a different JPanel