View RSS Feed

fatabass

JList Multiple Selection and Copying with a Button to Another JList Example

Rating: 1 votes, 1.00 average.
by , 12-28-2011 at 10:33 PM (4143 Views)
Java Code:
package myPackage;

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 MultipleSelectionFrame extends JFrame 
{
	private static final long	serialVersionUID	= 1L;
	private JList colorJList; //list to hold the color names	
	private JList copyJList; // list to copy the color names into
	private JButton copyJButton; // button to copy selected names
	private static final String[] colorNames = {"Black","Blue","Cyan","Dark Gray",
		"Gray", "Green","Light Gray","Magenta","Orange","Pink","Red","White","Yellow"};
	
	//MultipleSelectionFrame constructor
	public MultipleSelectionFrame()
	{
		super("Multiple Selection Lists");
		this.setLayout(new FlowLayout());
		
		colorJList = new JList();
		colorJList.setListData(colorNames);
		colorJList.setVisibleRowCount(4);
		this.add(new JScrollPane(colorJList));
		colorJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		
		copyJButton = new JButton(">>>>>");
		this.add(copyJButton);
		copyJButton.addActionListener(new ListenerClass());
		
		copyJList = new JList();
		copyJList.setVisibleRowCount(4);
		copyJList.setFixedCellWidth(100);
		copyJList.setFixedCellHeight(15);
		this.add(new JScrollPane(copyJList));

	}
	
	class ListenerClass implements ActionListener
	{
		
		public void actionPerformed(ActionEvent event) 
		{
			copyJList.setListData(colorJList.getSelectedValues());
		}
	}
	

}//end class MultipleSelectionFrame
Java Code:
package myPackage;

import javax.swing.JFrame;

public class MultipleSelectionListTest 
{
	public static void main(String[] args) 
	{
		MultipleSelectionFrame myFrame = new MultipleSelectionFrame();
		myFrame.setSize(500, 500);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFrame.setVisible(true);
	}// end main()
}// end class MultipleSelectionListTest

Submit "JList Multiple Selection and Copying with a Button to Another JList Example" to Facebook Submit "JList Multiple Selection and Copying with a Button to Another JList Example" to Digg Submit "JList Multiple Selection and Copying with a Button to Another JList Example" to del.icio.us Submit "JList Multiple Selection and Copying with a Button to Another JList Example" to StumbleUpon Submit "JList Multiple Selection and Copying with a Button to Another JList Example" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments