import java.awt.BorderLayout
import java.awt.Component
import javax.swing.DefaultCellEditor
import javax.swing.JCheckBox
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTable
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableCellRenderer
import javax.swing.table.TableColumnModel
@SuppressWarnings("serial")
public class TestJtable extends JPanel {
public TestJtable(){
init()
}
private void init() {
setLayout(new BorderLayout())
JScrollPane scrollPane = new JScrollPane(createTable())
add(scrollPane)
}
private JTable createTable() {
JTable table = new JTable()
String[] tableHeads = new String[]{"姓名","年龄","是否通过"}
DefaultTableModel dtm = (DefaultTableModel)table.getModel()
dtm.setColumnIdentifiers(tableHeads)
/*******添加的一些数据,也可以从数据库中提取数据*************/
dtm.addRow(new Object[]{"aa","12",new Boolean(true)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
dtm.addRow(new Object[]{"aa","12",new Boolean(false)})
dtm.addRow(new Object[]{"bb","34",new Boolean(false)})
/*****设置table的列模型****/
TableColumnModel tcm = table.getColumnModel()
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()))
tcm.getColumn(2).setCellRenderer(new TestTableCellRenderer())
tcm.getColumn(2).setPreferredWidth(80)
tcm.getColumn(2).setWidth(80)
tcm.getColumn(2).setMaxWidth(80)
return table
}
public static void main(String[] args) {
JFrame jframe = new JFrame()
jframe.setBounds(300,100,300,200)
jframe.setTitle("测试")
jframe.add(new TestJtable())
jframe.setVisible(true)
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value
this.setSelected(b.booleanValue())
return this
}
}
}
一直没有时间给你说,不好意思。今天下午有点空。有什么不明白的。可以问我。
忘记你的Jtable把,你要处理的是数据。public class Frame extends javax.swing.JFrame {
TableModel model = new TableModel()
/** Creates new form Frame */
public Frame() {
initComponents()
this.jTable1.setModel(model)
}
class TableModel extends AbstractTableModel {
String[] values = {"乌龟", "海龟", "鸵鸟", "老鸟"}
private int n = 3
@Override
public int getRowCount() {
return (n + 1) * 2
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true
}
public void setIndex(int n) {
this.n = n
this.fireTableStructureChanged()
}
@Override
public int getColumnCount() {
return (n + 1) * 2
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return values[n]
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane()
jTable1 = new javax.swing.JTable()
jPanel1 = new javax.swing.JPanel()
乌龟 = new javax.swing.JButton()
海龟 = new javax.swing.JButton()
鸵鸟 = new javax.swing.JButton()
老鸟 = new javax.swing.JButton()
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
jScrollPane1.setName("jScrollPane1")// NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
))
jTable1.setName("jTable1")// NOI18N
jScrollPane1.setViewportView(jTable1)
jPanel1.setName("jPanel1")// NOI18N
乌龟.setText("乌龟")
乌龟.setName("乌龟")// NOI18N
乌龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
乌龟ActionPerformed(evt)
}
})
海龟.setText("海龟")
海龟.setName("海龟")// NOI18N
海龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
海龟ActionPerformed(evt)
}
})
鸵鸟.setText("鸵鸟")
鸵鸟.setName("鸵鸟")// NOI18N
鸵鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
鸵鸟ActionPerformed(evt)
}
})
老鸟.setText("老鸟")
老鸟.setName("老鸟")// NOI18N
老鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
老鸟ActionPerformed(evt)
}
})
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1)
jPanel1.setLayout(jPanel1Layout)
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(乌龟)
.addGap(18, 18, 18)
.addComponent(海龟)
.addGap(18, 18, 18)
.addComponent(鸵鸟)
.addGap(18, 18, 18)
.addComponent(老鸟)
.addContainerGap(117, Short.MAX_VALUE))
)
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(乌龟)
.addComponent(海龟)
.addComponent(鸵鸟)
.addComponent(老鸟))
.addContainerGap(16, Short.MAX_VALUE))
)
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane())
getContentPane().setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
)
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
)
pack()
}// </editor-fold>
private void 乌龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(0)
}
private void 海龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(1)
}
private void 鸵鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(2)
}
private void 老鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(3)
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true)
}
})
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1
private javax.swing.JScrollPane jScrollPane1
private javax.swing.JTable jTable1
private javax.swing.JButton 乌龟
private javax.swing.JButton 海龟
private javax.swing.JButton 老鸟
private javax.swing.JButton 鸵鸟
// End of variables declaration
}
JCheckBox i0=new JCheckBox(“武术”);JCheckBox i1=new JCheckBox(“体育”);
JCheckBox i2=new JCheckBox(“语文”);
。。。创建不同对象,并初始化时候给予String的参数即标题即可,使用时候,只要将这一个个对象添加到相应容器中即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)