English 中文(简体)
包含所有组成部分的J表格
原标题:JTable containing all components

这就是我的JTable的代码 我打算制作这个JTable 以拥有所有部件,如Combombox、Juspinner、JradioButton、JTfreefuled等

但我最后还是做了这个, 桌子页眉没有出现, 我无法编辑栏目。 请指出我的错误, 我做了很多尝试, 放弃了

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();
        }
    }
}
问题回答

JTable 不应该包含组件。 它应该包含数据( strings、 Intgers、 Booleans、Dates等)。 创建者的目标是使用一个组件(多个单元格的相同实例)将数据转换成视觉物体( 标签、 无线电按钮、 图标 ) 。 编辑器的目标是能够在一个可编辑的组件中显示数据, 接受新的值, 并用终端用户输入的数据改变数据值 。

不要在 JTable 中存储组件。 如果默认值不符合您的需要, 请配置投递者和/ 或编辑来生成/ 编辑数据 。

这在"http://docs.oracle.com/javase/tuative/uiswing/upports/table.html"rel=“norefererr”>JTable教程 中解释。

桌子页眉没有出现,我无法编辑列,请指出我的错误,因为我做了很多尝试,放弃了

这个问题在>Jtable 教义概念:编辑和Renderers http://docs.oracle.com/javase/tument/tument/uiswing/upres/table.html#editor" rel=“不随从 noreferr> >Using其他编辑 中详细叙述。

  1. JComboBox

  2. JSpinner

  3. JRadioButton

  4. < a href=""http://docs.oracle.com/javase/tumentive/uiswing/ parties/ attable.html#validtext" rel="不跟随 no follow norefererr">JText Field, JFormatteText Field

这些链接无法帮助您, 不理解 Renderer 和 编辑器如何工作 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签