Have been trying to code a program and i am having problems with the listeners. I have a combobox, a bunch of textfields and a 'ok button'. User picks the combo box, and writes in some text into the textfields and hits the 'ok button'. For now i want to just get all the info in varialbes so i can write more code later to write the results to disk. However i cant get the listeners to work right. here are some of the problem points.. blah blah ButtonListener BL=new ButtonListener();
if either i enter info in the box or just leave it emty (JTextField) the output pane would spit out info and it would not work. It complained about that line. roba
What it could be doing is trying to get the selected item when there is no item selected. Maybe try to getSelectedIndex and check that the value is >=0 before you do the getSelectedItem line.
I tried to make declare the variable right off the bat now to see what happens. private JComboBox carType="Cabboose";
but it says 'imcompatible type' so i can not do it that way. Do you want to have a quick look at my code? It is only about 100 lines and the gui parts work fine, just the listener problem. Thanks roba JComboBox carType= new JComboBox(); carType.addItem("Caboose");
blah blah
public class ButtonListener implements ActionListener{
would you possibly have your code structured like this:
if you do, including the 'type' (JComboBox) in the constructor makes that 'carType' local to the constructor. The other 'carType' is then null when accessed from elswhere in your code
public robbox1(){ this.setLocation(120,120); this.setSize (450,420); this.setTitle("Slim Car Builder 2.0"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener bl=new ButtonListener(); // Is this right??? JPanel mainPanel=new JPanel();
JLabel instructions=new JLabel("Please select the Car Type to construct:"); mainPanel.add(instructions);
JLabel twentyline=new JLabel("Enter extra instuctions shown with car on covering with mouse"); mainPanel.add(twentyline); JTextField twentylinebox=new JTextField("bill to Zuni, NM when empty", 25); mainPanel.add(twentylinebox);
JLabel yardNumlab=new JLabel(" Enter yard number for cut of cars to arrive at:"); mainPanel.add(yardNumlab); JTextField yardNumfield=new JTextField("1100 ", 5); mainPanel.add(yardNumfield);
JLabel tracklabel=new JLabel(" Enter yard exit number for cut to arrive on: (track #):"); mainPanel.add(tracklabel); JTextField trackfield=new JTextField("1", 2); mainPanel.add(trackfield);
public class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent e){
if(e.getSource()==buttoncreate){ String start=carID.getText(); //This is the line it complains about if(name.length()==0); buttoncreate.setText("no entry"); }
} /else{
buttoncreate.setText("bad"); }
// basically i want to put all of the JTextFields and JComboBox into // separate String variables that I can use to make another piece of the program (diskwriting) // I think Micheal Dunn is right but I am unsure how to change it to make it work! // Thanks for you time!! // roba