Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JList - Learning Java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question JList - Learning Java

    HI, I'm learning Java but I got a problem when I'm using JList - but I don't know what it is.

    My Main-Class:

    import javax.swing.JFrame;
     
    import GUI_JComboBox.Gui;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		Gui go = new Gui();
    		go.setSize(300, 200);
    		go.setVisible(true);
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    	}
     
    }


    My Gui Class:

    import javax.swing.*;
     
    import java.awt.*;
     
    import javax.swing.event.*;
     
    @SuppressWarnings("serial")
    public class Gui extends JFrame{
     
    	private JList list;
    	private static String[] colornames = {"black", "blue", "red", "white"};
    	private static Color[] colors = {Color.BLUE, Color.BLACK, Color.WHITE, Color.RED};
     
    	public Gui(){
    		super("JList");
    		setLayout(new FlowLayout());
     
    		list = new JList(colornames);
     
    		//This is how many the user can see by default.
    		list.setVisibleRowCount(4);
     
    		//This is so the user only can select ONE.
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    		add(new JScrollPane(list));
     
    		list.addListSelectionListener(
    				new ListSelectionListener(){
     
    					//This is need, when we use the ListSelectionListener.
    					public void valueChanged(ListSelectionEvent event) {
     
    						//This will set the background color.
    						getContentPane().setBackground(colors[list.getSelectedIndex()]);
    					}
     
    				}
    				);
    	}
    }


    Please Help me, I wanna learn Java - so please be nice to me, and explain VERY NOOB-FRENDLY what I did worng, and how I can fix it - Thank You!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JList - Learning Java

    What makes you think you did something wrong? What does this code do? Does it compile? Does it run? Does it exhibit some strange behavior? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Can not clear the contents of JList Java
    By nfs3250 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 01:13 AM
  2. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  3. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  4. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM

Tags for this Thread