gpt4 book ai didi

java - JXTable日期列

转载 作者:行者123 更新时间:2023-12-02 06:26:19 25 4
gpt4 key购买 nike

我如何知道该列是否已被我的 TableModel 子类覆盖。我想将表列之一设置为 Date 数据类型并按降序排序,但我不确定该列的数据类型,因为当我打印它们时,它们都会给出输出:

类 org.jdesktop.swingx.table.TableColumnExt

这是我的代码:

public class NewJFrame extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
for (int i = 0; i < jtbSchedule.getColumnCount(true); i++) {
System.out.println("column " + i + ": " + jtbSchedule.getColumn(i).getClass());
}
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane2 = new javax.swing.JScrollPane();
jtbSchedule = new org.jdesktop.swingx.JXTable(new MyTableModel());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jtbSchedule.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jtbSchedule);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(118, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane2;
private org.jdesktop.swingx.JXTable jtbSchedule;
// End of variables declaration

public class MyTableModel extends DefaultTableModel {

@Override
public Class getColumnClass(int col) {
if (col == 3) {
return java.util.Date.class;
} else {
return super.getColumnClass(col); // return the appropriate class for every column
}
}
}
}

最佳答案

关于表格模型:

  • 第一次使用 MyTableModel 实例初始化表。
  • 重写模型后两行设置新的 DefaultTableModel 实例。

看看下面的评论:

  jtbSchedule = new org.jdesktop.swingx.JXTable(new MyTableModel());// Here you set a MyTableModel instance
...
jtbSchedule.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
)); // But here you override the table model setting a DefaultTableModel instance

无论如何,这个方法:

System.out.println("column " + i + ": " + jtbSchedule.getColumn(i).getClass());

它将打印 TableColumn 的类返回者 JTable.getColumn()方法,而不是表模型的列类。应该是:

for(int i = 0; i < jtbSchedule.getModel().getColumnCount(); i++){
System.out.println("column " + i + ": " + jtbSchedule.getModel().getColumnClass(i));
}

关于java - JXTable日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498234/

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