问题
In the code below, for various rows of same table, I am trying to set an Editable comboBox as editor for first row ( so that the user can select from the available choices or type its own), a filechooser for second row and the default textFiled for the rest of rows.
The Problem: and steps to reproduce it:
1- Run the code, 2- click on second row and choose a folder (the row turns yellow) 3- now click on first row to select the type of movie (just click , no need to type anything or to choose) 4- now make another click back on second row(Folder selection)
you will see the contents of this row will be copied to first row?!
I know there are many things that I did not do right, perhaps handling swings which are not thread safe, handling references and so on. I was wondering if you guys can help me to fix this bug and turn this code to something solid.
after following steps above:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.TableModel;
public class CCellEditor extends DefaultCellEditor {
private JTable m_Table = null;
public CCellEditor(JFrame parentFrame, JTable table) {
super(new JTextField());
super.setClickCountToStart(1);
m_Table = table;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column){
if(row==0) // comBoBox for First row
{
Object[] objectArray = {"3D","2D"};
JComboBox comboBox = new JComboBox(objectArray);
comboBox.setEditable(true);
comboBox.setSelectedItem(value);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(null != m_Table.getCellEditor()){
m_Table.getCellEditor().stopCellEditing();
}
m_Table.setValueAt(e.getItem(), row, 1);
}
}
};
comboBox.addItemListener(itemListener);
PopupMenuListener popMenuEvent = new PopupMenuListener() {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
String sValue = (String)m_Table.getValueAt(row, 1);
if(null != m_Table.getCellEditor()){
m_Table.getCellEditor().stopCellEditing();
}
m_Table.setValueAt(sValue, row, 1);
}
public void popupMenuCanceled(PopupMenuEvent e) {
}
};
comboBox.addPopupMenuListener(popMenuEvent);
return comboBox;
}
else if(row==1) // fileChooser for Second row
{
JFileChooser fileChooser;
fileChooser = new JFileChooser("c:\\");
fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
fileChooser.setVisible(true);
int returnVal = fileChooser.showOpenDialog(null);
JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
textField.setBackground(Color.yellow);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File m_fFirmware= fileChooser.getSelectedFile();
textField.setText(m_fFirmware.getPath());
return textField;
}else
{
return textField;
}
}
// for any other rows
JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
return textField;
}
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnTitles = { "Name", "Value"};
Object[][] dataEntries = {
{ "Movie Type:", "3D" }, {"Folder:","C:"}, {"# of Movies requested:","5"}};
TableModel model = new EditableTableModel(columnTitles, dataEntries);
JTable table = new JTable(model);
table.createDefaultColumnsFromModel();
table.setDefaultEditor(Object.class, new CCellEditor(frame, table));
frame.add(new JScrollPane(table));
frame.setSize(300, 200);
frame.setVisible(true);
}
}
and here is the EditableTableModel class:
import javax.swing.table.AbstractTableModel;
class EditableTableModel extends AbstractTableModel {
String[] columnTitles;
Object[][] dataEntries;
int rowCount;
public EditableTableModel(String[] columnTitles, Object[][] dataEntries) {
this.columnTitles = columnTitles;
this.dataEntries = dataEntries;
}
public int getRowCount() {
return dataEntries.length;
}
public int getColumnCount() {
return columnTitles.length;
}
public Object getValueAt(int row, int column) {
return dataEntries[row][column];
}
public String getColumnName(int column) {
return columnTitles[column];
}
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
public boolean isCellEditable(int row, int column) {
return true;
}
public void setValueAt(Object value, int row, int column) {
dataEntries[row][column] = value;
}
}
回答1:
I must admit it took me a while but I found the issue. You have not overridden the DefaultCellEditor.getCellEditorValue()
function, which is important for your implementation. This function is called when when the cell being edited loses focus or whatever else causes editing to be complete. Anyway, in your case, it just gets the final value from the last default TableCellEditor (from DefaultCellEditor). This is why the first row keeps getting replaced with the last edited cell value.
Here is a way to fix your problem:
- Create a global reference to the
comboBox
. - Override
getCellEditorValue()
and returncomboBox.getSelectedItem()
if the last edited item was the second row. Otherwise, just returnsuper.getCellEditorValue()
.
This will fix your current issue but your code could use a lot of improvement.
回答2:
Preliminary examination suggests letting the model notify its listening view of the change:
public void setValueAt(Object value, int row, int column) {
dataEntries[row][column] = value;
fireTableCellUpdated(row, column);
}
See also Initial Threads, and consider one of these default directories.
fileChooser = new JFileChooser(System.getProperty("user.dir"));
fileChooser = new JFileChooser(System.getProperty("user.home"));
来源:https://stackoverflow.com/questions/11041575/using-all-jcombobox-jtextfield-jfilechooser-as-table-editor-overrides-the-re