我的任务是实现一个JTable,其中一个列具有这样的外观:
它应该包含4个元素:
1.一个JCombobox,它控制另外两个的启用状态:
它们已启用"手册"状态,并锁定其他状态的某些数值(让我们调用其他组合状态:' first',' second',' third' )。
2。一个JTextField,启用后,控制滑块的值。
3.一个JLabel,读取"%"什么都不做
4. s JSlider,当启用时,控制JTextField值(这种相互控制是否存在?)。
我遍布网络搜索实现此目的的方法。大多数例子都过于肤浅,包括类似的question。
在我问这个之前,我得到了Cell Rendering / fireEditingStopped等版本的折磨,但不幸的是没有简单的短代码。
任何人都可以告诉我具体的指南或代码片段,它可以让我走上正确的道路吗? 提前谢谢。
答案 0 :(得分:6)
对于所有单元格的元素是单个渲染器/编辑器还是每个元素的多个Ren'/ Ed'?
我不确定问题,但这是一个从TableCellRenderer
和TableCellEditor
检索数据的简单示例。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class MultipleComponentCellTest {
private final String[] columnNames = {"Band Type"};
private final Object[][] data = {
{new BandType("manual", 50)},
{new BandType("locked", 100)},
{new BandType("manual", 32)},
{new BandType("locked", 0)},
};
private final TableModel model = new DefaultTableModel(data, columnNames) {
@Override public Class<?> getColumnClass(int column) {
return BandType.class;
}
};
private final JTable table = new JTable(model);
public JComponent makeUI() {
table.setRowHeight(42);
table.setDefaultRenderer(BandType.class, new BandTypeRenderer());
table.setDefaultEditor(BandType.class, new BandTypeEditor());
return new JScrollPane(table);
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try {
for (UIManager.LookAndFeelInfo laf: UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MultipleComponentCellTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class BandType {
public final String state;
public final int value;
public BandType(String state, int value) {
this.state = state;
this.value = value;
}
}
class BandTypePanel extends JPanel {
private static String DEFAULT = "0";
public final JSlider slider = new JSlider(0, 100);
public final JTextField textField = new JTextField(3);
public final JComboBox<String> comboBox = new JComboBox<>(
new String[] {"manual", "locked"});
public BandTypePanel() {
super(new BorderLayout(5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
comboBox.addItemListener(new ItemListener() {
@Override public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
boolean f = "manual".equals(e.getItem());
slider.setEnabled(f);
textField.setEnabled(f);
}
}
});
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
slider.setOpaque(false);
slider.setFocusable(false);
slider.getModel().addChangeListener(new ChangeListener() {
@Override public void stateChanged(ChangeEvent e) {
BoundedRangeModel m = (BoundedRangeModel) e.getSource();
textField.setText(Objects.toString(m.getValue(), DEFAULT));
}
});
JPanel p = new JPanel();
p.setOpaque(false);
p.add(comboBox);
p.add(textField);
p.add(new JLabel("%"));
add(p, BorderLayout.WEST);
add(slider);
}
public void updateValue(BandType bt) {
comboBox.setSelectedItem(bt.state);
slider.setValue(bt.value);
textField.setText(Objects.toString(bt.value, DEFAULT));
}
}
class BandTypeRenderer extends BandTypePanel implements TableCellRenderer {
public BandTypeRenderer() {
super();
setName("Table.cellRenderer");
}
@Override public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
if (value instanceof BandType) {
updateValue((BandType) value);
}
return this;
}
}
class BandTypeEditor extends BandTypePanel implements TableCellEditor {
protected transient ChangeEvent changeEvent;
@Override public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
if (value instanceof BandType) {
updateValue((BandType) value);
}
return this;
}
@Override public Object getCellEditorValue() {
return new BandType(comboBox.getSelectedItem().toString(), slider.getValue());
}
//Copied from AbstractCellEditor
//protected EventListenerList listenerList = new EventListenerList();
@Override public boolean isCellEditable(EventObject e) {
return true;
}
@Override public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
@Override public void cancelCellEditing() {
fireEditingCanceled();
}
@Override public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (Objects.isNull(changeEvent)) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (Objects.isNull(changeEvent)) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
答案 1 :(得分:3)
答案 2 :(得分:0)
您需要实现TableCellRenderer。因此,您可以向同一个单元格添加多个组件。您可以在此处找到更多信息http://www.coderanch.com/t/614645/GUI/java/adding-components-cell-jtable
答案 3 :(得分:0)
如果您正在做的所有事情都在这里,示例将适用。当您尝试与之互动时,它会崩溃。我是盲人,除了使用鼠标外,还有许多访问计算机的方法。当我使用该表时,我看到的是带类型为单元格1名称的4行。我无法进入组合框或任何控件。尝试仅通过键盘使用此乐队类型表,看看是否可以使用组合框执行任何操作,或者是否可以选择编辑框。如果您想知道这有多严重,请下载免费的NVDA屏幕阅读器,并尝试使用键盘和屏幕阅读器,而无需使用其他表格。可以在这里找到:https://www.nvaccess.org/download/
我目前正在Arduino IDE上尝试修复像这样的人的桌子乱七八糟的东西,到目前为止,我还无法集中所有组件。重要的是,人们必须确保在创建类似这样的内容时使用键盘导航,鼠标甚至触摸时,焦点将起作用。我对可以使此表适用于NVDA的任何人都非常感兴趣。如果我首先到达那里,我将发布一个Panel Cell多组件单元格的正确实现,但这不是吗。