gpt4 book ai didi

java - JTable:按整数排序

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:58:56 25 4
gpt4 key购买 nike

我有一个 JTable,我想有时按整数(大小列)对行进行排序,有时按字符串(文件路径)对行进行排序。所以我在谷歌上搜索它,我已经到了这里。我知道我必须覆盖 DefaultTableModel 的方法,称为 getColumnClass。所以我在这里链接我的代码。

class Personal_model extends DefaultTableModel{

Personal_model(String[][] s,String[] i){
super(s,i);
}


@Override
public Class<?> getColumnClass(int columnIndex){

if (columnIndex!=2)
return String.class;
else
return Integer.class;

}
}

这里是通过模型“Personal_model”创建表的代码;我还设置了 rowsorter。但是这一切都不起作用!!!!!请帮助我

      modeltable = new Personal_model(data,col);   
table = new JTable(modeltable);
table.setRowSorter(new TableRowSorter<Personal_model>(modeltable));

通常情况下,没有我的排序器,一切都是完美的可视化,并且字符串被正确排序(很明显,因为通常它都是按字符串排序的..)

最佳答案

1) 请阅读教程 JTable that's contains TableRowSorter example ,有关 RowSorter 的问题必须在您的代码中

2) 默认情况下,您可以使用 ColumnClass 的以下定义,

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

3) 或者你可以硬编码

    @Override
public Class<?> getColumnClass(int colNum) {
switch (colNum) {
case 0:
return Integer.class;
case 1:
return Double.class;
case 2:
return Long.class;
case 3:
return Boolean.class;
case 4:
return String.class;
case 5:
return Icon.class;
default:
return String.class;
}
}

4) 或覆盖 RowSorter(注意疯狂代码)

enter image description here enter image description here

import com.sun.java.swing.Painter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;

public class JTableSortingIconsForNimbus extends JFrame {

private static final long serialVersionUID = 1L;
private JTable table;
private JTable table1;
private static Icon ascendingSortIcon;
private static Icon descendingSortIcon;

public JTableSortingIconsForNimbus() {
Object[] columnNames = {"Type", "Company", "Shares", "Price"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50)},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
{"Sell", "Apple", new Integer(3000), new Double(7.35)},
{"Buy", "Nortel", new Integer(4000), new Double(20.00)}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {

private static final long serialVersionUID = 1L;

@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {

private static final long serialVersionUID = 1L;

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int firstRow = 0;
int lastRow = table.getRowCount() - 1;
if (row == lastRow) {
((JComponent) c).setBackground(Color.red);
} else if (row == firstRow) {
((JComponent) c).setBackground(Color.blue);
} else {
((JComponent) c).setBackground(table.getBackground());
}
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.NORTH);
table1 = new JTable(model) {

private static final long serialVersionUID = 1L;

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int firstRow = 0;
int lastRow = table1.getRowCount() - 1;
if (row == lastRow) {
((JComponent) c).setBackground(Color.red);
} else if (row == firstRow) {
((JComponent) c).setBackground(Color.blue);
} else {
((JComponent) c).setBackground(table1.getBackground());
}
return c;
}
};
table1.setPreferredScrollableViewportSize(table1.getPreferredSize());
JScrollPane scrollPane1 = new JScrollPane(table1);
//UIDefaults nimbusOverrides = new UIDefaults();
//nimbusOverrides.put("Table.ascendingSortIcon", ascendingSortIcon);
//nimbusOverrides.put("Table.descendingSortIcon", descendingSortIcon);
//table1.putClientProperty("Nimbus.Overrides", nimbusOverrides);
//UIManager.getLookAndFeelDefaults().put("Table.ascendingSortIcon", ascendingSortIcon);
//UIManager.getLookAndFeelDefaults().put("Table.descendingSortIcon", descendingSortIcon);
UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].ascendingSortIconPainter",
new FillPainter1(new Color(255, 255, 191)));
UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].descendingSortIconPainter",
new FillPainter1(new Color(191, 255, 255)));


SwingUtilities.updateComponentTreeUI(table1);
add(scrollPane1, BorderLayout.SOUTH);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()) {

@Override
public void toggleSortOrder(int column) {
if (column >= 0 && column < getModelWrapper().getColumnCount() && isSortable(column)) {
List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
if (!keys.isEmpty()) {
SortKey sortKey = keys.get(0);
if (sortKey.getColumn() == column && sortKey.getSortOrder() == SortOrder.DESCENDING) {
setSortKeys(null);
return;
}
}
}
super.toggleSortOrder(column);
}
};
table.setRowSorter(sorter);
table1.setRowSorter(sorter);
}

static class FillPainter1 implements Painter<JComponent> {

private final Color color;

public FillPainter1(Color c) {
color = c;
}

@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}

public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
ascendingSortIcon = UIManager.getLookAndFeelDefaults().getIcon("Table.ascendingSortIcon");
descendingSortIcon = UIManager.getLookAndFeelDefaults().getIcon("Table.descendingSortIcon");
UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].ascendingSortIconPainter",
new FillPainter1(new Color(127, 255, 191)));
UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].descendingSortIconPainter",
new FillPainter1(new Color(191, 255, 127)));
} catch (Exception fail) {
}
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JTableSortingIconsForNimbus frame = new JTableSortingIconsForNimbus();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

关于java - JTable:按整数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com