Having issues with adding an item to JList.
First of all, the source code which matters.
Code:
DefaultListModel<String> listModel = new DefaultListModel<String>();
JList list = new JList(listModel);
JList list = new JList(listModel);
JTextField lnch_name;
public class lnch_menuadd_btn implements ActionListener {
public void actionPerformed(ActionEvent ev) {
listModel.addElement(lnch_name.getText());
Program.lnchaddGUIHide();
Program.lnchaddGUIClear();
}
}
When the listener is activated, the JTextFields are successfully cleared and the frame is successfully closed, I'm confused because nothing seems to be added to the JList. No errors show up in console.
Am I missing something? I don't see why it's not working.
Also Program is just the object of the class, and the lnch_menuadd_btn action listener class is a inner class. Yes, lnch_name is initialized somewhere else.
Edit: Whoops, I forgot to post this in the swing section. If it could be moved it would be appreciated.
Re: Having issues with adding an item to JList.
Moved to Swing forum. I'm not sure your question is answerable given the information that has been presented. I suspect that your problem lies elsewhere in code not shown.
Edit:
For another thing your code won't even compile as presented since you have two JList variables with the same name. I also wonder if the JLists that have this list model are the ones that are in fact being displayed. But again this all depends on code not yet shown.
Re: Having issues with adding an item to JList.
Quote:
Originally Posted by
Vinx
Also Program is just the object of the class
Please follow standard Java code conventions. A variable name should start with a lowercase letter. And only constants (static final variables) should include an underscore in the name; other variable names should use camelCase.
db
Re: Having issues with adding an item to JList.
Quote:
Originally Posted by
Fubarable
Moved to Swing forum. I'm not sure your question is answerable given the information that has been presented. I suspect that your problem lies elsewhere in code not shown.
Edit:
For another thing your code won't even compile as presented since you have two JList variables with the same name. I also wonder if the JLists that have this list model are the ones that are in fact being displayed. But again this all depends on code not yet shown.
Sorry, I was just copy and pasting out of my code what I thought was related. I must have pasted the JList variables twice on accident.
I believe this is what you're asking for.
Code:
public void buildLunchGUI() {
JFrame lnch_frame = new JFrame("Dvonx - Lunch");
JMenuBar lnch_menubar = new JMenuBar();
// lnch_frame.add(lnch_menubar);
lnch_frame.setJMenuBar(lnch_menubar);
JMenu lnch_file = new JMenu("File");
lnch_menubar.add(lnch_file);
JMenuItem lnch_help = new JMenuItem("Help");
JMenuItem lnch_close = new JMenuItem("Close");
lnch_file.add(lnch_help);
lnch_file.add(lnch_close);
JMenu lnch_add = new JMenu("Add");
JMenuItem lnch_additem = new JMenuItem("Item");
lnch_additem.addActionListener(new lnch_menuadd());
lnch_add.add(lnch_additem);
lnch_menubar.add(lnch_add);
JPanel lnch_northpanel = new JPanel();
lnch_frame.add(BorderLayout.NORTH, lnch_northpanel);
JLabel lnch_lbl_studentid = new JLabel("Student ID:");
lnch_studentid = new JTextField(5);
lnch_northpanel.add(lnch_lbl_studentid);
lnch_northpanel.add(lnch_studentid);
lnch_frame.setSize(500,500);
@SuppressWarnings("rawtypes")
DefaultListModel listModel = new DefaultListModel();
@SuppressWarnings({ "rawtypes", "unchecked" })
JList list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.setVisibleRowCount(20);
JScrollPane listScrollPane = new JScrollPane(list);
lnch_frame.add(listScrollPane, BorderLayout.WEST);
JPanel lnch_secondarypanel = new JPanel();
lnch_frame.add(BorderLayout.CENTER,lnch_secondarypanel);
lnch_console = new JTextArea(8,20);
lnch_console.setEditable(false);
lnch_frame.add(BorderLayout.SOUTH, lnch_console);
lnch_frame.setVisible(true);
}
public void lnchaddGUIBuild() {
lnch_add_frame = new JFrame();
lnch_add_frame.setResizable(false);
JPanel panel = new JPanel();
lnch_add_frame.add(panel);
lnch_lbl_name = new JLabel("Name:");
JLabel lnch_lbl_cost = new JLabel("Cost:");
JButton lnch_btn_add = new JButton("Add");
lnch_btn_add.addActionListener(new lnch_menuadd_btn());
panel.add(BorderLayout.CENTER,lnch_lbl_name);
lnch_name = new JTextField(5);
lnch_cost = new JTextField(5);
panel.add(BorderLayout.CENTER,lnch_name);
panel.add(BorderLayout.CENTER,lnch_lbl_cost);
panel.add(BorderLayout.CENTER,lnch_cost);
panel.add(BorderLayout.SOUTH,lnch_btn_add);
lnch_add_frame.setSize(250,81);
lnch_add_frame.setResizable(false);
lnch_add_frame.setVisible(true);
}
So I realize that I accidentally declared JList once again there, but for some reason if I put it at the top of the class(my usual trick so everything can access the object) my methods don't recognize the object when I try to refer to it.
http://i.imgur.com/nvF5X.png
Edit: The code tags seem to be glitching, they put a lot of extra space on the end.
Re: Having issues with adding an item to JList.
I think that if you copy your code to a new class and start cutting out code unrelated to your problem, paring it down to the bare minimum that allows it to run and show your problem, your error will become obvious to you. If not, then you can post this small but complete and runnable program, and we can experience the problem directly and be much better able to help you. In other words, consider creating and posting an SSCCE.
Re: Having issues with adding an item to JList.
Never mind I was able to finally fix it by just entirely re-doing the JList stuff, I decided a SSCCE would take rather long for me to do due to the size of this project. Thank you for your help anyways, made me realize my code was a mess ;)