JTable:单元格中自定义面板中的按钮

发布于 2024-09-17 01:05:43 字数 588 浏览 5 评论 0原文

我希望能够在单元格中放置一个 JPanel,并在单击时执行一些工作。

我查找了有关单元格编辑器的方法,但所有示例都讨论用另一个组件替换单元格(例如,用 JTextField 替换 int 等)。我的情况有点不同:

我有以下 ADT

class MyClass {
  public String title;
  public String url;
  public String path;
  public int annotations;
}

我创建了一个自定义表格单元格模型有 1 列,该列的类是 MyClass。然后,我为该类创建了一个返回 JPanel 的单元格渲染器,如下所示:

MyClass Cell Renderer

如您所见, JPanel 包含一个按钮。我希望每当单击此按钮时都会启动 JFrame。有什么想法吗?

如果您建议使用单元格编辑器,请更具体地说明如何操作。如果可能,请提供一些伪代码。

谢谢。

PS我很确定这个问题的标题需要一些修改。 ;)

I want to be able to have a JPanel in a cell with a JButton that does some work when clicked.

I looked for howtos about Cell Editors, but all examples talk about replacing the cell with another component (e.g. replace an int with a JTextField etc.) My situation is a little different:

I have the following ADT

class MyClass {
  public String title;
  public String url;
  public String path;
  public int annotations;
}

I created a custom table cell model that has 1 column and the class for that column is MyClass. Then I created a cell renderer for that class that returns a JPanel as shown here:

MyClass Cell Renderer

As you can see, the JPanel contains a button. I want this button to launch a JFrame whenever clicked. Any ideas?

If you will suggest Cell Editor, please be a little more specific about how to do so. If possible, provide some pseudo-code.

Thank you.

P.S. I'm pretty sure the title of this question needs some work. ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-09-24 01:05:43

coding.mof 的回复之后,我终于做了什么我想要。然而,我想要这个问题的更完整的答案,所以我自己提供一个。

因此,单元渲染器只是绘制组件,不允许在其中进行任何交互。而单元格编辑器则可以。

最初,JTable 中的所有单元格都是由注册的渲染器返回的组件。但是,当选择单元格时,该组件将被编辑器返回的组件替换。这两个实际上可以是不同的组件!我很确定您可以利用这一点并制作一些时髦的单元格:P

无论如何,在这个示例中,渲染器和编辑器都显示相同的组件,因此我们将创建一个供两者使用的组件。

首先,我们需要创建一个返回 ADT 的 TableModel:

class MyClassTableModel extends DefaultTableModel {
  List<MyClass> data;

  public MyClassTableModel(List<MyClass> data) {
    this.data = data;
  }

  public Class<?> getColumnClass(int columnIndex) { return MyClass.class; }
  public int getColumnCount() { return 1; }
  public String getColumnName(int columnIndex) { return "MyClass"; }
  public int getRowCount() { return (data == null) ? 0 : data.size(); }
  public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex); }
  public boolean isCellEditable(int rowIndex, int columnIndex) { return true; }
}

现在,我们创建一个将在渲染器和编辑器之间共享的组件:

class MyClassCellComponent extends JPanel() {
  MyClass myClass;

  public MyClassCellComponent() {
    // initialize components (labels, buttons, etc.)
    // add action listeners
  }

  public void updateData(MyClass myClass, boolean isSelected, JTable table) {
    this.myClass = myClass;
    // update buttons, labels etc. accordingly
  }
}

isSelected 和 table 参数用于渲染面板的背景,并且是可选的。以下是渲染器如何使用我们的组件:

class MyClassCellRenderer implements TableCellRenderer {
  MyClassCellComponent panel;

  public MyClassCellRenderer() {
    panel = new MyClassCellComponent();
  }

  public Component getTableCellRendererComponent(JTable table, Object value,        boolean isSelected, boolean hasFocus, int row, int column) {
    MyClass myClass = (MyClass)value;
    panel.updateData(myClass, isSelected, table);
    return panel;
  }
}

以下是编辑器如何使用它:

class MyClassCellEditor extends AbstractCellEditor {
  MyClassCellComponent panel;
  public MyClassCellEditor() {
    panel = new MyClassCellComponent();
  }
  public Component getTableCellEditorComponent(JTable table, Object value,      boolean isSelected, int row, int column) {
    MyClass myClass = (MyClass)value;
    panel.updateData(myClass, true, table);
    return panel;
  }
  public Object getCellEditorValue() {
    return null;
  }
}

仅此而已。现在我们可以简单地创建一个 JTable,如下所示:

JTable myClassTable = new JTable(new MyClassTableModel());
myClassTable.setDefaultRenderer(MyClass.class, new MyClassCellRenderer());
myClassTable.setDefaultEditor(MyClass.class, new MyClassCellEditor());

我们就完成了!

PS 我很确定我们可以将渲染器和编辑器合并到一个类中,扩展 AbstractCellEditor 并实现 TableCellRenderer,但我不确定性能。

After coding.mof's reply, I finally did what I wanted. However, I wanted a more complete answer for this question, so I will provide one myself.

So, Cell Renderers simply draw the component and do not allow any interactions within it. While Cell Editors do.

Initially, all cells in the JTable are components that are returned by the registered renderer. However, when a cell is selected, this component is replaced by a component that is returned by the editor. These two can actually be different components! Which I'm pretty sure you can take advantage of this and make some funky cells :P

Anyway, in this example, both the renderer and the editor display the same component, so we will create a component that will be used by both.

First, we need to create a TableModel that returns our ADT:

class MyClassTableModel extends DefaultTableModel {
  List<MyClass> data;

  public MyClassTableModel(List<MyClass> data) {
    this.data = data;
  }

  public Class<?> getColumnClass(int columnIndex) { return MyClass.class; }
  public int getColumnCount() { return 1; }
  public String getColumnName(int columnIndex) { return "MyClass"; }
  public int getRowCount() { return (data == null) ? 0 : data.size(); }
  public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex); }
  public boolean isCellEditable(int rowIndex, int columnIndex) { return true; }
}

Now, we create a component that will be shared between the Renderer and the Editor:

class MyClassCellComponent extends JPanel() {
  MyClass myClass;

  public MyClassCellComponent() {
    // initialize components (labels, buttons, etc.)
    // add action listeners
  }

  public void updateData(MyClass myClass, boolean isSelected, JTable table) {
    this.myClass = myClass;
    // update buttons, labels etc. accordingly
  }
}

The isSelected and table parameters are used to render the background of the panel and are optional. Here is how the renderer uses our component:

class MyClassCellRenderer implements TableCellRenderer {
  MyClassCellComponent panel;

  public MyClassCellRenderer() {
    panel = new MyClassCellComponent();
  }

  public Component getTableCellRendererComponent(JTable table, Object value,        boolean isSelected, boolean hasFocus, int row, int column) {
    MyClass myClass = (MyClass)value;
    panel.updateData(myClass, isSelected, table);
    return panel;
  }
}

And here is how the editor uses it:

class MyClassCellEditor extends AbstractCellEditor {
  MyClassCellComponent panel;
  public MyClassCellEditor() {
    panel = new MyClassCellComponent();
  }
  public Component getTableCellEditorComponent(JTable table, Object value,      boolean isSelected, int row, int column) {
    MyClass myClass = (MyClass)value;
    panel.updateData(myClass, true, table);
    return panel;
  }
  public Object getCellEditorValue() {
    return null;
  }
}

Thats all. Now we can simply create a JTable as follows:

JTable myClassTable = new JTable(new MyClassTableModel());
myClassTable.setDefaultRenderer(MyClass.class, new MyClassCellRenderer());
myClassTable.setDefaultEditor(MyClass.class, new MyClassCellEditor());

And we're done!

P.S. I'm pretty sure that we can combine the Renderer and the Editor into a single class the extends AbstractCellEditor and implements TableCellRenderer, but I'm not sure about performance.

请别遗忘我 2024-09-24 01:05:43
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor { 

JComponent pan = new JPanel();

public MyTableCellEditor () {
  pan.add(btn);
  // add all elments you need to your panel
  btn.addActionListener( /* the listener which will handle the events */ );
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) { 
   btn.setText( /* according to row or whatever*/ );
   // set all elemnts of you panel to the according values
   // or add dynamically an action listener
   return pan;
}
public Object getCellEditorValue() { return new Void(); } 
} 

因此,在侦听器中,您必须检查表的选择,以便可以以不同的方式对每一行进行响应。
如果您想将所有元素保留在一列中,您可以将 JButton 替换为包含所有组件的面板。然后 JTable 将把所有事件转发到该 JPanel。

public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor { 

JComponent pan = new JPanel();

public MyTableCellEditor () {
  pan.add(btn);
  // add all elments you need to your panel
  btn.addActionListener( /* the listener which will handle the events */ );
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) { 
   btn.setText( /* according to row or whatever*/ );
   // set all elemnts of you panel to the according values
   // or add dynamically an action listener
   return pan;
}
public Object getCellEditorValue() { return new Void(); } 
} 

So in your listener you will have to check the selection of the table so that you can respond in different ways for each row.
If you want to keep all elements in one column you can replace the JButton with your panel containing all the components. The JTable will then forward all events to that JPanel.

狼性发作 2024-09-24 01:05:43

您可以创建一个包含多列的表格,然后添加一个单独的列来包含您的按钮。 Table Button Column 类可以让您轻松完成此操作。

You could create a table with multiple columns, then you can add a separate column to contain your button. The Table Button Column class allows you to do this easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文