Java Jtable电子表格格式 [英] Java Jtable spreadsheet like format

查看:149
本文介绍了Java Jtable电子表格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现JTable,特别是AbstractTableModel时遇到问题.我可以从JTable创建和显示数据,但是我当前的场景与通常的场景不同.

I have problems regarding implementing JTable, specifically the AbstractTableModel. I can create and show data from JTable but my current scenario is different from my usually scenario.

我想做的是使我的JTable接受用户的输入,我想将其他组件放入JTable

What I want to do is to make my JTable accept an input from user and I want to put other components inside the JTable

让我们说每一行都有一个JComboBoxJTextField和一个JCheckBox,但是当我开始实现JTable模型(AbstractTableModel)时,我不知道如何放置它们我的JTable的组件?

Lets say for every row there is a JComboBox, JTextField and a JCheckBox, however when I start to implement the JTable model (AbstractTableModel) I don't know how can I put those components to my JTable?

由于返回类型AbstractTableModel的方法getValue是对象数据类型.您是否有任何解决此类问题的方法,以及我讨厌使用DefaultTableModel的方式,因为它尽可能缺乏灵活性,所以我非常希望AbstractTableModel :)帮助.

Since the method getValue from AbstractTableModel return type is of object data types. Do you have any workaround for this kind of problems and by the ways I hate using DefaultTableModel since it lacks flexibility as much as possible I want an AbstractTableModel :) help is really appreciated.

推荐答案

您绝对应该从 如何使用表格 并研究示例显示了如何使用JComboBox更新同一行中的其他单元格,而这些

You should definitely start with How to Use Tables and study the examples there. While a JTable ins't a spreadsheet, you can calculate derived values in your table's model. This example shows how to use a JComboBox to update other cells in the same row, and these examples similarly contrast the two TableModel implementations you mention. Finally, you can use an available ScriptEngine to evaluate simple arithmetic expressions. For example, the example below prints 42.0.

ScriptEngine engine = mgr.getEngineByExtension("js");
try {
    System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
    ex.printStackTrace(System.err);
}

这篇关于Java Jtable电子表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆