可编辑 JTable 教程

发布于 2024-07-05 05:40:11 字数 399 浏览 7 评论 0原文

有没有关于创建 JTable 的好书或网站? 我想让一列可编辑。 我想实际上将继承的 JCheckBox 组件(我们在此处创建的)放入表列之一,而不是仅仅将表放入 JCheckBox 中,因为它是一个可编辑的布尔值字段。

我有 JFC Swing 教程第二版 书,但我只是想知道如果还有其他示例,我可以查看并学习如何更好地处理表格。 这本书似乎只是把网上的java‘踪迹’放到了书中。

不过,我正在重新阅读这些内容,只是好奇是否有人找到了可能有更多帮助的东西。

Are there any good books or website that go over creating a JTable? I want to make one column editable. I would like to actually put a inherited JCheckBox component (that we created here) into one of the table columns instead of just having the table put JCheckBox in based on it being an editable boolean field.

I have the JFC Swing Tutorial Second Edition book but I just would like to know if there are other examples I could look at and learn how to deal with the tables better. The book seems to just take the java 'trail' online and put it in the book.

I am re-reading the stuff though, just curious if anyone has found something that might help out more.

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

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

发布评论

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

评论(6

走过海棠暮 2024-07-12 05:40:11

要使列可编辑,您必须重写 TableModel 中的 isCellEditable 方法。 如果您继承了 AbstractTableModel,那么创建 TableModel 就相当容易了,并且我建议除最简单的 JTable 之外的所有模型都使用它。

但是,调整 TableModel 只是您需要做的一部分。 要实际在 JTable 中获取自定义组件,您需要设置自定义单元格渲染器。 要使用交互式自定义组件,您需要设置自定义单元格编辑器。 在某些情况下,为此使用默认类的稍微修改版本就足够了。

编辑器

如果您已经有了自定义组件,则可以使用委托轻松完成:创建一个实现TableCellEditor的新类,并返回该组件的实例getCellEditorComponent 方法中的组件。 此方法的参数包括当前值以及单元格坐标、返回表格的链接以及单元格是否被选择。

TableCellEditor 还有一个方法,当用户提交对单元格内容的更改(您可以在其中验证用户输入并调整模型)或取消编辑时调用该方法。 如果您以编程方式中止编辑,请务必在编辑器上调用 stopEditing() 方法,否则编辑器组件将保留在屏幕上 - 这曾经花了我大约 2 个小时来调试。

请注意,在 JTable 编辑器中,只有编辑器接收事件! 显示按钮可以使用渲染器来完成。 但要获得正常运行的按钮,您需要实现一个注册了正确的 EventListener 的编辑器。 在渲染器上注册侦听器不会执行任何操作。

渲染器

对于您在问题中描述的内容来说,实现渲染器并不是绝对必要的,但您通常最终都会这样做,即使只是进行较小的修改。 与编辑器不同,渲染器对速度至关重要。 渲染器的 getTableCellRendererComponent 会为表格中的每个单元格调用一次!渲染器返回的组件仅用于绘制单元格,不用于交互,因此可以被下一个单元格“重用”。 换句话说,您应该调整组件(例如,如果它是 TextComponent,则使用 setText(...)setFont(...) >) 在渲染器中,您不应该实例化一个新的渲染器——这是一种削弱性能的简单方法。

注意事项

请注意,为了让渲染器和编辑器正常工作,您需要告诉 JTable 何时使用特定的渲染器/编辑器。 基本上有两种方法可以做到这一点。 您可以使用相应的 JTable 方法为特定类型设置默认单元格渲染器/编辑器。 为了使这种方式发挥作用,您的 TableModel 需要在 getColumnClass(...) 方法中准确返回此类型! 默认表模型不会为您执行此操作,它始终返回Object.class。 我相信这一点已经难倒了很多人。

设置编辑器/渲染器的另一种方法是在列本身上显式设置它,即通过以下方法的 getTableColumn(...) 方法获取 TableColumn JTable。 这要复杂得多,但是,这也是为单个类拥有两个不同渲染器/编辑器的唯一方法。 例如,您的模型可能有两列 String 类,它们以完全不同的方式呈现,一次可能使用 JLabel/DefaultRenderer ,另一次使用 JButton 来访问更详细的内容编辑。

JTable 及其自定义渲染器和编辑器极其具有多种功能,但它也有很多需要注意的地方,并且有很多事情可能会出错。 祝你好运!

如何在 Swing 教程 是任何自定义 JTables 的人的必读内容。 特别是,阅读并重读概念:编辑器和渲染器< /a> 因为它通常需要一段时间才能“点击”。 关于自定义渲染器和编辑器的示例也非常有价值。

To make a column editable you have to override the isCellEditable method in the TableModel. Creating a TableModel is fairly easy if you inherit AbstractTableModel and I'd recommend it for all but the most simple JTables.

However, adapting the TableModel is only part of what you need to do. To actually get a custom component in the JTable, you need to set a custom cell renderer. To use an interactive custom component, you need to set a custom cell editor. In some cases, it's enough to use slightly modificated versions of the default classes for this.

Editors

If you already have got a custom component is easily done using delegation: Create a new class implementing TableCellEditor, and return a new instance of the component in the getCellEditorComponent method. The paramaters to this method include the current value as well as the cell coordinates, a link back to the table and wether or not the cell is selected.

The TableCellEditor also has a method that is called when the user commits a change to the cell contents (where you can validate user input and adjust the model) or cancels an edit. Be sure to call the stopEditing() method on your editor if you ever programmatically abort editing, otherwise the editor component will remain on screen -- this once took me like 2 hours to debug.

Note that within a JTable editors and only editors receive events! Displaying a button can be done using a renderer. But to get a functioning button, you need to implement an editor with the correct EventListeners registered. Registering a listener on a renderer does nothing.

Renderers

Implementing a renderer is not strictly necessary for what you describe in your question, but you typically end up doing it anyway, if only for minor modifications. Renderers, unlike editors, are speed critical. The getTableCellRendererComponent of a renderer is called once for every cell in the table! The component returned by a renderer is only used to paint the cell, not for interaction, and thus can be "reused" for the next cell. In other words, you should adjust the component (e.g. using setText(...) or setFont(...) if it is a TextComponent) in the renderer, you should not instantiate a new one -- that's an easy way to cripple the performance.

Caveats

Note that for renderers and editors to work, you need to tell the JTable when to use a certain renderer/editor. There are basically two ways to do this. You can set the default cell renderer/editor for a certain type using the respective JTable methods. For this way to work, your TableModel needs to return exactly this type in the getColumnClass(...) method! The default table model will not do this for you, it always returns Object.class. I'm sure that one has stumped a lot of people.

The other way to set the editor/renderer is by explicitly setting it on the column itself, that is, by getting the TableColumn via the getTableColumn(...) method of the JTable. This is a lot more elaborate, however, it's also the only way to have two different renderers/editors for a single class. E.g. your model might have two columns of class String which are rendered in entirely different ways, maybe once using a JLabel/DefaultRenderer and the other using a JButton to access a more elaborate editor.

JTable with its custom renderers and editors is extremely versatile, but it is also a lot to take in, and there are a lot of things to do wrong. Good luck!

How to Use Tables in The Swing Tutorial is mandatory reading for anyone customising JTables. In particular, read and reread Concepts: Editors and Renderers because it typically takes a while for it to "click". The examples on custom renderers and editors are also very worthwhile.

幼儿园老大 2024-07-12 05:40:11

您想要扩展以创建您自己的行为的类是 DefaultTableModel。 这将使您能够定义自己的行为。 可以在 sun 网站

The class you want to look into extending to create your own behavior is DefaultTableModel. That will allow you to define your own behavior. A decent tutorial can be found on sun's site.

情深缘浅 2024-07-12 05:40:11

javaobby 中的本教程很容易理解。 您引用的在线 Swing Trail for JTable 表明它已被更新。 您是否浏览了整个内容以获取可能更好(不是越新越好)的信息?

This tutorial from the java lobby is easy to follow. The online Swing trail for JTable that you reference indicates that it has been updated. Did you scan through the whole thing for possible better (isn't newer always better) information?

巷雨优美回忆 2024-07-12 05:40:11

如果您尝试使用具有 1 列可编辑的简单 JTable 并且您知道列位置,则可以始终使用默认表模型并重载 isCellEditable 调用。

像这样的东西:

myTable.setModel(new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
    if (column == x) {
        return true;
    } else
        return false;
}
});

并为复选框创建一个渲染器类

MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer

If you are trying to use a simple JTable with 1 column editable and you know the column location you could always use default table model and overload the isCellEditable call.

something like this :

myTable.setModel(new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
    if (column == x) {
        return true;
    } else
        return false;
}
});

And for the check box create a renderer class

MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer
五里雾 2024-07-12 05:40:11

一些有用的类是:

Package javax.swing.table

TableModel - tablemodel 的接口
AbstractTableModel - 扩展的好类,用于使用自定义数据结构创建您自己的表
DefaultTableModel - 可以处理arrays[]Vectors的默认表格模型

要禁用对任何单元格的编辑,您需要覆盖isCellEditable (int row, int col) 方法

Some useful classes are:

Package javax.swing.table :

TableModel - Interface for a tablemodel
AbstractTableModel - Nice class to extend for creating your own table with custom data structures
DefaultTableModel - Default table model which can deal with arrays[] and Vectors

To disable editing on any cell you need to override the isCellEditable(int row, int col) method

沙与沫 2024-07-12 05:40:11

在表模型中,您应该覆盖“isCellEditable”和“setValueAt”函数,如下所示。
第 4 列是可编辑单元格的列。

然后,当您双击单元格并输入内容时,
setValueAt() 将被调用并将值保存到 tableModel 的 DO 字段 col4 中。

public ArrayList<XXXDO> tbmData = new ArrayList<XXXDO>(); //arraylist for data in table

@Override
public boolean isCellEditable(int row, int col) {
    if (col == 4) {
        return true;
    } else {
        return false;
    }
}

@Override
public void setValueAt(Object value, int row, int col) {
    if ((row >= 0) && (row < this.tbmData.size()) && (col >= 0) && (col < this.colNm.length)) {
        if (col == 4) {
            tbmData.get(row).col4= (String) value;
        }
        fireTableCellUpdated(row, col);
    } else {
    }
}

in your table Model, you should override "isCellEditable" and "setValueAt" functions, like below.
Column 4 is the column for editable cells.

Then when you double click the cell and type something,
setValueAt() will be called and save the value to the tableModel's DO,field col4.

public ArrayList<XXXDO> tbmData = new ArrayList<XXXDO>(); //arraylist for data in table

@Override
public boolean isCellEditable(int row, int col) {
    if (col == 4) {
        return true;
    } else {
        return false;
    }
}

@Override
public void setValueAt(Object value, int row, int col) {
    if ((row >= 0) && (row < this.tbmData.size()) && (col >= 0) && (col < this.colNm.length)) {
        if (col == 4) {
            tbmData.get(row).col4= (String) value;
        }
        fireTableCellUpdated(row, col);
    } else {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文