gpt4 book ai didi

java - 默认表模型为空?

转载 作者:行者123 更新时间:2023-12-02 00:24:43 25 4
gpt4 key购买 nike

我正在读取一个文件,并希望将其输出到 jTable 以便查看和编辑。当我尝试向 DefaultTableModel 添加行时,由于某种原因,模型始终为空。任何帮助/指导将不胜感激。谢谢!

public class ReadFileGUI extends javax.swing.JFrame {

public static String file;
private DefaultTableModel model = new DefaultTableModel();

public ReadFileGUI() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(model);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


jButton1.setText("Test");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

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


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String line = "Hello~There~This~Is~A~Test";
String datavalue[] = line.split("~");
Vector v = new Vector(Arrays.asList(datavalue));

model.addRow(v);
jTable1.tableChanged(new TableModelEvent(model));
}

public static void main(String args[]) {

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(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new ReadFileGUI().setVisible(true);
}
});
}

private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;

}

最佳答案

这里是您的代码的更新版本,似乎可以工作

import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Vector;

public class ReadFileGUI extends javax.swing.JFrame {
private DefaultTableModel tableModel = new DefaultTableModel();
private javax.swing.JButton populateTableButton;
private javax.swing.JScrollPane tableScrollPane;
private javax.swing.JTable table;

public ReadFileGUI() {
initComponents();
setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE);
}

@SuppressWarnings("unchecked")
private void initComponents() {
populateTableButton = new javax.swing.JButton();
tableScrollPane = new javax.swing.JScrollPane();
table = new javax.swing.JTable( tableModel );

populateTableButton.setText( "Test" );
populateTableButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent evt ) {
populateTable();
}
} );

tableScrollPane.setViewportView( table );

getContentPane().setLayout( new BorderLayout( ) );
getContentPane().add( tableScrollPane, BorderLayout.CENTER );
getContentPane().add( populateTableButton, BorderLayout.SOUTH );
pack();
}


private void populateTable( ) {
String line = "Hello~There~This~Is~A~Test";
String dataValue[] = line.split("~");
Vector<String> v = new Vector<>( Arrays.asList( dataValue ));
tableModel.setColumnCount( v.size() );
tableModel.addRow( v );
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ReadFileGUI().setVisible(true);
}
});
}
}

我改变的事情:

  • 使用导入而不是全名以避免水平滚动条
  • 使用 BorderLayout iso 生成的布局来缩短代码
  • 重命名了一些变量以提高代码的可读性
  • 删除了 jTable1.tableChanged(new TableModelEvent(model)); 调用,因为 DefaultTableModel 将在您使用 addRow 时触发相应的事件> 方法。无需再次在表中表明此更改

但相关的更改是 tableModel.setColumnCount( v.size() ); 调用。 DefaultTableModel 的默认构造函数创建一个具有零列和行的模型。如果先设置列数,可以使用addRow方法

关于java - 默认表模型为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249718/

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