jtree high DPI does not scale

308 views Asked by At

I have a java swing application using JTree.
I recently moved to a UHD monitor, and now the JTree doesn't scale nicely.
I have the feeling it uses the icon to scale the line-height, but have no clue how to influence this myself.
Please be aware that if UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); is being left out, it is unreadably small on the new monitor.
Please see screenshots / simple example code below.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTree;
import javax.swing.UIManager;

public class JTreeTest extends JFrame
{

   private JPanel contentPane;

   /**
    * Launch the application.
    */
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            try {
               // Set System L&F
               UIManager.setLookAndFeel(
                     UIManager.getSystemLookAndFeelClassName());
               JTreeTest frame = new JTreeTest();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * Create the frame.
    */
   public JTreeTest()
   {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 450, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      contentPane.setLayout(new BorderLayout(0, 0));
      setContentPane(contentPane);

      JTree tree = new JTree();
      contentPane.add(tree, BorderLayout.CENTER);
   }

}

normal resolution jtree screenshot:

https://i.stack.imgur.com/n4fUe.png

UHD resolution jtree screenshot:

https://i.stack.imgur.com/hvXMf.png

Thank you.

1

There are 1 answers

0
aterai On

Maybe you need to use JTree#setRowHeight(int)

public void setRowHeight(int rowHeight)

Sets the height of each cell, in pixels. If the specified value is less than or equal to zero the current cell renderer is queried for each row's height.

import java.awt.*;
import javax.swing.*;

public class JTreeTest2 extends JFrame
{
  private JPanel contentPane;

  /**
   * Launch the application.
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          // Set System L&F
          UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
          JFrame frame = new JTreeTest2();
          frame.setVisible(true);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public JTreeTest2()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JTree tree = new JTree();
    tree.setFont(tree.getFont().deriveFont(32f));

    JTree tree2 = new JTree();
    tree2.setFont(tree2.getFont().deriveFont(32f));
    tree2.setRowHeight(32);
    //or: tree2.setRowHeight(0);

    JPanel p = new JPanel(new GridLayout(1, 0));
    p.add(new JScrollPane(tree));
    p.add(new JScrollPane(tree2));

    contentPane.add(p, BorderLayout.CENTER);
    setBounds(100, 100, 450, 300);
  }
}