user979616
user979616

Reputation: 273

Regarding how to add an something from a text field into a JList

I have a program that needs to take user input (from an input box) and add it to a JList. When I click the Add button on my program however, errors occur.

Heres the code I hoped would work

JButton addButton = new JButton( "<-Add" );
         addButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
                         {
                         final String name=inputField.getText();
                         // prompt user for new philosopher's name

                         // add new philosopher to model
                         philosophers.addElement( name );
                     }
                 }
                 );

Edit: Heres all of the code although ive tested this part and i'm confident it works (Except for the listner I tried to add to the text box)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

    public class PhilosophersJList extends JFrame {

     private DefaultListModel philosophers;
     private JList list;
     private JTextField inputField;

     public PhilosophersJList()
         {
         super( "Favorite Philosophers" );

         // create a DefaultListModel to store philosophers
         philosophers = new DefaultListModel();
         philosophers.addElement( "Socrates" );
         philosophers.addElement( "Plato" );
         philosophers.addElement( "Aristotle" );
         philosophers.addElement( "St. Thomas Aquinas" );
         philosophers.addElement( "Soren Kierkegaard" );
         philosophers.addElement( "Immanuel Kant" );
         philosophers.addElement( "Friedrich Nietzsche" );
         philosophers.addElement( "Hannah Arendt" );

         // create a JList for philosophers DefaultListModel
         list = new JList( philosophers );
         JButton addButton = new JButton( "<-Add" );
         addButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
                         {
                         final String name=inputField.getText();
                         // prompt user for new philosopher's name

                         // add new philosopher to model
                         philosophers.addElement( name );
                     }
                 }
                 );




         // create JButton for removing selected philosopher
         JButton removeButton =
         new JButton( "Rem->" );

         removeButton.addActionListener(
             new ActionListener() {

             public void actionPerformed( ActionEvent event )
                 {
                 // remove selected philosopher from model
                 setTitle("Now Removing Contact");
                 try 
                {
                Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
                } 
                catch(InterruptedException e)
                {
                e.printStackTrace();
                }
                 philosophers.removeElement(list.getSelectedValue());
                 setTitle("My Contacts List");
             }
         }
         );
         JTextField inputField=new JTextField();
         inputField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }

        });

         // allow user to select only one philosopher at a time
         list.setSelectionMode(
         ListSelectionModel.SINGLE_SELECTION );
         //Create the text field


         // create JButton for adding philosophers


         // lay out GUI components
         JPanel inputPanel = new JPanel();
         inputPanel.add( addButton);
         inputPanel.add( removeButton);
         inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.Y_AXIS));

         inputField.setLayout(new FlowLayout());
         inputField.setBounds(5, 5, 100, 100);
         inputField.setPreferredSize(new Dimension(120,20));
         JScrollPane scrollPane=new JScrollPane(list);
         scrollPane.setPreferredSize(new Dimension(200,200));

         Container container = getContentPane();
         add(scrollPane);
         container.add( inputPanel);
         add( inputField);
         container.setLayout(new FlowLayout());

         setDefaultCloseOperation( EXIT_ON_CLOSE );
         setSize( 500, 250 );
         setVisible( true );

     } // end PhilosophersJList constructor

     // execute application
       public static void main( String args[] )
             {
             new PhilosophersJList();
         }
    }

Upvotes: 0

Views: 7317

Answers (1)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

You don't initialize the inputField field. The problem is on line 69, where you declare a new local variable named inputField, instead of assigning the field. You need to actually refer to the inputField field.

So instead of

JTextField inputField = new JTextField();

you should write just

inputField = new JTextField();

Upvotes: 6

Related Questions