2010-04-30 97 views
3

我有顯示JTable面板的小問題(我猜)。 我有類包含對象數組:不能顯示JTable

public class Item 

{ 

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

} 

這裏是表類包含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); 
} 

我沒有收到任何錯誤/警告,但仍然沒有看到任何表格。 有人可以指導我導致問題的原因是什麼?

謝謝。

+0

@Edan:請嘗試改進代碼的格式。使用'代碼示例(Ctrl + K)'代替'Blockquote(Ctrl + q)'。 – 2010-04-30 13:12:56

+0

我會在下一次... – firestruq 2010-04-30 13:18:51

+0

我想這一次,因爲我有編譯錯誤。你仍然可以編輯代碼.. – bragboy 2010-04-30 13:21:25

回答

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

} 
+0

如果我只顯示錶(初始化),所以一切都好..但是,每當我爲Object的每個參數設置值時,問題就會開始。 你可能有一個想法如何解決這個問題? (我想,以示與程序本身期間設定參數表 - 例如,如果我輸入新的項目,然後我需要輸入說明的參數(字符串),類型(枚舉)和價格(浮動)... 謝謝 – firestruq 2010-04-30 14:11:37

2

例如,如果我輸入新的項目,然後 我需要輸入的 描述參數(字符串) ,類型(Enum)和 價格(Float)...

要添加一行新的數據,您需要使用DefaultTableModel的addRow(...)方法。

所有更新應該做的模式,而不是用於創建模型的數組。