我正在通过Java查询一个MySQL数据库,从结果集中提取数据并将其放入DefaultTableModel,该Model被放入JTable,然后被放入JScrollPane中显示。
table.setAutoCreateRowSorter(true);
java.sql.Date
java.sql.Time
java.lang.Double
java.lang.Boolean
java.lang.String
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Double `enter code here`cannot be cast to java.lang.String
at java.text.Collator.compare(Collator.java:327)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:968)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:112)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1376)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1366)
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:607)
at javax.swing.DefaultRowSorter.setSortKeys(DefaultRowSorter.java:319)
at javax.swing.DefaultRowSorter.toggleSortOrder(DefaultRowSorter.java:480)
at javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(BasicTableHeaderUI.java:112)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:269)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
以下是该类的源代码:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class ResultSetToCheckBoxTable {
private int numberOfColumns;
private DefaultTableModel model;
private ResultSetMetaData metaData;
private JTable table;
private ResultSet rs;
private TableColumn tc;
public JTable getTable() {
return table;
}
public ResultSetToCheckBoxTable(ResultSet rs) {
try {
this.rs = rs;
//overrides getColumnClass method in DefaultTableModel to help make checkbox
model = new DefaultTableModel() {
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == numberOfColumns) {
return Boolean.class;
} else {
//return getValueAt(0, columnIndex).getClass();
return String.class;
}
}
};
// Get the column names
metaData = this.rs.getMetaData();
numberOfColumns = metaData.getColumnCount();
for (int column = 1; column <= numberOfColumns; column++) {
model.addColumn(metaData.getColumnLabel(column));
}
model.addColumn("");
// Get the table info
while (rs.next()) {
Vector tableRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
tableRow.addElement(rs.getObject(i));
}
tableRow.addElement(true);
model.addRow(tableRow);
}
//Create JTable using the model & set options
table = new JTable(model) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
};
};
table.setAutoCreateRowSorter(true);
table.setPreferredScrollableViewportSize(new Dimension(320, 160));
//Rest of the stuff
tc = table.getColumnModel().getColumn(numberOfColumns);
tc.setHeaderRenderer(new SelectAllHeader(table, numberOfColumns));
}
catch (SQLException ex) {
Logger.getLogger(ResultSetToCheckBoxTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
}
}
/**
* A TableCellRenderer that selects all or none of a Boolean column.
*
* @param numberOfColumns the Boolean column to manage
*/
class SelectAllHeader extends JToggleButton implements TableCellRenderer {
private static final String ALL = "✓ Select all";
private static final String NONE = "✓ Select none";
private JTable table;
private TableModel tableModel;
private JTableHeader header;
private TableColumnModel tcm;
private int targetColumn;
private int viewColumn;
public SelectAllHeader(JTable table, int targetColumn) {
super(ALL);
this.table = table;
this.tableModel = table.getModel();
if (tableModel.getColumnClass(targetColumn) != Boolean.class) {
throw new IllegalArgumentException("Boolean column required.");
}
this.targetColumn = targetColumn;
this.header = table.getTableHeader();
this.tcm = table.getColumnModel();
this.applyUI();
this.addItemListener(new ItemHandler());
header.addMouseListener(new MouseHandler());
tableModel.addTableModelListener(new ModelHandler());
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
return this;
}
private class ItemHandler implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
boolean state = e.getStateChange() == ItemEvent.SELECTED;
setText((state) ? NONE : ALL);
for (int r = 0; r < table.getRowCount(); r++) {
table.setValueAt(state, r, viewColumn);
}
}
}
@Override
public void updateUI() {
super.updateUI();
applyUI();
}
private void applyUI() {
this.setFont(UIManager.getFont("TableHeader.font"));
this.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
this.setBackground(UIManager.getColor("TableHeader.background"));
this.setForeground(UIManager.getColor("TableHeader.foreground"));
}
private class MouseHandler extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
viewColumn = header.columnAtPoint(e.getPoint());
int modelColumn = tcm.getColumn(viewColumn).getModelIndex();
if (modelColumn == targetColumn) {
doClick();
}
}
}
private class ModelHandler implements TableModelListener {
@Override
public void tableChanged(TableModelEvent e) {
if (needsToggle()) {
doClick();
header.repaint();
}
}
}
// Return true if this toggle needs to match the model.
private boolean needsToggle() {
boolean allTrue = true;
boolean allFalse = true;
for (int r = 0; r < tableModel.getRowCount(); r++) {
boolean b = (Boolean) tableModel.getValueAt(r, targetColumn);
allTrue &= b;
allFalse &= !b;
}
return allTrue && !isSelected() || allFalse && isSelected();
}
}
问题是您告诉JTable您的所有列都包含字符串(escpt最后一个),这是不对的:
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == numberOfColumns) {
return Boolean.class;
} else {
//return getValueAt(0, columnIndex).getClass();
return String.class;
}
}
把真相告诉JTable,它会很高兴的。
我从数据库中填充。中的数据根据自动生成的主键按降序排序。该表如下所示。 表中的数据由一个列表保存,该列表包含JPA实体的对象列表-
当添加到JTable(包含表单元格中不同的JCombo Box)中的排序器/autoRowSorter用列标题排序时,Jcomo Box不会用相应的行排序。 单击JCombo框可以更改它在显示组合框的模型值而不是视图值(与行相关的值)中结束的数据。 在互联网上找到的例子,用同样的方法来说明问题。我刚刚添加了setAutoCreateRowSorter(真) 代码参考: 如何在Swing中的JTab
已经存在一个jtable,我需要动态添加一列,然后为该列设置表格单元格呈现器,单元格呈现器是带有图标的jlabel。我已经做完了。 我的问题是:现在我需要根据表格单元格呈现器中使用的不同图标对列进行排序,那么如何做到这一点呢?谢谢你。
这个问题来自Geeksforgeks: 以下是链接:https://practice.geeksforgeeks.org/problems/find-all-pairs-whose-sum-is-x5808/1# Q) 给定两个大小为N的未排序数组A和大小为M的不同元素数组B,任务是从这两个数组中找出总和等于X的所有对。 我必须按升序对结果进行排序,但此处数组的数据类型是用户定义的(即pair)。
我正在用C语言开发一种象棋游戏,只是为了练习。在游戏开始时,用户可以键入4样东西: ROW
正如我的问题所描述的,如何在java中对类型数组的列表进行排序? 这是我的ListClass 这是我的驾驶程序 现在,我希望在ListClass中有一个通用排序方法,可以对通过驱动程序生成的任何类型的列表(字符串、整数、双精度、浮点等)进行排序。