this is a checkbox list ctrl for developer java swing application.
it is very simple. you only require implements a method!
require apache commons-lang.jar
For example:
langList = new CheckboxList(ts){
@Override
public Object getDataLabel(Object data){
Book a = (Book)data;
return a.getTitle();
}
};
centerPane.add(langList, BorderLayout.CENTER);
ArrayList as = langList.getSelectedData();
the ts is a Object array that your checkboxlist model.
you will implements the getDataLabel method.
get user's chooser is simple, only invoke langList.getSelectedData()
*/
/**
* this is a checkbox list for swing application
* @author yinzhiguo
*/ public class CheckboxList extends javax.swing.JPanel {
private Vector<BooleanWrap> dataModel = new Vector<BooleanWrap>();
private int columnWidth = 0;
public CheckboxList( Object[] datas) { if(datas == null) throw new java.lang.IllegalArgumentException("datas is null!"); int max = 0; for(Object o : datas)
{
BooleanWrap bw = new BooleanWrap(o); int a = bw.getLabel().length(); if(max < a)
max = a;
dataModel.add(bw);
}
columnWidth = max;
initComponents(); this.setPreferredSize(table.getPreferredSize());
}
public CheckboxList(ArrayList list) { if(list == null || list.isEmpty()) throw new java.lang.IllegalArgumentException("list is null or empty!");
int max = 0; for(Object o : list)
{
BooleanWrap bw = new BooleanWrap(o); int a = bw.getLabel().length(); if(max < a)
max = a;
dataModel.add(bw);
}
columnWidth = max;
initComponents(); this.setPreferredSize(table.getPreferredSize());
}
private TableModel getTableModel() { return new DataModel();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() {
popupMenu = new javax.swing.JPopupMenu();
allMenuItem = getMenuItem("all");
notAllMenuItem = getMenuItem("notAll");
reverseMenuItem = getMenuItem("reverse");
colText = new javax.swing.JTextField();
scrollPane = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
/*
* AlternationColorTableCellRenderer.java
*
* Created on 2007?9?14?, ??10:32
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
* @email [email protected]
* @author yinzhiguo
*/ public class CheckboxListTestFrame extends javax.swing.JFrame {
/** Creates new form CheckboxListTestFrame */ public CheckboxListTestFrame() {
initComponents(); this.setSize(140, 230); this.setTitle("CheckboxList ??");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() {
centerPane = new javax.swing.JPanel();
testButPane = new javax.swing.JPanel();
testBut = getButtonBy("test");
/**
* ??ID??JButton???
* @param id ?????
* @return ????
*/ public JButton getButtonBy(String id){
JButton but = new JButton(); if("test".equals(id)){
but.setText("??");
but.setToolTipText("??");
} return but;
}
private CheckboxList langList = null;
class Book{ private String title = ""; private double price = 0.0;
public String getTitle() { return title;
}
public void setTitle(String title) { this.title = title;
}
public double getPrice() { return price;
}
public void setPrice(double price) { this.price = price;
}
}
private void prepareCheckboxListPane() {
Book t = new Book();
t.setTitle("Java??");
t.setPrice(20.5);
Book t1 = new Book();
t1.setTitle("PHP??");
t1.setPrice(30.5);
Book t2 = new Book();
t2.setTitle("C++??");
t2.setPrice(50.5);
Book t3 = new Book();
t3.setTitle("Python??");
t3.setPrice(30.5);
Book t4 = new Book();
t4.setTitle("Perl??");
t4.setPrice(23.5);
Book t5 = new Book();
t5.setTitle("SmallTalk??");
t5.setPrice(23.5);
Book t6 = new Book();
t6.setTitle("Ruby??");
t6.setPrice(23.5);
Book t7 = new Book();
t7.setTitle("Lisp??");
t7.setPrice(23.5);
Book t8 = new Book();
t8.setTitle("Shell??");
t8.setPrice(23.5);
Book[] ts = {t,t1,t2,t3,t4,t5,t6,t7,t8};
langList = new CheckboxList(ts){
@Override public Object getDataLabel(Object data){
Book a = (Book)data; return a.getTitle();
}
};
centerPane.add(langList, BorderLayout.CENTER);
}
private void testButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButActionPerformed
// TODO add your handling code here:
ArrayList as = langList.getSelectedData(); if(as.isEmpty()){
JOptionPane.showMessageDialog(this, "????????!", "??", JOptionPane.INFORMATION_MESSAGE);
}else{
StringBuffer sub = new StringBuffer();
sub.append("<html>");
sub.append("<b>????:</b><br>");
sub.append("<ul>"); for(Object o : as){
Book b = (Book)o;
sub.append("<li>" + b.getTitle() + ", " + b.getPrice() + "</li>");
}
sub.append("</ul>");
sub.append("</html>");
JOptionPane.showMessageDialog(this, sub.toString(), "??", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_testButActionPerformed
/**
* @param args the command line arguments
*/ public static void main(String args[]) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
CheckboxListTestFrame cltf = new CheckboxListTestFrame();
SwingUtil.setLocationToCenterOfScreen(cltf);
cltf.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel centerPane; private javax.swing.JButton testBut; private javax.swing.JPanel testButPane;
// End of variables declaration//GEN-END:variables