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();

}

}

[6043 byte] By [N00dlesa] at [2007-11-27 10:05:24]
# 1

> And can anyone explain to me why the pop-up window doesnt work?

Well why is your code different? The only difference should be the varialble you use to updated the ListModel.

In the first case you use:

listModel.addElement(...);

In the second case you use:

buildProgramList(programInputN);

Notice the difference?

The first way just adds a String to the list.

The second way rebuilds the entire GUI. Why don't you just add the String to the list?

camickra at 2007-7-13 0:40:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

Well, in the actual code, the inputDialog code is in another class, so I dont have direct access to the JList. Therefore, I can't simply use addElement, can I? I have to send the string back to the method/class that contains the JList, where the string will be added to the list.

Or am I completely wrong? Could I simply do classname.jlistname.addElement(programInputN);

?

N00dlesa at 2007-7-13 0:40:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

Somehow you need a reference to the JList and you need the String to add to the JList.

So you can pass the JList as a reference to the input dialog class and then update the list with the string.

Or, you can pass the String back to the class that contains the list and then update the list.

I don't know how your application is written so I can't give you definite advice.

camickra at 2007-7-13 0:40:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

> "Or, you can pass the String back to the class that contains the list and then update the list."

This is what I have in my actual code (but doesnt seem to be working). So I'll try your first suggestion tomorrow. If that doesn't work out, I'll post my actual code. Thanks.

Message was edited by:

N00dles

N00dlesa at 2007-7-13 0:40:27 > top of Java-index,Desktop,Core GUI APIs...