问题
This is how my JTable
is reconstructed from the serialized data:
table = new JTable(){
public boolean isCellEditable(int arg0, int arg1) {
return true;
}
};
table.addMouseListener(this);
table.addFocusListener(this);
int width = HCAConstants.hcaGuiFuzzyTableWidth;
int tableRowHeight = HCAConstants.hcaGuiFuzzyTableRowHeight;
table.putClientProperty("cancelEditOnFocusLost", Boolean.TRUE);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setModel(new MBFTableModel(data[currentH.Int][currentF.Int]));
TableColumn col = table.getColumnModel().getColumn(0);
col.setPreferredWidth(width);}
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "none");
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK), "none");
table.setCellSelectionEnabled(true);
KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
InputMap map = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
map.put(tab, "selectNextRowCell");
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "none");
table.setEnabled(true);
table.setDefaultRenderer(FuzzyDat.class,new FuzzyDatRender());
table.setDefaultEditor(FuzzyDat.class, new FuzzyDatEditor(currentF,(LoadToTable)this));
table.setDefaultRenderer(MinMax.class, new MinMaxRender());
table.setDefaultEditor(MinMax.class, new MinMaxEditor());
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
table.setRowHeight(tableRowHeight);
My Scenario is this: Whenever I click on the cell's slider for the first time, it does not respond; on the second click, the component responds and thereafter the slider responds to the first click.
How can make the slider interactive with the first click only rather than clicking twice initially.
The DefaultCellEditor has three default constructors: JCheckBox
, JComboBox
and JTextField
; but I need to override this constructor for JSlider
. How can I achieve this? Is this necessary? It was suggested that I use setClickCountToStart(1)
.
回答1:
Maybe one of SliderColumn
or SpinnerColumn
may help:
SliderColumn:
import java.awt.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class SliderColumn extends AbstractCellEditor
implements TableCellEditor, TableCellRenderer {
private JSlider editSlider, renderSlider;
private Border originalBorder;
public SliderColumn(JTable table, int column) {
editSlider = new JSlider();
renderSlider = new JSlider();
originalBorder = editSlider.getBorder();
editSlider.setBorder(new LineBorder(Color.BLUE));
table.getColumnModel().getColumn(column).setCellEditor(this);
Dimension d = renderSlider.getPreferredSize();
table.setRowHeight(d.height);
}
@Override
public Object getCellEditorValue() {
return editSlider.getValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value != null) {
editSlider.setValue((Integer) value);
}
return editSlider;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (hasFocus) {
renderSlider.setBorder(new LineBorder(Color.BLUE));
} else {
renderSlider.setBorder(originalBorder);
}
if (value == null) {
renderSlider.setValue(0);
} else {
int intValue = ((Integer) value).intValue();
renderSlider.setValue(intValue);
}
return renderSlider;
}
@Override
public boolean isCellEditable(EventObject evt) {
return true;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("SpinnerColumn");
JPanel panel = new JPanel(new GridLayout(1, 1));
JTable table = new JTable(5, 1);
SliderColumn spinnerColumn = new SliderColumn(table, 0);
table.setDefaultRenderer(Object.class, spinnerColumn);
panel.add(table);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
SpinnerColumn:
import java.awt.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class SpinnerColumn extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private static final long serialVersionUID = 1L;
private JSpinner editSpinner, renderSpinner;
private JTable table;
private String[] list;
private Border originalBorder;
public SpinnerColumn(JTable table, int column) {
editSpinner = new JSpinner();
renderSpinner = new JSpinner();
originalBorder = editSpinner.getBorder();
editSpinner.setBorder(new LineBorder(Color.BLUE));
this.table = table;
table.getColumnModel().getColumn(column).setCellEditor(this);
}
public SpinnerColumn(String[] list, JTable table, int column) {
editSpinner = new JSpinner();
editSpinner.setModel(new SpinnerListModel(list));
renderSpinner = new JSpinner();
originalBorder = editSpinner.getBorder();
editSpinner.setBorder(new LineBorder(Color.BLUE));
this.list = list;
this.table = table;
table.getColumnModel().getColumn(column).setCellEditor(this);
}
@Override
public Object getCellEditorValue() {
return editSpinner.getValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
if (list != null) {
editSpinner.setValue(list[0]);
} else {
editSpinner.setValue(0);
}
if (value != null) {
editSpinner.setValue(value);
}
return editSpinner;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (hasFocus) {
renderSpinner.setBorder(new LineBorder(Color.BLUE));
} else {
renderSpinner.setBorder(originalBorder);
}
// *** here's where we set the spinner's value
if (value == null) {
renderSpinner.setValue(0);
} else {
int intValue = ((Integer) value).intValue();
renderSpinner.setValue(intValue);
}
return renderSpinner;
}
@Override
public boolean isCellEditable(EventObject evt) {
return true;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("SpinnerColumn");
JPanel panel = new JPanel(new GridLayout(1, 1));
JTable table = new JTable(5, 1);
SpinnerColumn spinnerColumn = new SpinnerColumn(table, 0);
table.setDefaultRenderer(Object.class, spinnerColumn);
panel.add(table);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
回答2:
It's not clear whether either of FuzzyDatRender
or MinMaxRender
derive from JSlider. It's not clear whether either of FuzzyDatEditor
or MinMaxEditor
have a clickCountToStart
property similar to or derived from that seen in DefaultCellEditor. Without an sscce that exhibits the problem you describe, I don't see how to answer the question.
Addendum: After editing the question, I'm guessing that extending AbstractCellEditor
would be better than DefaultCellEditor
in order to avoid the default clickCountToStart
value.
来源:https://stackoverflow.com/questions/7459898/default-cell-editor-class-query