==
-
--
---通用的表格设计,弄了我一整天,应该可以满足一般用途了.
1,自己定义表格模型,让boolean值变checkbox
2,有些信息隐藏起来,比如电话号码等,但是可以拿到用.
3,设置事件,这个做的不太好,只可以通过鼠标,
4,这里的一个TO我没有写,补一下
---------------
public class BeautyTo {
private String name;
private String age;
private boolean Beauty;
private String telNum;
GET SET 方法自己弄.JAVABEAN而已.
---------------
5.附件是几张练习体会的图片,可以看看
===
package downtimemodule;
import java.util.ArrayList;
import java.util.UUID;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class HbTT2 extends javax.swing.JFrame {
/** Creates new form HbTT2 */
public HbTT2() {
initComponents();
makeTable();
this.setLocationRelativeTo(null);
}
/** 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">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 546, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 140, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(35, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int i = jTable1.getSelectedRow();
JOptionPane.showMessageDialog(null, "u select row's index is: [" + i + "]");
Object o = jTable1.getValueAt(i, 2);
JOptionPane.showMessageDialog(null, "the check box status is: [" + o + "]");
Object o2 = jTable1.getValueAt(i, 3);
JOptionPane.showMessageDialog(null, "the hidden serise no is: [" + o2 + "]");
}
private void makeTable() {
// initial some data
ArrayList<BeautyTo> arrays = new ArrayList<BeautyTo>();
BeautyTo beautyTo = null;
for (int i = 0; i < 7; i++) {
beautyTo = new BeautyTo();
beautyTo.setAge(i * 5 + "");
beautyTo.setName("avbb" + i);
beautyTo.setBeauty(i % 2 == 0 ? true : false);
beautyTo.setTelNum(UUID.randomUUID().toString());
arrays.add(beautyTo);
}
// make table
String[] columns = {"NAME", "AGE", "IS_BEAUTIFUL", "TEL"};
Object[][] data = new Object[arrays.size()][columns.length];
for (int j = 0; j < arrays.size(); j++) {
data[j][0] = arrays.get(j).getName();
data[j][1] = arrays.get(j).getAge();
data[j][2] = arrays.get(j).isBeauty();
data[j][3] = arrays.get(j).getTelNum();
}
//this is very important
DefaultTableModel defaultTableModel = new MyDefaultTableModel(data, columns);
jTable1.setModel(defaultTableModel);
hideColumn(jTable1, 3);
//jScrollPane1.setViewport(jTable1); already set in the gen code
}
private void hideColumn(JTable jtable, int columnIndex) {
TableColumn tc = jtable.getColumnModel().getColumn(columnIndex);
tc.setWidth(0);
tc.setMinWidth(0);
tc.setMaxWidth(0);
tc.setPreferredWidth(0);
jtable.getTableHeader().getColumnModel().getColumn(columnIndex).setMaxWidth(0);
jtable.getTableHeader().getColumnModel().getColumn(columnIndex).setMinWidth(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new HbTT2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
class MyDefaultTableModel extends DefaultTableModel {
public MyDefaultTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
Class[] types = new Class[]{
String.class, String.class, Boolean.class, String.class
};
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int row, int column) {
if (column == 2) {
return true;
}
return false;
}
}