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 3 of 3

Thread: JList help

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JList help

    Main (groupingTest)
    import javax.swing.JFrame;
     
    public class groupingTest {
    	public static void main (String[] args) {
    		groupWindow test = new groupWindow();
     
    		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		test.setBounds(400, 400, 500, 150);
    		test.setVisible(true);
    	}
    }

    groupWindow
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
     
    public class groupWindow extends JFrame {
     
     
    	private String[] rightWords = new String[15];
    	private String[] leftWords  = {"Some", "Random", "Crap", "hehe", "yeah", "nahh", "djkfgdjkhdjr", "bleed", "I hate you"};
     
    	// GUI
    	JList rightList = new JList();
    	JList leftList = new JList(leftWords);
    	JButton button = new JButton("MOVE >");
     
    	// Keep count of right list
    	int rightWordCount;
     
    	public groupWindow() {
    		super("Making lists");
    		setLayout(new FlowLayout());
     
    		// create left list
    		leftList.setVisibleRowCount(4);
    		leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
     
    		// create right list
    		rightList.setVisibleRowCount(2);
    		rightList.setFixedCellWidth(50);
    		rightList.setFixedCellHeight(20);
    		rightList.setVisibleRowCount(4);
    		rightList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    		// add everything to our window
    		add(new JScrollPane(leftList));
    		add(button);
    		add(new JScrollPane(rightList));
     
    		// listen for an action on the button
    		button.addActionListener(
    			new ActionListener() {
    				public void actionPerformed(ActionEvent event) {
     
    					//for (int i = 0; i < leftList.getSelectedValuesList().toArray().length; i++) {
    						//rightWords[i] = leftList.getSelectedValuesList().toArray();
    						//rightList.setListData(rightWords);	
    					//}
     
    					rightList.setListData(leftList.getSelectedValuesList().toArray());
    				}
    			}
    		);
    	}
    }

    In the second where it's commented out at the bottom (inside the ActionPerformed(ActionEvent event) I want to be able to put the list into an array and use the array to put inside the right list but I can't seem to find a way to do this (I want to do this so I can finally add to the list and not delete the others, I know that won't do it so far it's incomplete as of now).

    - Nicky


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: JList help

    There's an ArrayList method for converting to an Array. Check the API for that. I don't understand how we can help yet with the rest of your question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JList help

    Thank you, I have seem to found a solution to my problem, which is here:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
     
    public class groupWindow extends JFrame {
     
    	private String[] rightWords = new String[15];
    	private String[] leftWords  = {"Some", "Random", "Crap", "hehe", "yeah", "nahh", "djkfgdjkhdjr", "bleed", "I hate you"};
     
    	// GUI
    	JList rightList = new JList();
    	JList leftList = new JList(leftWords);
    	JButton button = new JButton("MOVE >");
     
    	// Keep count of right list
    	int rightWordCount = 0;
     
    	public groupWindow() {
    		super("Making lists");
    		setLayout(new FlowLayout());
     
    		// create left list
    		leftList.setVisibleRowCount(4);
    		leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
     
    		// create right list
    		rightList.setVisibleRowCount(2);
    		rightList.setFixedCellWidth(75);
    		rightList.setFixedCellHeight(20);
    		rightList.setVisibleRowCount(4);
    		rightList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    		// add everything to our window
    		add(new JScrollPane(leftList));
    		add(button);
    		add(new JScrollPane(rightList));
     
    		// listen for an action on the button
    		button.addActionListener(
    			new ActionListener() {
    				public void actionPerformed(ActionEvent event) {
     
    					for (int i = rightWordCount; i < rightWordCount+leftList.getSelectedValuesList().toArray().length; i++) {
    						if (i==(rightWords.length)) {
    							System.out.print("Reached max limit for this array.\n");
    							break;
    						}
    						else
    						{
    							rightWords[i] = getStringIndex(leftList.getSelectedValuesList().toArray(), i-rightWordCount);
    							rightList.setListData(rightWords);
    						}
    					}
    					if (rightWordCount < rightWords.length)
    					rightWordCount = rightWordCount + leftList.getSelectedValuesList().toArray().length;
    				}
    			}
    		);
    	}
     
    	public String getStringIndex(Object[] word, int index) {
    		return word[index].toString();
    	}
    }

    I made a function (function==method, sorry not originally a Java programmer) which is "getStringIndex" which puts the array into it and also where I'd get to choose the array index at that point. I don't mind doing it this way as it seems reasonable but if there is a way to access the array indexes to functions like that individually, obviously that would be much better. Other then that, it works perfect and it doesn't delete my list now it just keeps adding on well until it's reached its array length...Dynamic arrays? I don't think Java supports them, I was reading up something about collections/containers? ... Well better Study them up... Thanks for the help so far. Brilliant forums.

    - Nicky

Similar Threads

  1. Jlist.
    By lucky123098 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2013, 06:35 AM
  2. Why does JList use Vectors?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2013, 08:39 AM
  3. Updating JList
    By luigi10011 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2013, 11:29 PM
  4. Custom JList
    By daghost in forum AWT / Java Swing
    Replies: 3
    Last Post: November 17th, 2011, 04:57 PM
  5. Updating JList
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 3
    Last Post: October 6th, 2011, 07:17 AM