Friday, 15 August 2014

JAVA -JList multiple selection list

An implementation of JList to select multiple girl's name and enjoy dumping them lol

Basically we gonna select multiple list items from left and show them at right list with click event of button pressed ,with java's inbuilt function , while setting action listener  for button  -

any_list_name.setListData(any_other_list_name.getSelectedValues()); 


Main class-
import javax.swing.JFrame;


public class Main extends JFrame {
public static void main(String[]  args){
GUI go = new GUI();

//defines the operation done on closing the app , programme terminates
//exit the JFrame 
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set the frame size
go.setSize(275, 275);

//set visibility
go.setVisible(true);
}

}


JList with multiple selection -

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 GUI extends JFrame{

private JList leftList, rightList;
private static String[] girls = { "jenny", "lopez", "stacy",
"jessica", "avril", "cheryl", "cher" };
private JButton moveButton;



//constructor
public GUI(){

super("dump em up");
setLayout(new FlowLayout());
leftList =  new JList(girls);
leftList.setVisibleRowCount(3);
//set the left list to select multiple list items
leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//add the view 
add(new JScrollPane(leftList));

//set the button and its functionality
moveButton = new JButton("Dump em up :P ----> ");
moveButton.addActionListener(
// create an anonymous class
new ActionListener(){
public void  actionPerformed(ActionEvent event){
//Returns an array of all the selected values, 
//in increasing order based on their indices in the list.

rightList.setListData(leftList.getSelectedValues());

}
}
);
add(moveButton);

rightList = new JList();
rightList.setVisibleRowCount(3);
rightList.setFixedCellWidth(100);
rightList.setFixedCellHeight(15);
rightList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//add the view 
add(new JScrollPane(rightList));


}


}








No comments:

Post a Comment