细胞编辑和“物质” JTable 中的 L&F

发布于 2024-10-20 17:47:45 字数 407 浏览 2 评论 0原文

这是我认为更好的二重奏问题 一起回答。

1) 如何使整个 JTable 不可编辑? (除了放有没有其他办法 它进入一个循环并使用 isCellEditable 方法?)

2)现在更棘手的一个;我正在使用 DefaultTableCellRenderer() ,我已覆盖它以更改字体等一些内容。问题是我也在我的应用程序中使用物质 L&F 并且如果我使用:

table.setDefaultRenderer(Object.class, renderer);

然后我得到了新字体,但整个 JTable 的 L&F 将会消失。

坦率地说,我追求物质皮肤的每隔一行的阴影着色效果,我不想失去它,但同时我也想使用我的“扩展渲染器”......

有什么想法吗? 先感谢您

This is a duet of questions which I think are better
answered together.

1) How do I make my entire JTable uneditable? (Is there any other way except putting
it into a loop and using the isCellEditable method?)

2)Now the more tricky one; I am using a DefaultTableCellRenderer() which I have overriden in order to change a couple of stuff like fonts etc. The problem is that I am also using the substance L&F in my app and if I use:

table.setDefaultRenderer(Object.class, renderer);

then I get my new fonts but the L&F will go away for the entire JTable.

Frankly I am after the shaded coloring effect on every other row of the substance skin and I don't want to lose it but at the same I would also like to use my "extended renderer"...

Any ideas?
Thank you in advance

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

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

发布评论

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

评论(2

娇纵 2024-10-27 17:47:45

对于 1),只需让您的 TableModel 始终从 isCellEditable() 方法。如果您只是使用 DefaultTableModel 的普通实例,请扩展它以覆盖此方法。

对于 2),您也许能够更改 LookAndFeel UIDefaults 中的 JTable 字体。 UIDefaults 类包含许多字体、颜色、间距和其他外观设置。

For 1), just have your TableModel always return false from the isCellEditable() method. If you are just using a plain instance of DefaultTableModel, extend it in order to override this method.

For 2), you might be able to change the JTable fonts in the LookAndFeel UIDefaults. The UIDefaults class contains many font, color, spacing, and other settings for the look and feel.

猫烠⑼条掵仅有一顆心 2024-10-27 17:47:45

有点晚了,但是

1)我假设您使用的是 DefaultTableModel?

JTable table = null;
Vector dataVector = null;
Vector columnNames = null;
table.setModel(new DefaultTableModel(dataVector, columnNames) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
});

一般来说,在 Java 中扩展单个方法并不是一个大问题,我不能说我理解你为什么不想这样做。此外,一般来说,组件不应该负责决定可编辑性。在具有从 RSS 提要中获取数据的表模型的 JTable 上 setEditable(true) 意味着什么?您无法编辑提要,并且只有模特知道。不过,内存中的文档始终是可编辑的,因此它适用于 JTextComponent 及其子类(JTextArea、JTextPane、JEditorPane、JTextField)。

2) 不扩展 DefaultTableCellRenderer,而是扩展 SubstanceDefaultTableCellRenderer。那应该可以解决问题。也就是说,Substance 希望您从 getTableCellRendererComponent 返回 SubstanceDefaultTableCellRenderer 的子类。如果您只更改字体和字体颜色,这应该没有问题。只需扩展 SubstanceDefaultTableCellRenderer 并覆盖 getTableCellRendererComponent 即可使其看起来像


class MyTableRenderer extends SubstanceDefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        JLabel label = (JLabel)super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
        label.setFont(label.getFont().deriveFont(Font.BOLD, 14));
        return label;
    }
}

“这是特定于 Substance 的”,我不能说我非常喜欢它。这给我带来了一些头痛,因为我有很多自定义渲染器,我非常希望它们不依赖于 Substance。

Bit late but

1) I'm assuming you're using a DefaultTableModel?

JTable table = null;
Vector dataVector = null;
Vector columnNames = null;
table.setModel(new DefaultTableModel(dataVector, columnNames) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
});

Generally, it's not a big issue extending on single methods in Java and I can't say I understand why you don't want to do it. Moreover, in general, a component shouldn't be responsible for deciding editability. What would it mean to setEditable(true) on a JTable that has a table model that fetches data from, say, an RSS-feed? You can't edit the feed and only the model knows it. An in-memory document is always editable though, so it works for JTextComponent and its sub-classes (JTextArea, JTextPane, JEditorPane, JTextField).

2) Instead of extending DefaultTableCellRenderer, extend SubstanceDefaultTableCellRenderer. That should do the trick. That is, Substance want you to return a sub-class of SubstanceDefaultTableCellRenderer from the getTableCellRendererComponent. This should be no problem if you only change font and font color. Just extend SubstanceDefaultTableCellRenderer and override getTableCellRendererComponent to look something like


class MyTableRenderer extends SubstanceDefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        JLabel label = (JLabel)super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
        label.setFont(label.getFont().deriveFont(Font.BOLD, 14));
        return label;
    }
}

This is Substance-specific and I can't say I like it very much. Has caused a few headaches for me since I have a lot of custom renderers that I would very much like not to be dependent on Substance.

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