包含所有组件的JTable

时间:2012-05-28 06:52:40

标签: java swing jtable

这是我的JTable代码,我打算用它来拥有ComboBox,JSpinner,JRadioButton,JTextfeiled等所有组件

但我最终得到了这个,表头没有出现,我无法编辑列。请指出我的错误,因为我经常尝试并放弃了

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.table.*;

public class TableComponent extends JFrame
{   
    JTable dataTable = null;
    public int changeRow = -1, changeColumn = -1;
    public JRadioButton radioButton = new JRadioButton();

    public void init() {
        JPanel upperPanel = setMainPanel();

        super.getContentPane().removeAll();
        Container content = super.getContentPane();
        content.setLayout(new BorderLayout());
        content.add(upperPanel, BorderLayout.CENTER);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(800, 400);
        setVisible(true);
    }

    public static void main(String[] args) {
        TableComponent tableComponent = new TableComponent();
        tableComponent.init();
    }

    // This function set the main Panel
    private JPanel setMainPanel() {
        dataTable = createTable();

        dataTable = setTableProp(dataTable);

        JPanel panel = new JPanel(new BorderLayout(5, 10));

        //Get the screen size
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        panel.add(dataTable, BorderLayout.CENTER);

        return panel;
    }

    private JTable createTable()
    {
        String[] columnName = {"", "Name", "Radio", "Text", "Combo", "Spiner"};
        String []s={"01:1mS","02:2mS","04:4mS","06:6mS"}; 
        Object[][] data = new Object[5][6];
        for (int i = 0; i < 5; i++) {
            data[i][0] = new Boolean(false);
            data[i][1] = "Column 1";
            data[i][2] = new CustomRadio(false);
            data[i][3] = new CustomTextField("Test");
            data[i][4] = new CustomCombo(s);
            data[i][5] = new CustomSpiner();
        }

        AbstractTableModel model = new MyTableModel(data, columnName);
        JTable table = new JTable(model);

        return table;
    }

    // This function set the child table properties
    private JTable setTableProp(JTable table)
    {
        JTableHeader tableHeader = table.getTableHeader();
        tableHeader.setBackground(Color.WHITE);
        tableHeader.setForeground(Color.gray);

        table.setTableHeader(tableHeader);

        TableColumn tc = null;

        for (int j = 1; j < table.getColumnCount(); j++) {
            tc = table.getColumnModel().getColumn(j);

            if (j == 1) {
                tc.setCellRenderer(new ColumnRenderer());
            } else if (j == 2) {
                tc.setCellRenderer(new RadioButtonRenderer());
            } else if (j == 3) {
                tc.setCellRenderer(new TextFieldRenderer());
            } else if (j == 4) {
                tc.setCellRenderer(new ComboRenderer());
            } else if (j == 5) {
                tc.setCellRenderer(new SpinerRenderer());
            }
        }
        return table;
    }

    //This is the Abstract Table Model class
    class MyTableModel extends AbstractTableModel {

        private String[] columnName = null;
        private Object[][] data = null;

        MyTableModel(Object[][] data, String[] columnName) {
            this.data = data;
            this.columnName = columnName;
        }

        public int getColumnCount() {
            return columnName.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnName[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the first column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
        //    if (col == 1)
                return true;
    //        else
    //          return false;
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
    }

    class ColumnRenderer extends DefaultTableCellRenderer {
            public ColumnRenderer() {
            super();
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            return cell;
        }
    }

    class RadioButtonRenderer implements TableCellRenderer, ItemListener {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value == null) {
                return null;
            }

            CustomRadio radio = (CustomRadio) value;

            return radio;
        }

        public void itemStateChanged(ItemEvent e) {}
    }

    class TextFieldRenderer implements TableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                if (value == null) {
                    return null;
                }

                CustomTextField customTextField = (CustomTextField) value;

                return customTextField;
            }
    }

    class ComboRenderer implements TableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value == null) {
                return null;
            }

            CustomCombo customCombo = (CustomCombo) value;

            return customCombo;
        }
    }

    class SpinerRenderer implements TableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value == null) {
                return null;
            }

            CustomSpiner customSpiner = (CustomSpiner) value;

            return customSpiner;
        }
    }

    class CustomRadio extends JRadioButton {
        public CustomRadio(boolean isSel) {
            super();
            this.setSelected(isSel);
        }

        public CustomRadio(ButtonGroup btnGroup, boolean isSel) {
            this(isSel);
            btnGroup.add(this);
        }
    }

    class CustomTextField extends JTextField {
        public CustomTextField(String dataVal) {
            super();
        }
    }

    class CustomCombo extends JComboBox {
        public CustomCombo(String[] s) {

            super(s);
        }
    }

    class CustomSpiner extends JSpinner {
        public CustomSpiner() {
            super();
        }
    }
}

2 个答案:

答案 0 :(得分:5)

JTable不应包含组件。它应该包含数据(字符串,整数,布尔值,日期等)。渲染器的目标是使用一个组件(多个单元格的相同实例)将此数据转换为可视化的东西(标签,单选按钮,图标......)。编辑器的目标是能够在可编辑组件中显示数据,接受新值,并使用最终用户输入的数据更改数据值。

不要将组件存储在JTable中。如果默认设置不符合您的需要,请配置渲染器和/或编辑器以渲染/编辑数据。

这一点在JTable tutorial中解释。

答案 1 :(得分:3)

  

表格标题没有显示,我无法编辑列,请指出我的错误,因为我尝试了很多并放弃了

JTable tutorial Concepts: Editors and RenderersUsing Other Editors

中详细介绍了此问题
  1. JComboBox

  2. JSpinner

  3. JRadioButton

  4. JTextField, JFormattedTextField

  5. 如果不了解渲染器和编辑器的工作方式,这些链接无法帮助您