我正在体育和艺术专栏中展示一个带有组合框编辑器的表格。即使选择的项目是随机选择的,它们也会添加到列表中。我的问题是正确地将firstname列中的内容映射到sports列和arts列。我的代码基于TableRenderDemo
,在此处给出。
/*
* 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.DefaultTableCellRenderer;
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;
import java.util.LinkedList;
public class TableRenderDemo extends JPanel {
private boolean DEBUG = false;
static LinkedList<String> helpStringFirst = new LinkedList<String>();
static LinkedList<String> helpStringSecond = new LinkedList<String>();
static LinkedList<String> arts = new LinkedList<String>();
Object[][] data = {
{"Kathy", "Smith", "Snowboarding", "PencilArt", new Boolean(false)},
{"John", "Doe", "Rowing", "Drawing", new Boolean(true)},
{"Sue", "Black","Knitting", "Yoga", new Boolean(false)},
{"Jane", "White","Speed reading","Music", new Boolean(true)},
{"Joe", "Brown", "Pool", "Dance", new Boolean(false)}
};
public TableRenderDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sports",
"Arts",
"Vegetarian"};
final Object[] longValues = {"Jane", "Kathy",
"None of the above",
new Integer(20), Boolean.TRUE};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(1000, 200));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
setUpSportColumn(table, table.getColumnModel().getColumn(2));
setUpArtsColumn(table, table.getColumnModel().getColumn(3));
add(scrollPane);
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
final 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");
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);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED) ) {
String str = comboBox.getSelectedItem().toString();
System.out.println("str>>:"+str);
setLabelText(str);
}
}
});
}
public void setUpArtsColumn(JTable table,
TableColumn artColumn) {
//Set up the editor for the sport cells.
final JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("Pencilsketch");
comboBox1.addItem("Drawing");
comboBox1.addItem("music");
comboBox1.addItem("Dance");
comboBox1.addItem("Yoga");
comboBox1.addItem("None of the above");
artColumn.setCellEditor(new DefaultCellEditor(comboBox1));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
artColumn.setCellRenderer(renderer);
comboBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
String str1 = comboBox1.getSelectedItem().toString();
System.out.println("str:"+str1);
setLabelText1(str1);
}
}
});
}
private static void setLabelText(String str1) {
helpStringFirst.add(str1);
System.out.println("helpStringFirst:"+helpStringFirst);
}
private static void setLabelText1(String str2) {
helpStringSecond.add(str2);
System.out.println("helpStringSecond:"+helpStringSecond);
}
/**
* 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("SimpleTableDemo");
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();
}
});
}
}