我正在使用Oracle官方页面上的示例代码。用于排序和过滤表数据的那个。在原始变体中,它有两个字段 - 一个用于输入过滤条件,另一个用于显示从网格中选择哪一行。我不需要第二个,只想留下过滤器字段,但似乎我想念一些东西。下面是我的代码,其中包含对我认为不必要的代码的评论:
public TableFilterDemo() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//Create a table with a sorter.
MyTableModel model = new MyTableModel("customers.dat");
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//For the purposes of this example, better to have a single
//selection.
// table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//When selection changes, provide user with row numbers for
//both view and model.
/* table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = table.getSelectedRow();
if (viewRow < 0) {
//Selection got filtered away.
statusText.setText("");
} else {
int modelRow =
table.convertRowIndexToModel(viewRow);
statusText.setText(
String.format("Selected Row in view: %d. " +
"Selected Row in model: %d.",
viewRow, modelRow));
}
}
}
);
*/
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//Create a separate form for filterText and statusText
JPanel form = new JPanel(new SpringLayout());
JLabel l1 = new JLabel("Student Number:", SwingConstants.TRAILING);
form.add(l1);
filterText = new JTextField();
//Whenever filterText changes, invoke newFilter.
filterText.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}
public void insertUpdate(DocumentEvent e) {
newFilter();
}
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
l1.setLabelFor(filterText);
form.add(filterText);
/* JLabel l2 = new JLabel("Status:", SwingConstants.TRAILING);
form.add(l2);
statusText = new JTextField();
l2.setLabelFor(statusText);
form.add(statusText); */
SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6);
add(form);
}
当我尝试编译时,我遇到了很多错误,但第一个是:
线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:没有这样的孩子:2
答案 0 :(得分:0)
不需要评论的块,但随之而来的是SpringUtilities.makeCompactGrid(form, 1, 2, 6, 6, 6, 6);
中传递的第二个参数应该从2
更改为像我原来的问题一样更改为1
,就像它在这里一样。