Results 1 to 2 of 2
  1. #1
    Somelauw is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Getting a Jlist to work

    The code is supposed to activate a JList containg a label called hello as soon as the button is pressed.
    Why does nothing happen when I click the activate-button?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    public class JListPractice extends JFrame implements ActionListener {
    	JButton 	 activateButton 	= new JButton("activate");
    	JList		 resultList		= new JList();
    	public static void main(String[] args) {
    		new JListPractice();
    	public JListPractice() {
    		setBounds(200, 200, 500, 500);
    		Container content = getContentPane();
    		content.add(activateButton, BorderLayout.NORTH);
    		content.add(resultList, BorderLayout.CENTER);
    	public void actionPerformed(ActionEvent ev) {
    		if (ev.getSource() == activateButton){
    			//String[] g = {"a", "b", "v"};
    			//for Characters.
    			resultList.add(new JLabel("Hello"));
    			//resultList = new JList(g);
    			getContentPane().add(resultList, BorderLayout.CENTER);
    	private static final long serialVersionUID = 1L;

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    Well, you should not be adding "components" to the "list", you need to add "data" to the "model". So the first thing you need to do is use a ListModel that can be changed dynamically, such as the DefaultListModel. So you need to change:

    Java Code:
    //JList resultList = new JList();
    JList resultList = new JList( new DefaultListModel());
    Then you need to update the model:

    Java Code:
    // resultList.add(new JLabel("Hello"));
    // getContentPane().add(resultList, BorderLayout.CENTER);
    DefaultListModel model = (DefaultListModel)resultList.getModel();
    model.addElement( "Hello" );
    Read the JList API and follow the link to the Swing tutorial on "How to Use Lists" for working examples of using a JList. And download the entire tutorial for future reference.

Similar Threads

  1. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 11:27 AM
  2. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 09:36 AM
  3. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 09:24 PM
  4. JList problem
    By zizou147 in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 08:50 AM
  5. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts