imnotmarvin
imnotmarvin

Reputation: 55

How do I add data to a JList via user input captured by JOptionPane?

I have a JButton named 'course' that has an actionListener that populates a JOptionPane asking the user to enter a course name. I'm trying to add the course name to the JList to display after the add course action is completed. I've tried classList.add(input) in the actionListener method but it's not working. Any help is appreciated. This is for class so tips are appreciated; I'm not after full code. Thanks.

import javax.swing.*;

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

public class LabEleven extends JFrame{

    public LabEleven(){
String[] list = {"Math", "\nComputer", "\nPhysics", "\nChemistry"}; // create array     of String data for JList
JList<String> classList = new JList<String> (list); // create JList to pass to     JPanel

JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
p1.add(classList);
p1.setBackground(Color.white);

JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));


// add "add course" button and attach action listener
JButton course = new JButton("Add Course");
course.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        String input = (JOptionPane.showInputDialog("Please enter another course"));

    } // end actionPerformed

}); // end addActionListener

// add "close" button and attach action listener
JButton close = new JButton("Close");
close.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);

    } // end actionPerformed

}); // end addActionListener


p2.add(course);
p2.add(close);


add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);

} // end LabEleven constructor

public static void main(String[] args) {
    JFrame frame = new LabEleven();
    frame.setSize(400, 420);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

} // end main method

} // end LabEleven class

Upvotes: 0

Views: 1550

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209004

You should be using a ListModel when dealing with the JList, for example, an easily manageable DefaultListModel. You can then use the method DefaultListModel.addElement for dynamic population of the list.

You first need to initialize your list with the model

final DefaultListModel model = new DefaultListModel();
JList jList = new JList(model);
// you can loop to populate the model here with your default list[] data

After doing that, you can call model.addElement(input) in your listener


See more at How to use Lists and maybe focus on the section using models.

Also see the API for DefaultListModel for more available methods.

Upvotes: 1

noobakologed
noobakologed

Reputation: 13

This can be helpful: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#creating Manipulating JList after declaring It's model can be very effective.

Upvotes: 0

Related Questions