英文:
jTable boolean column shows true/false instead of checkboxes
问题
我正在创建一个NetBeans Java项目,该项目从Microsoft Access数据库中检索数据。我需要显示一个可编辑的复选框以代表其中一列,但出现了true/false的情况。在Design视图中,jTable显示复选框,但当我运行程序时,显示为true/false。我真的不理解编辑器和渲染器,尽管我已经尝试过,但如果没有这个问题的解决方法,最好。如果这是唯一的方法,请解释如何做到这一点。
这是我的GUI代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timetable;
/**
*
* @author Lenovo
*/
public class TaskFrame extends javax.swing.JFrame {
/**
* Creates new form Main
*/
public TaskFrame() {
initComponents();
this.setLocationRelativeTo(null);
taskList t = new taskList()
jtblTodayTasks.setModel(t.filljtblTodayTasks());
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jtblTodayTasks = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setMinimumSize(new java.awt.Dimension(1100, 619));
setResizable(false);
setSize(new java.awt.Dimension(1100, 619));
getContentPane().setLayout(null);
jtblTodayTasks.setFont(new java.awt.Font("Roboto", 0, 14)); // NOI18N
jtblTodayTasks.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String [] {
"ID", "Task", "Subject", "Due Date", "Completed"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jtblTodayTasks);
if (jtblTodayTasks.getColumnModel().getColumnCount() > 0) {
jtblTodayTasks.getColumnModel().getColumn(4).setPreferredWidth(0);
}
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(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TaskFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jtblTodayTasks;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration
}
这是我的taskList类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timetable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Lenovo
*/
public class taskList {
ArrayList<Task> taskArray = new ArrayList<>();
public taskList(){
ConnectDB db = new ConnectDB();
ResultSet rs = db.getResults("SELECT * FROM tblTasks WHERE userID = " + Login.currentUserID + " ORDER BY dueDate;");
try{
while (rs.next()){
Task task = new Task(rs.getInt("taskID"), rs.getInt("userID"), rs.getString("taskName"), rs.getString("Subject"), rs.getString("taskDetails"), rs.getString("dueDate"), rs.getBoolean("Completed"));
taskArray.add(task);
}
rs.close();
}catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Database error. Please contact the system Administrator");
}
}
public DefaultTableModel filljtblTodayTasks(){
Object[] columnNames = {"ID", "Task", "Subject", "Due Date", "Completed"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (int i = 0; i < taskArray.size(); i++) {
model.addRow(new Object[]{taskArray.get(i).getTaskID(), taskArray.get(i).getTaskName(), taskArray.get(i).getSubject(), taskArray.get(i).getDueDate(), taskArray.get(i).isCompleted()});
}
return model;
}
}
谢谢!
英文:
I am creating a netbeans java project that retrieves data from a microsoft access database.
I need to display an editable checkbox for one of my columns but for some reason it comes up as true/false instead. The jTable in the Design view shows checkboxes but when I run the program true/false is displayed.
I really don't understand editors and renders though I have tried, a solution without this would be preferable but if it is the only way, please explain how to do it.
Here is my GUI code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timetable;
/**
*
* @author Lenovo
*/
public class TaskFrame extends javax.swing.JFrame {
/**
* Creates new form Main
*/
public TaskFrame() {
initComponents();
this.setLocationRelativeTo(null);
taskList t = new taskList()
jtblTodayTasks.setModel(t.filljtblTodayTasks());
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jtblTodayTasks = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setMinimumSize(new java.awt.Dimension(1100, 619));
setResizable(false);
setSize(new java.awt.Dimension(1100, 619));
getContentPane().setLayout(null);
jtblTodayTasks.setFont(new java.awt.Font("Roboto", 0, 14)); // NOI18N
jtblTodayTasks.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String [] {
"ID", "Task", "Subject", "Due Date", "Completed"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jtblTodayTasks);
if (jtblTodayTasks.getColumnModel().getColumnCount() > 0) {
jtblTodayTasks.getColumnModel().getColumn(4).setPreferredWidth(0);
}
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(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TaskFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TaskFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jtblTodayTasks;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration
}
Here is my taskList class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package timetable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Lenovo
*/
public class taskList {
ArrayList<Task> taskArray = new ArrayList<>();
public taskList(){
ConnectDB db = new ConnectDB();
ResultSet rs = db.getResults("SELECT * FROM tblTasks WHERE userID = " + Login.currentUserID + " ORDER BY dueDate;");
try{
while (rs.next()){
Task task = new Task(rs.getInt("taskID"), rs.getInt("userID"), rs.getString("taskName"), rs.getString("Subject"), rs.getString("taskDetails"), rs.getString("dueDate"), rs.getBoolean("Completed"));
taskArray.add(task);
}
rs.close();
}catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Database error. Please contact the system Administrator");
}
}
public DefaultTableModel filljtblTodayTasks(){
Object[] columnNames = {"ID", "Task", "Subject", "Due Date", "Completed"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (int i = 0; i < taskArray.size(); i++) {
model.addRow(new Object[]{taskArray.get(i).getTaskID(), taskArray.get(i).getTaskName(), taskArray.get(i).getSubject(), taskArray.get(i).getDueDate(), taskArray.get(i).isCompleted()});
}
return model;
}
Thank you so much!
答案1
得分: 2
-
"The jTable in the Design view shows checkboxes but when I run the program true/false is displayed."
- "在设计视图中,jTable 显示复选框,但运行程序时显示 true/false。"
-
"The
getColumnClass(...)
method determines which renderer is used for each column."- "
getColumnClass(...)
方法确定每列使用哪个渲染器。"
- "
-
"The code in your TaskFrame already creates a custom model and overrides the
getColumnClass(...)
method."- "您的 TaskFrame 中的代码已经创建了自定义模型并重写了
getColumnClass(...)
方法。"
- "您的 TaskFrame 中的代码已经创建了自定义模型并重写了
-
"But then you create another table model but are not overriding the
getColumnClass(...)
method:"- "但然后您创建了另一个表模型,但没有重写
getColumnClass(...)
方法:"
- "但然后您创建了另一个表模型,但没有重写
-
"Object[] columnNames = {"ID", "Task", "Subject", "Due Date", "Completed"};"
- "Object[] columnNames = {"ID", "Task", "Subject", "Due Date", "Completed"};"
-
"DefaultTableModel model = new DefaultTableModel(columnNames, 0);"
- "DefaultTableModel model = new DefaultTableModel(columnNames, 0);"
-
"Don't create a new DefaultTableModel."
- "不要创建新的 DefaultTableModel。"
-
"Instead you can just use:"
- "相反,您可以只使用:"
-
"DefaultTableModel model = (DefaultTableModel)jtblTodayTasks.getModel();"
- "DefaultTableModel model = (DefaultTableModel)jtblTodayTasks.getModel();"
-
"model.setRowCount( 0 );"
- "model.setRowCount(0);"
-
"The above code will remove the data from the existing model, so you can add new data."
- "上述代码将从现有模型中删除数据,以便您可以添加新数据。"
英文:
> The jTable in the Design view shows checkboxes but when I run the program true/false is displayed.
The getColumnClass(...)
method determines which renderer is used for each column
The code in your TaskFrame already creates a custom model and overrides the getColumnClass(...)
method.
But then you create another table model but are not overriding the getColumnClass(...)
method:
Object[] columnNames = {"ID", "Task", "Subject", "Due Date", "Completed"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
Don't create a new DefaultTableModel.
Instead you can just use:
DefaultTableModel model = (DefaultTableModel)jtblTodayTasks.getModel();
model.setRowCount( 0 );
The above code will remove the data from the existing model, so you can add new data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论