JTable包含所有组件 [英] JTable containing all components
问题描述
这是我的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();
}
}
}
JTable不应该包含组件.它应该包含数据(字符串,整数,布尔值,日期等).渲染器的目标是使用组件(多个单元的相同实例)将数据转换为视觉对象(标签,单选按钮,图标等).编辑者的目标是能够在可编辑的组件中显示数据,接受新值并使用最终用户输入的数据来更改数据的值.
不要将组件存储在JTable中.如果默认设置不适合您的需求,请配置渲染器和/或编辑器以渲染/编辑数据.
在 JTable教程中对此进行了解释. /p>
This is my code for a JTable which I intend to make to have all the components like the ComboBox, JSpinner, JRadioButton, JTextfeiled, etc
But I ended up with this, the table header is not appearing, and I can't edit the columns. Please point out my mistakes as I tried a lot and have given up
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();
}
}
}
A JTable isn't supposed to contain components. It's supposed to contain data (Strings, Integers, Booleans, Dates, etc.). The renderer's goal is to use a component (the same instance for multiple cells) to transform this data into a visual thing (a label, a radio button, an icon, ...). The editor's goal is to be able to display the data in an editable component, to accept a new value, and to change the value of the data with the data entered by the end user.
Don't store components in a JTable. Configure renderers and/or editors to render/edit the data if the default doesn't suit your needs.
This is all explained in the JTable tutorial.
这篇关于JTable包含所有组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!