我可以找到的每个例子都可以创建一个没有gui的表,或者解释如何从数据库中填充。我必须遗漏一些简单的东西,但这是我到目前为止基于不同教程和How to Use Tables: Creating a Table Model的内容。
private void btnAddRecordToTableActionPerformed(java.awt.event.ActionEvent evt) {
String strPledgeArray[][] =
{
{"1","2","3","4","5"}
};
String columnNames[] = {"Column 1", "Column 2", "Column 3","Column 4","Column 5"};
JTable tblDataGridResults = new JTable(strPledgeArray, columnNames);
}
按下按钮后没有任何反应。我究竟做错了什么?
从我的阅读中我认为这是因为没有模型,但每当我看到任何关于模型的东西时,它们都在不同的类中,或者使用只在程序中创建错误的构造函数。
答案 0 :(得分:2)
1)代码行
JTable tblDataGridResults = new JTable(strPledgeArray, columnNames);
创建一个未添加到GUI的新JTable
2)最好将ColumnNames和Rows添加到DefaultTableModel,请阅读教程how TableModel works
修改
仍然不明白???
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.*;
public class RemoveAddRows extends JFrame {
private static final long serialVersionUID = 1L;
private Object[] columnNames = {"Type", "Company", "Shares", "Price"};
private 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)}
};
private JTable table;
private DefaultTableModel model;
public RemoveAddRows() {
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;
int width = 0;
if (row == lastRow) {
((JComponent) c).setBackground(Color.red);
} else if (row == firstRow) {
((JComponent) c).setBackground(Color.blue);
} else {
((JComponent) c).setBackground(table.getBackground());
}
/*if (!isRowSelected(row)) {
String type = (String) getModel().getValueAt(row, 0);
c.setBackground("Buy".equals(type) ? Color.GREEN : Color.YELLOW);
}
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.red));
}*/
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
JButton button1 = new JButton("Remove all rows");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (model.getRowCount() > 0) {
for (int i = model.getRowCount() - 1; i > -1; i--) {
model.removeRow(i);
}
}
System.out.println("model.getRowCount() --->" + model.getRowCount());
}
});
JButton button2 = new JButton("Add new rows");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)};
model.addRow(data0);
Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)};
model.addRow(data1);
Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)};
model.addRow(data2);
Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)};
model.addRow(data3);
System.out.println("model.getRowCount() --->" + model.getRowCount());
}
});
JPanel southPanel = new JPanel();
southPanel.add(button1);
southPanel.add(button2);
add(southPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
RemoveAddRows frame = new RemoveAddRows();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
答案 1 :(得分:0)
试试这个,
使用Object [] []数组而不是String [] []
JTree构造函数接受2维对象数组作为表的内容,并使用1维字符串数组...然后创建表。
例如:
import java.awt.BorderLayout
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test6 extends JFrame {
public Test6(){
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mpanel m = new Mpanel();
this.add(m,BorderLayout.CENTER);
}
class Mpanel extends JPanel {
JTable mTable;
private Object[][] cells = {{"Vivek",10.00},{"Vishal",20.00}};
private String[] columnNames = { "Planet", "Radius" };
JScrollPane mScroll;
public Mpanel(){
this.setSize(150,150);
this.setComponent();
}
public void setComponent(){
mTable = new JTable(cells,columnNames);
mTable.setAutoCreateRowSorter(true);
mScroll = new JScrollPane(mTable);
this.add(mScroll);
}
}
public static void main(String[] args){
new Test6().setVisible(true);
}
}