2010-04-30 10 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 : 코드 서식을 향상 시키십시오. 'Blockquote (Ctrl + q) '대신'Code Sample (Ctrl + K)'를 사용하십시오. –

+0

나는 다음에 ... – firestruq

+0

내가 컴파일 오류를 가지고 있기 때문에 이번에는 .. 코드를 편집 할 수 있습니다. – bragboy

답변

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

테이블 (Init) 만 표시하면 모든 것이 괜찮아요. ..하지만 문제는 내가 Object의 각 매개 변수에 값을 설정할 때마다 시작됩니다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까? (예를 들어 새 항목을 입력하면 설명 (문자열), 유형 (열거 형) 및 가격 (유동)의 매개 변수를 입력해야합니다. 감사 – firestruq

2

, 내가 설명의 매개 변수를 입력해야 (문자열) , 유형 (열거 형) 및 가격 (변동 가능) ...

새 데이터 행을 추가하려면 DefaultTableModel의 addRow (...) 메소드를 사용해야합니다.

모델을 만들 때 사용한 업데이트가 아닌 모델을 모두 업데이트해야합니다.

관련 문제