开发者

Cell editing and "Substance" L&F in JTable

开发者 https://www.devze.com 2023-02-15 00:09 出处:网络
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

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 ide开发者_运维百科as? Thank you in advance


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号