Java学习笔记(JTable的简单使用)
学习说明
要点:
学习资料为:《Java语言程序设计》梁勇
JTable的简单使用
使用到的组件、函数:
JTable :
JTable 的构造函数
public class TestTable extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Country","Capital","Population in Millions","Democracy"};
private Object[][] data = {
{"USA","Washington DC",280,true},
{"Cananda","Ottawa",32,true},
{"United Kingdom","London",60,true},
{"Germany","Berlin",83,true},
{"France","Paris",60,true},
{"Norway","Oslo",4.5,true},
{"India","New Delhi",1046,true}
};
private JTable jTable = new JTable(data, columnNames);
public TestTable() {
add(new JScrollPane(jTable));
}
public static void main(String[] args) {
JFrame frame = new TestTable();
frame.setTitle("TestTable");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
构造函数说明
运行效果
JTable的一些常用属性介绍
使用到的函数:
setAutoResizeMode :
setGridColor :
setSelectionMode :
setSelectionBackground :
setSelectionForeground :
setRowHeight :
setRowMargin :
setShowGrid :是否显示网格线
public class TablePropertiesDemo extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Country","Capital","Population in Millions","Democracy"};
private Object[][] data = {
{"USA","Washington DC",280,true},
{"Cananda","Ottawa",32,true},
{"United Kingdom","London",60,true},
{"Germany","Berlin",83,true},
{"France","Paris",60,true},
{"Norway","Oslo",4.5,true},
{"India","New Delhi",1046,true}
};
private JTable jTable = new JTable(data, columnNames);
private JSpinner jspiRowHeight = new JSpinner(new SpinnerNumberModel(16, 1, 50, 1));
private JSpinner jspiRowMargin = new JSpinner(new SpinnerNumberModel(1, 1, 50, 1));
private JCheckBox jchShowGrid = new JCheckBox("hsowGrid", true);
private JComboBox<String> jcboAutoResizeMode = new JComboBox<String>();
public TablePropertiesDemo() {
jcboAutoResizeMode.addItem("AUTO_RESIZE_OFF");
jcboAutoResizeMode.addItem("AUTO_RESIZE_LAST_COLUMN");
jcboAutoResizeMode.addItem("AUTO_RESIZE_SUBSEQUENT_COLUMNS");
jcboAutoResizeMode.addItem("AUTO_RESIZE_NEXT_COLUMN");
jcboAutoResizeMode.addItem("AUTO_RESIZE_ALL_COLUMNS");
JPanel panel1 = new JPanel();
panel1.add(new JLabel("rowHeight"));
panel1.add(jspiRowHeight);
panel1.add(new JLabel("rowMargin"));
panel1.add(jspiRowMargin);
panel1.add(jchShowGrid);
JPanel panel2 = new JPanel();
panel2.add(new JLabel("autoResizeMode"));
panel2.add(jcboAutoResizeMode);
add(panel1, BorderLayout.SOUTH);
add(panel2, BorderLayout.NORTH);
add(new JScrollPane(jTable));
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable.setGridColor(Color.BLUE);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setSelectionBackground(Color.RED);
jTable.setSelectionForeground(Color.WHITE);
jspiRowHeight.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jTable.setRowHeight(Integer.parseInt(jspiRowHeight.getValue().toString()));
}
});
jspiRowMargin.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jTable.setRowMargin(Integer.parseInt(jspiRowHeight.getValue().toString()));
}
});
jchShowGrid.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTable.setShowGrid(jchShowGrid.isSelected());
}
});
jcboAutoResizeMode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedItem = (String)jcboAutoResizeMode.getSelectedItem();
if(selectedItem.equals("AUTO_RESIZE_OFF")) {
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
else if(selectedItem.equals("AUTO_RESIZE_LAST_COLUMN")) {
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
}
else if(selectedItem.equals("AUTO_RESIZE_SUBSEQUENT_COLUMNS")) {
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
}
else if(selectedItem.equals("AUTO_RESIZE_NEXT_COLUMN")) {
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
}
else if(selectedItem.equals("AUTO_RESIZE_ALL_COLUMNS")) {
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}
}
});
}
public static void main(String[] args) {
JFrame frame = new TablePropertiesDemo();
frame.setTitle("TablePropertiesDemo");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行效果
表格模型和表格列模型的讲解
表格模型:DefaultTableModel
下标都是从0开始,只能操作行
使用到的函数:
addRow :
insertRow :
addColumn :
setValueAt :
public class TestTableModel extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Country","Capital","Population in Millions","Democracy"};
private Object[][] data = {
{"USA","Washington DC",280,true},
{"Cananda","Ottawa",32,true},
{"United Kingdom","London",60,true},
{"Germany","Berlin",83,true},
{"France","Paris",60,true},
};
private DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
private JTable jTable = new JTable(tableModel);
public TestTableModel() {
add(new JScrollPane(jTable));
tableModel.addRow(new Object[] {"Norway","Oslo",4.5,true});
tableModel.addRow(new Object[] {"India","New Delhi",1046,true});
tableModel.insertRow(0, new Object[] {"India","New Delhi",1046,true});
tableModel.addColumn("Area");
tableModel.setValueAt(10, 0, 4);
tableModel.setValueAt(20, 1, 4);
tableModel.setValueAt(30, 2, 4);
tableModel.removeRow(3);
}
public static void main(String[] args) {
JFrame frame = new TestTableModel();
frame.setTitle("TestTableModel");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行效果
表格列模型:TableColumnModel
可以操作列(当然也只能操作列了)
使用到的函数:
moveColumn :交换两列的位置
removeColumn :移除某一列
public class TestTableColumnModel extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Country","Capital","Population in Millions","Democracy"};
private Object[][] data = {
{"USA","Washington DC",280,true},
{"Cananda","Ottawa",32,true},
};
private JTable jTable = new JTable(data, columnNames);
public TestTableColumnModel() {
add(new JScrollPane(jTable));
TableColumnModel columnModel = jTable.getColumnModel();
columnModel.moveColumn(0, 1);
columnModel.removeColumn(columnModel.getColumn(3));
}
public static void main(String[] args) {
JFrame frame = new TestTableColumnModel();
frame.setTitle("TestTableColumnModel");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行效果
JTable的自动排序和过滤
感觉这个是比较秀的呀
使用到的组件和函数:
TableRowSorter :排序的组件
setRowSorter :设置排序规则
setRowFilter :
setRowFilter(RowFilter.regexFilter(text)) :进行正则匹配
public class TestTableSortFilter extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Country","Capital","Population in Millions","Democracy"};
private Object[][] data = {
{"USA","Washington DC",280,true},
{"Cananda","Ottawa",32,true},
{"United Kingdom","London",60,true},
{"Germany","Berlin",83,true},
{"France","Paris",60,true},
{"Norway","Oslo",4.5,true},
{"India","New Delhi",1046,true}
};
private JTable jTable = new JTable(data, columnNames);
private TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jTable.getModel());
private JTextField jtfFilter = new JTextField();
private JButton jbtnFilter = new JButton("Filter");
public TestTableSortFilter() {
jTable.setRowSorter(sorter);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Specify a word to match:"), BorderLayout.WEST);
panel.add(jtfFilter, BorderLayout.CENTER);
panel.add(jbtnFilter, BorderLayout.EAST);
add(panel, BorderLayout.SOUTH);
add(new JScrollPane(jTable), BorderLayout.CENTER);
jbtnFilter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = jtfFilter.getText();
if(text.trim().length() == 0)
sorter.setRowFilter(null);
else
sorter.setRowFilter(RowFilter.regexFilter(text));
}
});
}
public static void main(String[] args) {
JFrame frame = new TestTableSortFilter();
frame.setTitle("TestTableSortFilter");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行效果
实例学习–修改表格的行和列
最高级的用法了
很高级的效果,就是有一些泛型使用了原始类型,美中不足啊
用到的组件和函数:
setSelectionMode
getSelectedRow
insertRow
addRow
addColumn
removeRow
getColumnModel
setRowCount
setRowSelectionAllowed
public class ModifyTable extends JFrame {
private static final long serialVersionUID = 1L;
private String[] columnNames = { "Country", "Capital", "Population in Millions", "Democracy" };
private Object[][] rowData = {
{ "USA", "Washington DC", 280, true },
{ "Cananda", "Ottawa", 32, true },
{ "United Kingdom", "London", 60, true },
{ "Germany", "Berlin", 83, true },
{ "France", "Paris", 60, true },
{ "Norway", "Oslo", 4.5, true },
{ "India", "New Delhi", 1046, true }
};
private DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);
private JTable jTable = new JTable(tableModel);
private JButton jbtnAddRow = new JButton("Add New Row");
private JButton jbtnAddColumn = new JButton("Add New Column");
private JButton jbtnDeleteRow = new JButton("Delete Selected Row");
private JButton jbtnDeleteColumn = new JButton("Delete Selected Column");
private JButton jbtnSave = new JButton("Save");
private JButton jbtnClear = new JButton("Clear");
private JButton jbtnResotre = new JButton("Restore");
private JComboBox<String> jcboSelectionMode = new JComboBox<String>();
private JCheckBox jchkRowSelectionAllowed = new JCheckBox("RowSelectionAllowed", true);
private JCheckBox jchkColumnSelectionAllowed = new JCheckBox("ColumnSelectionAllowed", true);
public ModifyTable() {
jcboSelectionMode.addItem("SINGLE_SELECTION");
jcboSelectionMode.addItem("SINGLE_INTERVAL_SELECTION");
jcboSelectionMode.addItem("MULTIPLE_INTERVAL_SELECTION");
JPanel panel1 = new JPanel(new GridLayout(2, 2));
panel1.add(jbtnAddRow);
panel1.add(jbtnAddColumn);
panel1.add(jbtnDeleteRow);
panel1.add(jbtnDeleteColumn);
JPanel panel2 = new JPanel();
panel2.add(jbtnSave);
panel2.add(jbtnClear);
panel2.add(jbtnResotre);
JPanel panel3 = new JPanel(new BorderLayout(5, 0));
panel3.add(new JLabel("Selection Mode"), BorderLayout.WEST);
panel3.add(jcboSelectionMode, BorderLayout.CENTER);
JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel4.add(jchkRowSelectionAllowed);
panel4.add(jchkColumnSelectionAllowed);
JPanel panel5 = new JPanel(new GridLayout(2, 1));
panel5.add(panel3);
panel5.add(panel4);
JPanel panel6 = new JPanel(new BorderLayout());
panel6.add(panel1, BorderLayout.SOUTH);
panel6.add(panel2, BorderLayout.CENTER);
add(panel5, BorderLayout.NORTH);
add(new JScrollPane(jTable), BorderLayout.CENTER);
add(panel6, BorderLayout.SOUTH);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jbtnAddRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedRow() >= 0)
tableModel.insertRow(jTable.getSelectedRow(), new Vector());
else
tableModel.addRow(new Vector());
}
});
jbtnAddColumn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog("New Column Name");
tableModel.addColumn(name, new Vector());
}
});
jbtnDeleteRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedRow() >= 0)
tableModel.removeRow(jTable.getSelectedRow());
}
});
jbtnDeleteColumn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedColumn() >= 0) {
TableColumnModel columnModel = jTable.getColumnModel();
TableColumn tableColumn = columnModel.getColumn(jTable.getSelectedColumn());
columnModel.removeColumn(tableColumn);
}
}
});
jbtnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tablemodel.dat"));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jbtnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tableModel.setRowCount(0);
}
});
jbtnResotre.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tableModel.dat"));
Vector rowData = (Vector) in.readObject();
Vector columnNames = (Vector) in.readObject();
tableModel.setDataVector(rowData, columnNames);
in.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
jchkRowSelectionAllowed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTable.setRowSelectionAllowed(jchkRowSelectionAllowed.isSelected());
}
});
jchkColumnSelectionAllowed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTable.setColumnSelectionAllowed(jchkColumnSelectionAllowed.isSelected());
}
});
jcboSelectionMode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedItem = (String) jcboSelectionMode.getSelectedItem();
if (selectedItem.equals("SINGLE_SELECTION")) {
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
} else if (selectedItem.equals("SINGLE_INTERVAL_SELECTION")) {
jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
} else if (selectedItem.equals("MULTIPLE_INTERVAL_SELECTION")) {
jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
}
});
}
private Vector<String> getColumnNames() {
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < jTable.getColumnCount(); i++) {
columnNames.add(jTable.getColumnName(i));
}
return columnNames;
}
public static void main(String[] args) {
JFrame frame = new ModifyTable();
frame.setTitle("ModifyTable");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行效果