无法显示 JTable
Posted
技术标签:
【中文标题】无法显示 JTable【英文标题】:can't show JTable 【发布时间】:2013-06-11 08:53:55 【问题描述】:我正在使用来自互联网的代码来创建 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
向量包含列名称。
这是主要代码:
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);
谢谢
【问题讨论】:
1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2)class EvolImportations extends javax.swing.JInternalFrame
为什么哦为什么人们扩展组件?我看到很少有这样做的充分理由。你的理由是什么? 3) To change this template, choose..
并将这些垃圾从 SSCCE 中删除。 4) “我正在使用 netbeans 生成 IHM” 什么是 IHM?
可能无法回答,为了获得更好的帮助,请尽快发布SSCCE,简短、可运行、可编译,然后您可能会发现您的帖子中未介绍的其他类的问题
我在帖子中添加了一些信息,编译代码时没有错误。只是数据没有出现在jtable中
【参考方案1】:
最初的问题是您尝试修改不应该修改的代码并弄乱了顺序。
您在创建之前将jTable1
添加到滚动窗格...
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);
【讨论】:
【参考方案2】:/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jtable;
/**
*
* @author shreyansh.jogi
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
/**
* @author John B. Matthews; distribution per LGPL
* 11-Jul-2011 Correct item listener in ValueEditor
*/
public class Jtable extends JPanel
private static DecimalFormat df = new DecimalFormat("#0.00");
private DataModel model = new DataModel();
private JTable table = new JTable(model);
public Jtable()
this.setLayout(new BorderLayout());
table.setDefaultRenderer(Value.class, new ValueRenderer());
table.setDefaultEditor(Value.class, new ValueEditor());
table.setAutoCreateRowSorter(true);
table.setRowHeight(24);
this.add(table.getTableHeader(), BorderLayout.NORTH);
this.add(table, BorderLayout.CENTER);
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
@Override
public void run()
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Jtable());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
);
private static class Value implements Comparable<Value>
private Boolean selected;
private Double value;
public Value(Boolean selected, Double value)
this.selected = selected;
this.value = value;
@Override
public int compareTo(Value v)
return this.value.compareTo(v.value);
@Override
public boolean equals(Object v)
return v instanceof Value && this.value.equals(((Value) v).value);
@Override
public int hashCode()
return this.value.hashCode();
private static class DataModel extends AbstractTableModel
private static final int MAX = 8;
private static final String[] names = "Number", "Value";
private List<Value> values = new ArrayList<Value>();
public DataModel()
for (int i = 0; i < MAX; i++)
values.add(new Value(false, (i + 1) * 1.1));
@Override
public int getRowCount()
return MAX;
@Override
public int getColumnCount()
return names.length;
@Override
public Object getValueAt(int row, int col)
if (col == 0)
return row + 1;
else if (col == 1)
return values.get(row);
else
return null;
@Override
public void setValueAt(Object aValue, int row, int col)
if (col == 1)
values.get(row).selected = (Boolean) aValue;
this.fireTableCellUpdated(row, col);
@Override
public Class<?> getColumnClass(int col)
if (col == 0)
return Integer.class;
else if (col == 1)
return Value.class;
else
return null;
@Override
public String getColumnName(int col)
return names[col];
@Override
public boolean isCellEditable(int row, int col)
return col == 1;
private static class ValueRenderer extends JCheckBox
implements TableCellRenderer
private static final Color hilite = new Color(0xE8E8E8);
public ValueRenderer()
this.setOpaque(true);
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col)
Value v = (Value) value;
this.setSelected(v.selected);
this.setText(df.format(v.value));
if (isSelected)
this.setBackground(hilite);
else
this.setBackground(Color.white);
return this;
private class ValueEditor extends AbstractCellEditor
implements TableCellEditor, ItemListener
private ValueRenderer vr = new ValueRenderer();
public ValueEditor()
vr.addItemListener(this);
@Override
public Object getCellEditorValue()
return vr.isSelected();
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int col)
Value v = (Value) value;
vr.setSelected(v.selected);
vr.setText(df.format(v.value));
return vr;
@Override
public void itemStateChanged(ItemEvent e)
this.fireEditingStopped();
【讨论】:
以上是关于无法显示 JTable的主要内容,如果未能解决你的问题,请参考以下文章