Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Swing Code Examples

Copy items from one JList to another JList

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Copy items from one JList to another JList import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MultipleSelection extends JFrame { private JList colorList, copyList; private JButton copy; private String colorNames[] = { "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow" }; public MultipleSelection() { super( "Multiple Selection Lists" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); colorList = new JList( colorNames ); colorList.setVisibleRowCount( 5 ); colorList.setFixedCellHeight( 15 ); colorList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); c.add( new JScrollPane( colorList ) ); // create copy button copy = new JButton( "Copy >>>" ); copy.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // place selected values in copyList copyList.setListData( colorList.getSelectedValues() ); } }); c.add( copy ); copyList = new JList( ); copyList.setVisibleRowCount( 5 ); copyList.setFixedCellWidth( 100 ); copyList.setFixedCellHeight( 15 ); copyList.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION ); c.add( new JScrollPane( copyList ) ); setSize( 300, 120 ); show(); } public static void main( String args[] ) { MultipleSelection app = new MultipleSelection(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }