问题描述
我遵循Oracle的模型来实现AbstractTableModel
I followed Oracle's model for implementing an AbstractTableModel
之所以这样做,是因为我的表必须包含3列,而第一列必须是JCheckBox.
I did this because my table has to contain 3 columns, and the first has to be a JCheckBox.
这是我的代码:
public class FestplattenreinigerGraphicalUserInterfaceHomePagePanelTableModel extends AbstractTableModel {
private String[] columnNames = {"Auswahl",
"Dateiname",
"Pfad"};
private Object[][] data = {
{new Boolean(true), "datei.tmp",
"/home/user/tmp"}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col == 0) {
return true;
} else {
return false;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
这是我的问题:
- JTable(
new JTable(FestplattenreinigerGraphicalUserInterfaceHomePagePanelTableModel)
)如何知道什么是列名及其值?由于我的AbstractTableModel中没有构造函数?是否是因为columnNames和数据必须按原样命名,并且JTable访问它们? - 如何在JTable中放入新值?由于columnNames和data是数组.我可以用向量替换它们吗?如果是这样,我如何初始化这些向量?在myAbsTableModel的构造函数中?
- How does JTable (
new JTable(FestplattenreinigerGraphicalUserInterfaceHomePagePanelTableModel)
) know what the column names are and their values are? Since there's no contructor in my AbstractTableModel?! Is it becaue columnNames and data must be named like they are and JTable accesses them? - How can i put new Values in my JTable? Since columnNames and data are arrays. Can i replace them with vectors? If so, how do I init these vectors? In a constructor in myAbsTableModel?
我认为找到解决方案非常容易,但是这种表处理对我来说并不简单,所以非常感谢您!
I think it's very easy to get a solution but this Table handling isn't trivial to me, so thank u very much!
推荐答案
所有Swing组件均带有默认模型实现.我建议您先了解如何使用它们,然后再尝试创建自己的它们.对于JTable,其称为DefaultTableModel.阅读API,了解从模型动态添加/删除数据行的方法.这是一个简单的示例,可以帮助您入门:
All Swing components come with default model implementations. I suggest you understand how to use them first before trying to create your own. For a JTable its called the DefaultTableModel. Read the API for methods to dynamically add/remove rows of data from the model. Here is a simple example to get you started:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableBasic extends JPanel
{
public TableBasic()
{
String[] columnNames = {"Date", "String", "Integer", "Boolean"};
Object[][] data =
{
{new Date(), "A", new Double(1), Boolean.TRUE },
{new Date(), "B", new Double(2), Boolean.FALSE},
{new Date(), "C", new Double(9), Boolean.TRUE },
{new Date(), "D", new Double(4), Boolean.FALSE}
};
JTable table = new JTable(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers and editors to be used based on Class
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
return o.getClass();
}
return Object.class;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("TableBasic");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableBasic() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
这篇关于Java摇摆|扩展AbstractTableModel并将其与JTable一起使用|几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!