0

With increasing age I have come to use Ctrl-+ more and more in OSX to increase font size in all kinds of windows (browser, editors etc). However, on JTable in custom apps it doesn't work of course.

How shall I add this feature to my own Swing JTables?

I would like it to increase/decrease whatever font I already have in the JTable, and also adjust column widths appropriately.

I have a general idea how to code it (add hotkey, take current Font, increase size, set new Font, invalidate), but not how to apply it to the many tables I already have. Also not how to adjust column width properly.

Shall I create a new base class or add some kind of plugin? Other ideas?

user1803551
  • 12,965
  • 5
  • 47
  • 74
Peter Andersson
  • 1,947
  • 3
  • 28
  • 44
  • There are 2 issues here. One is how to code this function, the other is how to implement it backwards. I suggest you post the code of what you have tried for the first issue. – user1803551 Dec 22 '15 at 09:32
  • 1
    If you want something globally, you can alter the fonts handled by the UIManager, if you want something more localised, you can use JLayer (although this could also be applied globally) – MadProgrammer Dec 22 '15 at 09:43
  • Suppose you increase the font and the cell grow, now you decrease the font, what do you want to happen? Shrink it back? By how much? – user1803551 Dec 22 '15 at 10:02
  • On Mac OS X, `System Preferences > Accessibility > Zoom > Scroll Gesture`. – trashgod Dec 22 '15 at 10:59
  • I would create a new class extending the JTable with the features you want implemented in that. Then just use that class to replace the existing JTable implementations – NoddySevens Dec 23 '15 at 00:13

1 Answers1

1

This solution is a bit crude but it demonstrates how to implement zooming in and out with ctrl + and ctrl -:

public class FontTable extends JPanel {

    JTable table;

    FontTable() {

        Object[][] data = {{"aaa", "122", "_____"}, {",,,,,", ",,,", ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"}};
        Object[] names = {"C1", "C2", "C3"};
        table = new JTable(data, names);

        InputMap im = table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        im.put(KeyStroke.getKeyStroke('=', InputEvent.CTRL_DOWN_MASK), "zoom in");
        im.put(KeyStroke.getKeyStroke('-', InputEvent.CTRL_DOWN_MASK), "zoom out");
        table.getActionMap().put("zoom in", new ZoomAction(true));
        table.getActionMap().put("zoom out", new ZoomAction(false));

        setLayout(new BorderLayout());
        add(new JScrollPane(table));
    }

    class ZoomAction extends AbstractAction {

        boolean in;

        ZoomAction(boolean in) {

            this.in = in;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            Font oldFont = table.getFont();
            float size = oldFont.getSize() + (in ? +2 : -2);
            table.setFont(oldFont.deriveFont(size));

            for (int row = 0; row < table.getRowCount(); row++) {
                int rowHeight = table.getRowHeight(row);

                for (int col = 0; col < table.getColumnCount(); col++) {
                    Component comp = table.prepareRenderer(table.getCellRenderer(row, col), row, col);
                    TableColumn column = table.getColumnModel().getColumn(col);
                    int colWidth = column.getWidth();

                    rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
                    colWidth = comp.getPreferredSize().width;

                    column.setPreferredWidth(colWidth);
                }
                table.setRowHeight(row, rowHeight);
            }
        }
    }
}

I used Camick's answer as a reference. Put it in some frame to test.

As for implementing it backwards, you can either subclass JTable and add this new functionality and then replace your existing ones with the new class, or do something more tricky with nested classes in an interface and implement that interface.

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Great solution. It's actually very close the one I coded myself yesterday. I just bound it to VK_ADD and VK_SUBTRACT instead. Also, I used TableColumnAdjuster since it includes width of headers. – Peter Andersson Dec 23 '15 at 11:43
  • @PeterAndersson Yes, as I said, it's a bit crude and there are certainly several points you can improve upon, one being the headers. Another is fine-tuning the widths and heights because larger fonts can take a lot of extra space for little benefit. – user1803551 Dec 23 '15 at 11:48