无法显示 JTable

发布于 2024-08-31 01:38:59 字数 1988 浏览 2 评论 0原文

我在显示 JTable 面板时遇到了一个小问题(我猜)。 我的类包含对象数组:

public class Item 

{

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

}

这里是包含 JTable 的 Table 类:

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 );   
}

我没有收到任何错误/警告,但我仍然没有看到任何表。 有人可以告诉我是什么原因导致了这个问题吗?

谢谢。

I'm having small problem (I guess) of showing the JTable panel.
I have class contains Object array with:

public class Item 

{

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

}

here is the Table class contains the 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 ); 

  }

}

The main program is:

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 );   
}

I did not receive any error/warning, but still, I don't see any table.
Can someone direct me what causing the problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-09-07 01:38:59

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

它对我来说工作得很好。以下是屏幕截图

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);
    }

}

I cant tell what has gone wrong. But I changed your code a bit (since it had compile time errors)

It works fine for me. Following is the screenshot

Item

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

Your main table class

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);
    }

}
呢古 2024-09-07 01:38:59

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

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

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

for example if I enter new item, then
I need to enter parameters of
Description (String), type (Enum) and
price (Float)...

To add a new row of data you need to use the addRow(...) method of the DefaultTableModel.

All updates should be done to the model, not the Array used to create the model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文