关于java Swing的JTable多项选择的问题
在做“选择课程并交费”选项卡,课程编号可以多选怎么做?(即可以选择多行记录),
用 JTabbedPane和JTable做,新手求帮助,各位帅哥美女赶快过来瞅瞅啊!
[解决办法]
课程编号跟选择框做成两列,下面写个简单例子给你看:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class FF {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FF window = new FF();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FF() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
JTable tab = new JTable();
CustomModel mod = new CustomModel();
mod.addColumn("选择");
mod.addColumn("课程编号");
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
Object[] data = {new Boolean(false),"03"};
Object[] data1 = {new Boolean(false),"04"};
mod.addRow(data);
mod.addRow(data1);
tab.setModel(mod);
scrollPane.getViewport().add(tab);
}
class CustomModel extends DefaultTableModel {
public boolean isCellEditable(int row, int column) {
if (column > 0) {
return false;
} else {
return true;
}
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
}