gpt4 book ai didi

java - 无法显示 JTable

转载 作者:行者123 更新时间:2023-12-01 14:27:54 31 4
gpt4 key购买 nike

我正在使用互联网上的代码来创建 Groupable TableHeader,我遵循了所有步骤,但我不知道为什么我的 Jtable 没有显示!!

我正在使用 netbeans 生成 IHM,任何人都可以帮我找出 Jtable 不显示的原因吗?

注意:IHM 的 OnLoad,Jtable 应该出现

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package GUI;

import Control.ColumnGroup;
import Control.GroupableTableHeader;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

/**
*
* @author Archavine
*/
public class EvolImportations extends javax.swing.JInternalFrame {

DefaultTableModel dm = null;
int thisyear = 0;
Vector column = new Vector();

/** Creates new form EvolImportations */
public EvolImportations() {
initComponents();
}

/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();

setClosable(true);
setIconifiable(true);
setTitle("Evolution des importations par principaux produits");


column.addElement("");
column.addElement("");
column.addElement(thisyear);
column.addElement(thisyear - 1);
column.addElement("Valeur");
column.addElement("%");


Calendar now = Calendar.getInstance(); // This gets the current date and time.
thisyear = now.get(Calendar.YEAR); // This returns the year as an int.

dm = new DefaultTableModel();
dm.setDataVector(new DAO.EvolutionImportation().EvolImport(1, 2), column);

System.err.println("Nombre de lignes : "+dm.getRowCount());

jScrollPane1.setViewportView(jTable1);


jTable1 = new JTable(dm) {

protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};

System.err.println("Nombre de lignes Jtable : "+jTable1.getRowCount());

TableColumnModel cm = jTable1.getColumnModel();
ColumnGroup g_name = new ColumnGroup("Periode");
g_name.add(cm.getColumn(1));
g_name.add(cm.getColumn(2));
ColumnGroup g_lang = new ColumnGroup("Evolution");
g_lang.add(cm.getColumn(3));
g_lang.add(cm.getColumn(4));

GroupableTableHeader header = (GroupableTableHeader) jTable1.getTableHeader();
header.addColumnGroup(g_name);
header.addColumnGroup(g_lang);

jTable1.setRowHeight(30);


jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel1.setText("MOIS");

jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 12));
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Egale à", "Entre"}));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});

jTextField1.setFont(new java.awt.Font("Tahoma", 0, 12));

jButton1.setFont(new java.awt.Font("Tahoma", 0, 12));
jButton1.setText("Valider");
jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField2.setFont(new java.awt.Font("Tahoma", 0, 12));

jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel2.setText("&");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1))).addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));

pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String mois1 = jTextField1.getText();
String mois2 = jTextField2.getText();
if (jComboBox1.getSelectedItem().equals("Egale à")) {
dm.setDataVector(new DAO.EvolutionImportation().EvolImport(Integer.parseInt(mois1), Integer.parseInt(mois1)), column);
System.err.println("egale");
} else if (jComboBox1.getSelectedItem().equals("Entre")) {
dm.setDataVector(new DAO.EvolutionImportation().EvolImport(Integer.parseInt(mois1), Integer.parseInt(mois2)), column);
System.err.println("Entre");
}
}

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}

总而言之,整个是由netbeans(Jtable,Jtextbox,...以及布局)生成的,在构造函数中,我调用一个方法,该方法返回带有来自数据库的数据的Vector DAO.EvolutionImportation( ).EvolImport(1, 2)column vector 包含列名称。

这是主要代码:

dm = new DefaultTableModel();
dm.setDataVector(new DAO.EvolutionImportation().EvolImport(1, 2), column);

System.err.println("Nombre de lignes : "+dm.getRowCount());

jScrollPane1.setViewportView(jTable1);


jTable1 = new JTable(dm) {

protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};

System.err.println("Nombre de lignes Jtable : "+jTable1.getRowCount());

TableColumnModel cm = jTable1.getColumnModel();
ColumnGroup g_name = new ColumnGroup("Periode");
g_name.add(cm.getColumn(1));
g_name.add(cm.getColumn(2));
ColumnGroup g_lang = new ColumnGroup("Evolution");
g_lang.add(cm.getColumn(3));
g_lang.add(cm.getColumn(4));

GroupableTableHeader header = (GroupableTableHeader) jTable1.getTableHeader();
header.addColumnGroup(g_name);
header.addColumnGroup(g_lang);

jTable1.setRowHeight(30);

当我运行我的示例时,它显示了 JinternalFrame 和 jtable 边框,但没有数据!!

解决方案是:

Vector column = new Vector();

column.addElement("");
column.addElement("2013");
column.addElement("2012");
column.addElement("Valeur");
column.addElement("%");

DefaultTableModel dm = null;
int thisyear = 0;

String mois1 = jTextField1.getText();
String mois2 = jTextField2.getText();
Vector v = new DAO.EvolutionImportation().EvolImport(Integer.parseInt(mois1), Integer.parseInt(mois2));

dm = new DefaultTableModel();
dm.setDataVector(v, column);
jTable1.setModel(dm);

jTable1 = new JTable(dm) {

protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};

TableColumnModel cm = jTable1.getColumnModel();
ColumnGroup g_name = new ColumnGroup("Periode");
g_name.add(cm.getColumn(1));
g_name.add(cm.getColumn(2));
ColumnGroup g_lang = new ColumnGroup("Evolution");
g_lang.add(cm.getColumn(3));
g_lang.add(cm.getColumn(4));

GroupableTableHeader header = (GroupableTableHeader) jTable1.getTableHeader();
header.addColumnGroup(g_name);
header.addColumnGroup(g_lang);

jTable1.setRowHeight(30);
jScrollPane1.setViewportView(jTable1);

谢谢

最佳答案

最初的问题是您尝试修改不应该修改的代码并打乱了顺序。

您在创建jTable1 之前将其添加到滚动 Pane 中...

jScrollPane1.setViewportView(jTable1);

jTable1 = new JTable(dm) {
protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};

尝试切换语句...

jTable1 = new JTable(dm) {
protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};

jScrollPane1.setViewportView(jTable1);

关于java - 无法显示 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040059/

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