无法显示JTable

时间:2010-04-30 13:09:30

标签: java swing jtable

我有一个小问题(我猜)显示JTable面板。 我的类包含Object数组:

public class Item 

{

  String itemDesc = "";
  float price = 0;
  private itemType enmItemType;
  Object[][] data = {{itemDesc, enmItemType , new Float(price)}};
  .
  .
  .
  .

}

这里是Table类包含JTable:

class Table extends JFrame
{
  // Instance attributes used in this example
  private   JPanel      topPanel;
  private   JTable      table;
  private   JScrollPane scrollPane;
  private JButton       update_Button;

  // Constructor of main frame
  public Table()
  {
    // Set the frame characteristics
    setTitle("Add new item" );
    setSize(300, 200);
    setBackground( Color.gray );

    // Create a panel to hold all other components
    topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create columns names
    String columnNames[] = {"Item Description", "Item Type", "Item Price"};

    // Create some data
    Object dataValues[][] ;
    Item itm = new Item();
    dataValues = itm.data;

    // Create a new table instance
    table = new JTable( dataValues, columnNames );

    ////////////////////////////

    JComboBox itemTypeCombobox = new JComboBox();
        TableColumn column1 = table.getColumnModel().getColumn(1);
    column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));

    ////////////////////////////    

    // Add the table to a scrolling pane
    scrollPane = new JScrollPane( table );
    topPanel.add( scrollPane, BorderLayout.CENTER );
    JButton button = new JButton("Add Item");
    topPanel.add( button, BorderLayout.SOUTH ); 

  }

}

主要计划是:

public static void main(String[] args)
{ 
    Menu m = new Menu();
    m.chooseMenu();

    // Create an instance of the test application
    Table mainFrame = new Table();
    mainFrame.setVisible( true );   
}

我没有收到任何错误/警告,但仍然没有看到任何表格。 有人可以指导我导致问题的原因吗?

感谢。

2 个答案:

答案 0 :(得分:3)

我不知道出了什么问题。但是我改变了你的代码(因为它有编译时错误)

对我来说很好。以下是截图

Item

public class Item{
    String itemDesc = "";
    float price = 0;
    Object[][] data = {{"test","test","test"},
            {"test","test","test"},
            {"test","test","test"},
            {"test","test","test"}};
}

您的主要课程表

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Menu;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class Table extends JFrame

{
    // Instance attributes used in this example

    private JPanel topPanel;
    private JTable table;
    private JScrollPane scrollPane;
    private JButton update_Button;

    // Constructor of main frame
    public Table() {
        // Set the frame characteristics
        setTitle("Add new item");
        setSize(300, 200);
        setBackground(Color.gray);

        // Create a panel to hold all other components
        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        // Create columns names
        String columnNames[] = { "Item Description", "Item Type", "Item Price" };

        // Create some data
        Object dataValues[][];
        Item itm = new Item();
        dataValues = itm.data;

        // Create a new table instance
        table = new JTable(dataValues, columnNames);

        // //////////////////////////

        JComboBox itemTypeCombobox = new JComboBox();
        TableColumn column1 = table.getColumnModel().getColumn(1);
        column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));

        // //////////////////////////

        // Add the table to a scrolling pane
        scrollPane = new JScrollPane(table);
        topPanel.add(scrollPane, BorderLayout.CENTER);
        JButton button = new JButton("Add Item");
        topPanel.add(button, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
        Menu m = new Menu();
        // Create an instance of the test application
        Table mainFrame = new Table();
        mainFrame.setVisible(true);
    }

}

答案 1 :(得分:2)

  

例如,如果我输入新项目,那么   我需要输入参数   描述(字符串),类型(枚举)和   价格(浮动)......

要添加新的数据行,您需要使用DefaultTableModel的addRow(...)方法。

应该对模型进行所有更新,而不是用于创建模型的数组。