簡體   English   中英

JTable無法在Java Applet的表中呈現JCheckBox或JComboBox

[英]JTable not rendering JCheckBox or JComboBox in the table in Java Applet

我在獲取用於顯示小程序中的復選框或組合框的JTable時遇到了麻煩。

這是無法正常工作的代碼

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

 defaultTableModel2.addRow(obj);

defaultTableModel2只是一個DefaultTableModel defaultTabelModel2 = new DefaultTableModel(),所以那里沒有什么太大的戲劇性。 上面的代碼使用的是JComboBox,但我也嘗試過使用JCheckBox,並且存在相同的問題。

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

出現而不是JComboBox

任何幫助將不勝感激!

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM