这是我第一次在这里发帖提问,所以请告诉我,我是不是尊重规则,或者是否缺少某些规则。
我的问题如下。我有一个有JTable的窗口。在这张表中,我有不同的数据。两列使用JComboBoxes从一组数据中选择值。 第二个ComboBox中的数据应基于第一个ComboBox的选择,但仅适用于当前选定的行。
不幸的是,它并没有发挥作用,它在第二个ComboBox中对该列中的所有ComboBox进行了更改。
为了简化问题,我从Oracle Java教程中获取了演示代码: [http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox][1] 并调整了一些代码以完成我需要它做的事情。
我复制了我的整个小程序,因此您可以复制它并在您的机器上运行它,如果您需要/需要。
package demoTraining;
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* TableRenderDemo.java requires no other files.
*/
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* TableRenderDemo is just like TableDemo, except that it explicitly initializes
* column sizes and it uses a combo box as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel implements ItemListener {
private boolean DEBUG = true;
JTable table;
public TableRenderDemo() {
super(new GridLayout(1, 0));
table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
// Set up column sizes.
initColumnSizes(table);
// Fiddle with the Sport column's cell editors/renderers.
setUpSportColumn(table, table.getColumnModel().getColumn(2));
setUpCategoryColumn(table, table.getColumnModel().getColumn(5));
// Add the scroll pane to this panel.
add(scrollPane);
}
/*
* This method picks good column sizes. If all column heads are wider than
* the column's cells' contents, then you can just use
* column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer = table.getTableHeader()
.getDefaultRenderer();
for (int i = 0; i < 6; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(null,
column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i))
.getTableCellRendererComponent(table, longValues[i], false,
false, 0, i);
cellWidth = comp.getPreferredSize().width;
if (DEBUG) {
System.out.println("Initializing width of column " + i + ". "
+ "headerWidth = " + headerWidth + "; cellWidth = "
+ cellWidth);
}
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setUpSportColumn(JTable table, TableColumn sportColumn) {
// Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
comboBox.addItemListener(this);
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
// Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
private void setUpCategoryColumn(JTable table, TableColumn categoryColumn) {
JComboBox categoryComboBox = new JComboBox();
categoryComboBox.addItem("Category1");
categoryComboBox.addItem("Category2");
categoryComboBox.addItem("Category3");
categoryColumn.setCellEditor(new DefaultCellEditor(categoryComboBox));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
categoryColumn.setCellRenderer(renderer);
}
/*
* (non-Javadoc)
*
* @see
* java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*
* Catches the itemStateChange on the sport ComboBox and changes the values
* of the Category ComboBox.
*/
public void itemStateChanged(ItemEvent e) {
// Only does something on itemEven "SELECTED"
if (e.getStateChange() == ItemEvent.SELECTED) {
// Only does something if a row is selected as it needs the row
// number to make changes.
if (table.getSelectedRow() != -1) {
String currentSportSelection = e.getItem().toString();
System.out.println("1. Current selection: "
+ currentSportSelection);
// If the selection in the sports combobox is
// "None of the above" this change happens.
if (currentSportSelection == "None of the above") {
// Some Sysos to help with debugging.
System.out.println("2. Selected Row: "
+ table.getSelectedRow());
System.out.println("2.1. First Row Category: "
+ table.getModel().getValueAt(0, 5));
System.out.println("2.2. Second Row Category: "
+ table.getModel().getValueAt(1, 5));
System.out.println("3. Starting change of category column");
// Getting the tableCellEditor of the category cell in the
// selected row.
TableCellEditor categoryCellEditor = table.getCellEditor(
table.getSelectedRow(), 5);
// Getting the CellEditorComponent (ComboBox) of the
// category cell in the current row.
JComboBox currentCategoryComboBox = (JComboBox) categoryCellEditor
.getTableCellEditorComponent(table, null, true,
table.getSelectedRow(), 5);
// Refill the category combobox.
currentCategoryComboBox.removeAllItems();
currentCategoryComboBox.addItem("SuperCategory1");
currentCategoryComboBox.addItem("SuperCategory2");
currentCategoryComboBox.addItem("SuperCategory3");
currentCategoryComboBox.addItem("SuperCategory4");
// Set the new value in the cell.
table.getModel().setValueAt(
currentCategoryComboBox.getItemAt(0),
table.getSelectedRow(), 5);
// Some more Sysos
System.out.println("4. Selected Row: "
+ table.getSelectedRow());
System.out.println("5. Category of selected row: "
+ table.getValueAt(table.getSelectedRow(), 5));
System.out.println("6. Categorylist top item: "
+ currentCategoryComboBox.getItemAt(0));
System.out.println("7. Item deselected!");
table.getSelectionModel().clearSelection();
// Else clause does the same as if clause.
} else /*
* if (currentSportSelection == "Rowing" ||
* currentSportSelection == "Knitting" ||
* currentSportSelection == "Speed reading" ||
* currentSportSelection == "Pool" ||
* currentSportSelection == "None of the above")
*/{
System.out.println("8. Selected Row: "
+ table.getSelectedRow());
System.out.println("8.1. First Row Category: "
+ table.getModel().getValueAt(0, 5));
System.out.println("8.2. Second Row Category: "
+ table.getModel().getValueAt(1, 5));
TableCellEditor categoryCellEditor = table.getCellEditor(
table.getSelectedRow(), 5);
JComboBox categoryComboBox = (JComboBox) categoryCellEditor
.getTableCellEditorComponent(table, null, true,
table.getSelectedRow(), 5);
categoryComboBox.removeAllItems();
categoryComboBox.addItem("Category1");
categoryComboBox.addItem("Category2");
categoryComboBox.addItem("Category3");
table.getModel().setValueAt(categoryComboBox.getItemAt(0),
table.getSelectedRow(), 5);
// Some Sysos
System.out.println("9. Selected Row: "
+ table.getSelectedRow());
System.out.println("10. Category of selected row: "
+ table.getValueAt(table.getSelectedRow(), 5));
System.out.println("11. Categorylist top item: "
+ categoryComboBox.getItemAt(0));
System.out.println("12. Item deselected!");
table.getSelectionModel().clearSelection();
}
}
}
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian", "Category" };
private Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false), "Category1" },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true),
"Category2" }, };
public final Object[] longValues = { "Jane", "Kathy",
"None of the above", new Integer(20), Boolean.TRUE, "Category3" };
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[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 last column
* would contain text ("true"/"false"), rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's data can
* change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value + " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我已经读过其他一些我需要使用的问题&#34; getTableCellRendererComponent()&#34;我做了但我不是100%确定我正确使用它。
如果您需要更多信息,请随时询问。
提前感谢您对此问题的任何帮助。
Leo K。
答案 0 :(得分:1)
您需要更改表格中每行的编辑器。
以下是一个示例,说明如何为每行显示不同的编辑器:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editorData to be used for each row
editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
您显然需要自定义逻辑,因为您的编辑器将基于另一列中的值,而不是行,但代码应该让您开始。