gpt4 book ai didi

java - DefaultTableModel 有零列

转载 作者:行者123 更新时间:2023-11-29 05:20:12 25 4
gpt4 key购买 nike

更新了问题,具体问题是数据没有显示,因为没有列:

run:
[java] Aug 04, 2014 8:17:00 PM net.bounceme.dur.client.gui.ApplicationDriver <init>
[java] INFO: starting log..
[java] Aug 04, 2014 8:17:01 PM net.bounceme.dur.client.gui.GraphUserInterface setData
[java] INFO: [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
[java] Aug 04, 2014 8:17:01 PM net.bounceme.dur.client.gui.GraphUserInterface <init>
[java] INFO: rows 7
[java] columns 0

BUILD SUCCESSFUL
Total time: 6 seconds
thufir@dur:~/NetBeansProjects/table$

所以我只是没有正确填充表格模型和表格:

package net.bounceme.dur.client.gui;

import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

public final class GraphUserInterface extends javax.swing.JFrame {

private static final Logger log = Logger.getLogger(GraphUserInterface.class.getName());
private DefaultTableModel defaultTableModel = new DefaultTableModel();
private int columns = 3;
private int rows = 7;

private Vector setData() {
Vector dataVector = new Vector();
Vector r = null;
for (int i = 0; i < rows; i++) {
r = new Vector();
for (int j = 0; j < columns; j++) {
r.add(j);
}
dataVector.add(r);
}
log.info(dataVector.toString());
return dataVector;
}

private Vector setColumns() {
Vector v = new Vector();
for (int i = 0; i < columns; i++) {
}
return v;
}

public GraphUserInterface() {
initComponents();
defaultTableModel.setDataVector(setData(), setColumns());
defaultTableModel.setColumnIdentifiers(setColumns());
table.setModel(defaultTableModel);
defaultTableModel.fireTableDataChanged();
defaultTableModel.fireTableStructureChanged();
log.info("rows\t" + table.getRowCount()+ "\ncolumns\t"+ table.getColumnCount());
}

/**
* 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() {

buttonGroup = new javax.swing.ButtonGroup();
mainPane = new javax.swing.JTabbedPane();
update = new javax.swing.JPanel();
tablePane = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
email = new javax.swing.JTextField();
addRemove = new javax.swing.JToggleButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

table.setAutoCreateRowSorter(true);
table.setModel(defaultTableModel);
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
tablePane.setViewportView(table);

email.setText("foo@bar.com");
email.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
emailActionPerformed(evt);
}
});

addRemove.setText("toggle");

javax.swing.GroupLayout updateLayout = new javax.swing.GroupLayout(update);
update.setLayout(updateLayout);
updateLayout.setHorizontalGroup(
updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(updateLayout.createSequentialGroup()
.addContainerGap()
.addGroup(updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(updateLayout.createSequentialGroup()
.addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, Short.MAX_VALUE)
.addComponent(addRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(tablePane))
.addContainerGap())
);
updateLayout.setVerticalGroup(
updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, updateLayout.createSequentialGroup()
.addContainerGap()
.addGroup(updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(tablePane, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
.addContainerGap())
);

mainPane.addTab("update", update);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(mainPane, javax.swing.GroupLayout.PREFERRED_SIZE, 803, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(30, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainPane)
.addContainerGap())
);

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

private void emailActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

// Variables declaration - do not modify
private javax.swing.JToggleButton addRemove;
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JTextField email;
private javax.swing.JTabbedPane mainPane;
private javax.swing.JTable table;
private javax.swing.JScrollPane tablePane;
private javax.swing.JPanel update;
// End of variables declaration

}

大概。

最佳答案

在您的 setColumns 方法中,您实际上并没有添加任何东西...

private Vector setColumns() {
Vector v = new Vector();
for (int i = 0; i < columns; i++) {
// Empty loop here...?
}
return v;
}

尝试向表示实际列名称的 Vector 添加一些内容

private Vector setColumns() {
Vector v = new Vector();
for (int i = 0; i < columns; i++) {
v.add(Integer.toString(i));
}
return v;
}

Table with columns

关于java - DefaultTableModel 有零列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25130608/

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