import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
/**
*
*
*/
public class Test extends JPanel{
JScrollPane sb;
JPanel panel;
List itemCB;
List items;
JButton b,all,marked;
int height = 25,width = 200;
int cbWidth = 25;
int xshift = 5,yshift = 5;
String heading;
public Test(String heading){
this.heading = heading;
setBackground(new Color(230,230,250));
setLayout(null);
items = new Vector();
itemCB = new Vector();
initComponent();
}
private void initComponent(){
b = new JButton(){
public void paint(Graphics g){
super.paint(g);
int x[] = {10,13,16};
int y[] = {9,15,9};
g.fillPolygon(x, y, 3);
}
};
b.setSize(cbWidth, height);
b.setLocation(width-cbWidth,0);
b.addActionListener(new ActionListener(){
private boolean clicked = false;
public void actionPerformed(ActionEvent e){
if(!clicked){
showItems();
clicked = true;
}
else{
setSize(width,height);
clicked = false;
}
}
});
add(b);
Label l = new Label(heading){
public void paint(Graphics g){
g.setColor(Color.lightGray);
g.drawRect(0,0, getWidth()-1,getHeight()-1);
}
};
l.setSize(width-cbWidth,height);
l.setLocation(0,0);
add(l);
sb = new JScrollPane();
sb.setSize(width,height*(items.size()+1));
sb.setLocation(1, height+1);
sb.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(sb);
all = new JButton("All");
all.setSize(50, height);
all.setLocation(width-55,getHeight()-30);
all.addActionListener(new ActionListener(){
boolean selected = false;
public void actionPerformed(ActionEvent e){
b.getActionListeners()[0].actionPerformed(e);
if(selected){
for(int i=0;i<itemCB.size();i++)
((JCheckBox)itemCB.get(i)).setSelected(false);
selected = false;
}
else{
for(int i=0;i<itemCB.size();i++)
((JCheckBox)itemCB.get(i)).setSelected(true);
selected = true;
}
}
});
add(all);
marked = new JButton("Marked");
marked.setSize(80, height);
marked.setLocation(width-150,getHeight()-30);
marked.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b.getActionListeners()[0].actionPerformed(e);
getSelectedItems();
}
});
add(marked);
setSize(width,height);
}
public void addItem(String text){
items.add(text);
final JCheckBox cb = new JCheckBox(text);
cb.setSize(width,height);
cb.setLocation(0,height*(items.size()-1));
cb.addMouseListener(new MouseAdapter(){
boolean clicked = false;
@Override
public void mouseEntered(MouseEvent m){
cb.setBackground(Color.lightGray);
}
@Override
public void mouseExited(MouseEvent m){
cb.setBackground(new Color(240,240,240));
}
});
itemCB.add(cb);
}
private void showItems(){
panel = new JPanel();
panel.setLayout(null);
for(int i=0;i<itemCB.size();i++){
panel.add((JCheckBox)itemCB.get(i));
}
panel.setSize(width - 2,height*(items.size()+1)-height);
if(items.size()<9){
sb.setSize(width-2,height*(items.size()+1)-height);
setSize(width,height*(items.size()+1)+height+yshift*2);
}
else{
sb.setSize(width-2,height*(9)-height);
setSize(width,height*(9)+height+yshift*2);
}
sb.setViewportView(panel);
marked.setLocation(width-140,getHeight()-30);
all.setLocation(width-55,getHeight()-30);
}
public void setMarkedButtonActionListener(ActionListener al){
marked.addActionListener(al);
}
public void setAllButtonActionListener(ActionListener al){
all.addActionListener(al);
}
public List getSelectedItems(){
List selectedItems = new Vector();
for(int i=0;i<itemCB.size();i++){
if(((JCheckBox)itemCB.get(i)).isSelected()){
selectedItems.add(items.get(i));
}
}
if(selectedItems.isEmpty())
return null;
return selectedItems;
}
public List getAllItems(){
return items;
}
@Override
public void paint(Graphics g){
super.paint(g);
g.drawRect(0,height, getWidth()-1,getHeight()-height-1);
}
public static void main(String ar[]){
JFrame f = new JFrame();
f.setLayout(null);
f.setSize(500,500);
f.setLocation(400,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Test sp = new Test("");
sp.setLocation(20, 100);
for(int i=0;i<10;i++)
sp.addItem("Item "+(i+1));
f.add(sp);
f.setVisible(true);
}
}