具有多个组件的 JTable 单元格

JTable cell with multiple Component

我的任务是实现一个 JTable,其中一个列具有以下外观:

它应该包含4个元素:
1.一个JCombobox,控制另外两个的启用状态: 它们为 "manual" 状态启用,并锁定其他状态的某些数值(我们称其他组合状态为:'first'、'second'、'third')。
2。一个 JTextField,它在启用时控制滑块的值。
3. 读取“%”但什么都不做的 JLabel。
4. s JSlider,启用后控制 JTextField 值(这种相互控制是否存在?)。

我在网上到处搜寻以寻找实现此功能的方法。大多数例子都太肤浅了,包括这个类似的 question.

在我问这个之前我有我的 Cell Rendering/fireEditingStopped 等折磨,但不幸的是没有简单的短代码可以显示。

谁能告诉我具体的指导方针或代码片段,让我走上正确的道路? 提前致谢。

您需要实现 TableCellRenderer。因此,您可以将多个组件添加到同一个单元格。您可以在这里找到更多信息http://www.coderanch.com/t/614645/GUI/java/adding-components-cell-jtable

你需要 both a TableCellRenderer and a TableCellEditor for any components that are editable. A general example is shown here.

  • 默认渲染器将适用于 String、“%”,如图 here;或者,您可能希望在相邻列的渲染器中显示它。

  • 默认渲染器和编辑器将适用于表示百分比的 DoubleFloat 模型值,如图 here.

  • A DefaultCellEditor 将适用于组合框,如图所示 here; you can decorate the renderer as shown here

  • 尝试 SliderColumnSpinnerColumn,显示 here, for the last column; examine the examples seen here 更多。

Is that a single Renderer/Editor for the all the cell's elements or multiple Ren'/Ed' for each element?

我不确定问题所在,但这是从 TableCellRendererTableCellEditor.

检索数据的简单示例

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);
      }
    }
  }
}

如果您只看一眼,这里的示例就可以使用。当您尝试与它交互时,它会崩溃。我是盲人,除了使用鼠标之外,还有很多方法可以访问计算机。当我使用此 table 时,我只看到 4 行,带类型作为单元格 1 名称。我无法访问组合框或任何控件。尝试仅通过键盘使用此波段类型 table,看看是否可以使用组合框或 select 编辑框做任何事情。如果你想看看这到底有多糟糕,请下载 NVDA 免费屏幕 reader,然后只使用键盘和屏幕 reader 尝试 table。可以在这里找到:https://www.nvaccess.org/download/

我目前正在研究 Arduino IDE 以尝试修复一些像这样的 table 的乱七八糟的东西,但到目前为止我还没有能够让所有组件都集中起来。重要的是,当人们创建这样的东西时,如果您使用键盘导航以及鼠标甚至触摸,焦点就会起作用。我对任何可以让这个 table 为 NVDA 工作的人都非常感兴趣。如果我先到达那里,我会 post Panel Cell 多组件电池的正确实现,但这个不是。