问题描述
我想通过在程序中外部提供列索引来实现对 JTable 的排序.这是我的示例代码,其中我初始化了 JTable,向 JTable 添加了一列和 30 行.添加行后,我通过提供列索引 0 对 JTable 进行排序,但无法获得已排序的数据.如何按排序顺序获取我的第一列?我的代码有什么问题.为什么 sortCTableonColumnIndex() 方法无法对指定列索引的数据进行排序?
I would like to implement sorting on JTable by providing column index externally in program. Here is my sample code in which i have initialize JTable, Add one Column and 30 rows to JTable. After rows has been added i am sorting JTable by providing column index 0 but i could not get sorted data. how can i get my first column in sorted order? what's wrong with my code. Why sortCTableonColumnIndex() method could not sort data for specify column index?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Admin
*/
public class Test {
private JTable oCTable;
private DefaultTableModel oDefaultTableModel;
private JScrollPane oPane;
private JTableHeader oTableHeader;
private TableRowSorter sorter;
public void adddata() {
for (int i = 0; i < 30; i++) {
Object[] row = new Object[1];
String sValueA = "A";
String sValueB = "A";
row[0] = "";
if (i % 2 == 0) {
if (i < 15) {
sValueA = sValueA + sValueA;
row[1] = sValueA;
} else {
if (i == 16) {
sValueB = "D";
row[1] = sValueA;
} else {
sValueB = sValueB + sValueB;
row[1] = sValueA;
}
}
} else {
if (i < 15) {
sValueB = sValueB + sValueB;
row[1] = sValueB;
} else {
if (i == 17) {
sValueB = "C";
row[1] = sValueB;
} else {
sValueB = sValueB + sValueB;
row[1] = sValueB;
}
}
}
}
}
public void createTable() {
oCTable = new JTable();
oDefaultTableModel = new DefaultTableModel();
oCTable.setModel(oDefaultTableModel);
oTableHeader = oCTable.getTableHeader();
oCTable.setAutoResizeMode(oCTable.AUTO_RESIZE_OFF);
oCTable.setFillsViewportHeight(true);
JTable oTable = new LineNumberTable(oCTable);
oPane = new JScrollPane(oCTable);
oPane.setRowHeaderView(oTable);
JPanel oJPanel = new JPanel();
oJPanel.setLayout(new BorderLayout());
oJPanel.add(oPane, BorderLayout.CENTER);
JDialog oDialog = new JDialog();
oDialog.add(oJPanel);
oDialog.setPreferredSize(new Dimension(500, 300));
oDialog.pack();
oDialog.setVisible(true);
}
public void insert() {
oDefaultTableModel.addColumn("Name");
int iColumnPlace = ((DefaultTableModel) oCTable.getModel()).findColumn("Name");
CellRendererForRowHeader oCellRendererForRowHeader = new CellRendererForRowHeader();
TableColumn Column = oCTable.getColumn(oTableHeader.getColumnModel().getColumn(iColumnPlace).getHeaderValue());
Column.setPreferredWidth(300);
Column.setMaxWidth(300);
Column.setMinWidth(250);
Column.setCellRenderer(oCellRendererForRowHeader);
for (int i = 0; i < 30; i++) {
Object[] row = new Object[1];
String sValueA = "A";
if (i % 2 == 0) {
if (i < 15) {
sValueA = sValueA + "a";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
} else {
if (i == 16) {
sValueA = sValueA + "b";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
} else {
sValueA = sValueA + "c";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
}
}
} else {
if (i < 15) {
sValueA = sValueA + "d";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
} else {
if (i == 17) {
sValueA = sValueA + "e";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
} else {
sValueA = sValueA + "f";
oDefaultTableModel.insertRow(oCTable.getRowCount(), new Object[]{""});
oDefaultTableModel.setValueAt(sValueA, i, 0);
}
}
}
}
}
public void showTable() {
createTable();
insert();
sortCTableonColumnIndex(0, true);
}
public void sortCTableonColumnIndex(int iColumnIndex, boolean bIsAsc) {
sorter = new TableRowSorter(oDefaultTableModel);
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
if (bIsAsc) {
sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.ASCENDING));
} else {
sortKeys.add(new RowSorter.SortKey(iColumnIndex, SortOrder.DESCENDING));
}
sorter.setSortKeys(sortKeys);
oDefaultTableModel.fireTableStructureChanged();
oCTable.updateUI();
}
public static void main(String[] argu) {
Test oTest = new Test();
oTest.showTable();
}
class CellRendererForRowHeader extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = null;
try {
label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column == 0) {
label.setBackground(new JLabel().getBackground());
label.setForeground(Color.BLACK);
}
} catch (RuntimeException ex) {
}
return label;
}
}
class LineNumberTable extends JTable {
private JTable mainTable;
public LineNumberTable(JTable table) {
super();
mainTable = table;
setAutoCreateColumnsFromModel(false);
setModel(mainTable.getModel());
setAutoscrolls(false);
addColumn(new TableColumn());
getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
getColumnModel().getColumn(0).setPreferredWidth(40);
setPreferredScrollableViewportSize(getPreferredSize());
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
return Integer.valueOf(row + 1);
}
@Override
public int getRowHeight(int row) {
return mainTable.getRowHeight();
}
}
}
推荐答案
你真的应该坚持 Java 编码标准 在编写 Java 代码时,但无论你走哪条路,你至少应该保持一致.
You should really stick to the Java coding standards when writing Java code, but whatever route you go, you should at the very least be consistent.
查看您的代码,您实际上并没有在桌子上设置排序器.来自 JavaDocs 以下是您的代码中缺少:
Looking at your code you don't actually set the sorter on the table.From the JavaDocs the following is missing from your code:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
所以它在你的代码中看起来像这样(或者至少删除了你的非标准约定)
So it would look something like this in your code (or at least with your non-standard conventions removed)
public void createTable() {
tableModel = new DefaultTableModel();
table = new JTable(tableModel);
sorter = new TableRowSorter<DefaultTableModel>(tableModel);
table.setRowSorter(sorter);
...
}
这篇关于如何通过在外部提供列索引来对 JTable 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!