Adding an element to a JList via inputDialog doesnt work
Hi,
I'm having a little trouble with adding elements to a JList. My plan is to have a button 'Other' to pop-up an inputDialog that asks for the name of a computer program to be added to the list.
I used an example from the Java site that uses adirect JTextField (without any pop-up window), which works fine.
However, I definitely would like apop-up inputDialog, but for some reason, it doesn' t work. The value is passed to the method correctly, but the JList doesnt update.
I included the code of a program that demonstrates this behaviour;
- Button Other pops up the inputDialog, and after typing something and pressing Ok, nothing happens.
-After typing something in the JTextfield and pressing button Enter, the JList DOES update.
Note: after using the popup window, the Enter button doesnt work anymore. Why?
And can anyone explain to me why the pop-up window doesnt work? Thanks in advance.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
publicclass JListTryextends JPanelimplements ActionListener,ListSelectionListener{
private JButton buttonOtherProgram =new JButton("Other");
private JButton buttonEnter =new JButton("Enter");
private DefaultListModel listModel =new DefaultListModel();
private JList templateProgramList =new JList(listModel);
private JScrollPane templateProgramScrollPane;
private JTextField programName =new JTextField();
public JListTry(){
super(new BorderLayout());
buildProgramList(null);
}
publicvoid buildProgramList(String programInput){
listModel.addElement("A");
listModel.addElement("B");
listModel.addElement("C");
System.out.println("List contents: "+listModel);
listModel.addElement(programInput);
System.out.println("New program is: "+programInput);
listModel.addElement(programInput);
templateProgramList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
templateProgramList.addListSelectionListener(this);
templateProgramScrollPane =new JScrollPane(templateProgramList);
buttonOtherProgram.addActionListener(this);
buttonEnter.addActionListener(this);
this.add(templateProgramScrollPane, BorderLayout.NORTH);
this.add(buttonOtherProgram, BorderLayout.WEST);
this.add(buttonEnter, BorderLayout.EAST);
this.add(programName, BorderLayout.SOUTH);
}
publicvoid valueChanged(ListSelectionEvent e){
}
publicvoid actionPerformed(ActionEvent e){
if (e.getSource() == buttonEnter){
listModel.addElement(programName.getText());
}
if (e.getSource() == buttonOtherProgram){
String programInputN = JOptionPane.showInputDialog(new JDialog(),
"What program do you want to add?",
"Add program", JOptionPane.QUESTION_MESSAGE);
System.out.println("Dialog method value new prog: "+ programInputN);
buildProgramList(programInputN);
}
}
privatestaticvoid createAndShowGUI(){
//Create and set up the window.
JFrame frame =new JFrame("JListTry");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane =new JListTry();
newContentPane.setOpaque(true);//content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
createAndShowGUI();
}
}

