JTable 或其他具有高级单元格选择功能的 Java 表类?

发布于 2024-07-18 06:35:31 字数 537 浏览 7 评论 0原文

现在我正在使用 JTable 来完成我正在做的事情。 在浏览了 Java API 和各种 Web 资源之后,我认为 JTable 不再能解决这个问题了。 我正在寻找的是一个可以指定非常严格的选择程序的表格。 我希望不仅能够选择行和列,而且还能够选择对角线方向的单元格。 更重要的是,我需要能够指定在另一个单元格中时可以选择哪些单元格的整体能力。

例如,如果我有一个 10x10 的表格,并且位于单元格 (4, 3) [(行,列)] 中,我希望能够说,好吧,您可以从此处选择以下间隔:

  • (4, 3) 至 (4, 10)
  • (4, 3) 至 (4, 1)
  • (4, 3) 至 (10, 4)
  • (4, 3) 至 (1, 4)
  • (4, 3) 至 (10, 10) [对角线]
  • (4, 3) 到 (1, 1) [对角线]
  • (4, 3) 到 (1, 6) [对角线]
  • (4, 3) 到 (6, 1) [对角线]

任何想法我怎么能做到这一点?

Right now I'm using a JTable for what I'm doing. After looking through the Java API and various web resources, I don't think a JTable is going to cut it anymore. What I'm looking for is a table for which I can specify very strict selection procedures. I want to be able to not only select rows and columns, but also select cells in a diagonal direction. More than that, I need the overall ability to be able to specify which cells can be selected when in another cell.

For example, if I have a 10x10 table and I'm in cell (4, 3) [(row, column)], I want to be able to say, okay, you can select the following intervals from here:

  • (4, 3) to (4, 10)
  • (4, 3) to (4, 1)
  • (4, 3) to (10, 4)
  • (4, 3) to (1, 4)
  • (4, 3) to (10, 10) [diagonally]
  • (4, 3) to (1, 1) [diagonally]
  • (4, 3) to (1, 6) [diagonally]
  • (4, 3) to (6, 1) [diagonally]

Any ideas for how I could do this?

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

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

发布评论

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

评论(2

嘿嘿嘿 2024-07-25 06:35:31

听起来你并不是真的在为“桌子”建模。 (JTable 假定表语义并使用列表选择模型。)但是,如果您愿意破解 JTable 代码,我认为它与矩阵相差并不远。

另一种选择是您自己的(是的)组件:包含矩阵单元的 JPanel。 所有键盘/鼠标事件处理都需要委托给父 JPanel。 我当然会建议从 JTable 克隆相关子集和设计(数据模型、选择模型等)。

所以,基本上,您将需要 3 个类:

JMatrix、JMatrixModel、JMatrixSelectionModel。

JMatrix 是扩展的 JPanel 及其子组件。 JMatrixSelectionModel 是将实现选择规则的类。 JMatrix 应在选择事件上调用选择模型(在矩阵单元上注册,委托给父 JMatrix 上的处理程序)。 数据模型相当简单——您甚至可以使用现有的 JTableModel。

Doesn't sound like you are really modeling a 'table'. (JTable assumes table semantics and uses a List selection model.) However, I don't think it's that far removed from a matrix, if you are willing to hack the JTable code.

An alternative is your own (yep) component: A JPanel that contains the matrix cells. All keyboard/mouse event processing needs to be delegated to the parent JPanel. I would certainly recommend cloning the relevant subsets and design from JTable (data model, selection model, etc.).

So, basically, you will need 3 classes:

JMatrix, JMatrixModel, JMatrixSelectionModel.

The JMatrix is the extended JPanel with its child components. The JMatrixSelectionModel is the class that will implement the selection rules. The JMatrix should call the selection model on selection events (registered on matrix cells, delegated to handler on the parent JMatrix). The data model is fairly straightforward -- you may even be able to use existing JTableModel.

凯凯我们等你回来 2024-07-25 06:35:31

我也有类似的情况。 我的解决方案(抱歉,不想编写一个庞大的类)是为所有列添加一个单元格渲染器,它是表的鼠标侦听器。 由于渲染器知道哪些按钮已被选择,因此它可以以不同的方式渲染它们。

public class MultipleSelectionRenderer extends DefaultTableCellRenderer implements MouseListener {
    private JTable table;
    private Map<String, Boolean> selectedMap = new LinkedHashMap<String, Boolean>();
    TableUpdateIfc updater;
public MultipleSelectionRenderer(TableUpdateIfc updater, JTable table, Map<String, Boolean> selectedMap) {
    this.table = table;
    this.selectedMap = selectedMap;
    this.updater = updater;
}

@Override
public void mouseReleased(MouseEvent e) {
    if(e.getSource() == table){
        try {
            if(!e.isControlDown())
                selectedMap.clear();
            selectedMap.put(table.getSelectedRow()+":"+table.getSelectedColumn(), true);
        } catch (Exception ex) {
            selectedMap.clear();
        }
    }
    updater.updateMultipleSelectionTable(table);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}

/**
 *
 * @param table
 * @param value
 * @param isSelected
 * @param hasFocus
 * @param row
 * @param column
 * @return
 */
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component result =super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if(selectedMap.get(row+":"+column) != null && selectedMap.get(row+":"+column) == true) {
        setText(getHTMLString(value));
    }
    return result;
}

private String getHTMLString(Object value){
    String html = "<html><body><table cellpadding=0><tr>";
    html = html + "<td bgcolor=#bf65a5>";
    html = html + value.toString();
    html = html + "</td><td> " + value+"</td>";
    html = html + "</tr></table></body></html>";
    return html;
}

}

I am in a similar situation. My solution (sorry, don't want to write a huge class) was to add a Cell Renderer for all the columns which was a mouse listener for the table. Since the Renderer knows which buttons have been selected, it can render them differently.

public class MultipleSelectionRenderer extends DefaultTableCellRenderer implements MouseListener {
    private JTable table;
    private Map<String, Boolean> selectedMap = new LinkedHashMap<String, Boolean>();
    TableUpdateIfc updater;
public MultipleSelectionRenderer(TableUpdateIfc updater, JTable table, Map<String, Boolean> selectedMap) {
    this.table = table;
    this.selectedMap = selectedMap;
    this.updater = updater;
}

@Override
public void mouseReleased(MouseEvent e) {
    if(e.getSource() == table){
        try {
            if(!e.isControlDown())
                selectedMap.clear();
            selectedMap.put(table.getSelectedRow()+":"+table.getSelectedColumn(), true);
        } catch (Exception ex) {
            selectedMap.clear();
        }
    }
    updater.updateMultipleSelectionTable(table);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}

/**
 *
 * @param table
 * @param value
 * @param isSelected
 * @param hasFocus
 * @param row
 * @param column
 * @return
 */
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component result =super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if(selectedMap.get(row+":"+column) != null && selectedMap.get(row+":"+column) == true) {
        setText(getHTMLString(value));
    }
    return result;
}

private String getHTMLString(Object value){
    String html = "<html><body><table cellpadding=0><tr>";
    html = html + "<td bgcolor=#bf65a5>";
    html = html + value.toString();
    html = html + "</td><td> " + value+"</td>";
    html = html + "</tr></table></body></html>";
    return html;
}

}

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