2012-06-29 1 views
-1

내 애플릿의 확인란이나 콤보 상자를 표시 할 때 사용하는 JTable을 가져 오는 데 문제가 있습니다. 여기 JTable이 Java Applet의 테이블에 JCheckBox 또는 JComboBox를 렌더링하지 않습니다.

는 defaultTableModel2 단순히 DefaultTableModel를 defaultTabelModel2 = 새의 DefaultTableModel()이 너무 극적인 그래서 아무것도 제대로

String[] options = {"download", "ignore"}; 
Object[] obj = {new JComboBox(options), ((MetadataList)array.get(1)).getMetadata("Filename").getValue()}; 

defaultTableModel2.addRow(obj); 

를 작동하지 않는 코드입니다. 위의 코드는 JComboBox를 사용하고 있지만 JCheckBox도 사용하여 동일한 문제가 발생했습니다.

javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicComboBoxUI$Handler,alignmentX=0.0,alignmentY=0.0,[email protected],flags=320,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=download] 

대신에 어떤 도움을 크게 감상 할 수 JComboBox를

이 나타납니다 것입니다! 예를 들어

+0

당신은 테이블 모델에 구성 요소를 추가하지 마십시오 - 당신은 * 체크 박스 *과 같은 사용자 정의 셀 렌더러를 사용할 필요가있다. –

+0

그 코드는 JTable의 작동 방식에있어 기본입니다. JTable이 보유한 데이터와 그 렌더링 사이에는 큰 차이가 있습니다. JTable 셀 렌더링 및 셀 편집에 대한 자습서 섹션을 읽어야합니다. 당신은이 물건을 추측 할 수 없으며 그것이 작동하기를 기대할 수 없습니다. 이것이 바로 튜토리얼의 목적입니다. 체크 박스의 경우, 당신이해야 할 일은 부울의 컬럼을 가지며'getColumnClass()'는'Boolean.class'를 반환해야합니다. 콤보 상자의 경우 렌더링 및 편집 작업을 더 많이 읽어야 작동합니다. –

+2

다음은 [JTable 자습서 링크] (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)입니다. 행운을 빕니다. –

답변

3

:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.table.*; 

@SuppressWarnings("serial") 
public class TableJunk extends JPanel { 
    enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY} 
    MyTableModel tableModel = new MyTableModel(); 
    private JTable myTable = new JTable(tableModel); 

    public TableJunk() { 
     setLayout(new BorderLayout()); 
     add(new JScrollPane(myTable)); 

     Object[] rowData = {Day.MONDAY, Boolean.TRUE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.TUESDAY, Boolean.TRUE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.WEDNESDAY, Boolean.TRUE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.THURSDAY, Boolean.TRUE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.FRIDAY, Boolean.TRUE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.SATURDAY, Boolean.FALSE}; 
     tableModel.addRow(rowData); 
     rowData = new Object[]{Day.SUNDAY, Boolean.FALSE}; 
     tableModel.addRow(rowData); 

     // create the combo box for the column editor   
     JComboBox<Day> comboBox = new JComboBox<TableJunk.Day>(Day.values()); 
     myTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox)); 
    } 

    private static void createAndShowGui() { 
     TableJunk mainPanel = new TableJunk(); 

     JFrame frame = new JFrame("TableJunk"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 


    private static class MyTableModel extends DefaultTableModel { 
     private static final String[] COLUMN_NAMES = {"Day", "Work Day"}; 

     public MyTableModel() { 
     super(COLUMN_NAMES, 0); 
     } 

     // this method will allow the check box to be rendered in the 2nd column 
     public java.lang.Class<?> getColumnClass(int columnIndex) { 
     if (columnIndex == 0) { 
      return Day.class; 
     } else if (columnIndex == 1) { 
      return Boolean.class; 
     } else { 
      return super.getColumnClass(columnIndex); 
     } 
     }; 
    } 
} 
관련 문제