这个例子是JTable可以选择行和列,界面如图
代码如下
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class A7_SelectionModel extends JFrame {
public A7_SelectionModel() {
super("Selection Model Test");
setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel m = new AbstractTableModel() {
public int getColumnCount() {
return 10;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "" + (r+1)*(c+1);
}
};
final JTable jt = new JTable(m);
this.getContentPane().add(new JScrollPane(jt), BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("(Selected Column INdices Go Here)");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getColumnModel().getSelectionModel();
JLabel rowCounter = new JLabel("(Selected Row INdices Go Here)");
rsm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
rowPanel.add(new JLabel("Selected Rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel l, ListSelectionModel m) {
this.debugger = l;
this.model = m;
}
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selecton = getSelectonIndices(model.getMinSelectionIndex(),
model.getMaxSelectionIndex());
if (selecton.length == 0) {
buf.append("NONE");
} else {
for (int i = 0; i < selecton.length - 1; i++) {
buf.append(selecton[i]);
buf.append(",");
}
buf.append(selecton[selecton.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectonIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
public static void main(String args[]) {
AbmUtils.setLookAndFeel();
A7_SelectionModel s = new A7_SelectionModel();
s.setVisible(true);
}
}
</script>