0

If you try out the following code do you also got a list with colors, sports and food as like some default model in the JTree? Is this a bug in Java? Or do one want such as a example? Best regards Fredrik

import java.awt.GraphicsDevice;
import java.awt.MouseInfo;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;

public class TreeTest extends JPanel
{
    private JScrollPane navigateScroll;
    private JTree tree;

    public TreeTest()
    {
        super();
        tree = new JTree();
        navigateScroll = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(navigateScroll);
    }

    public static void main( String[] args )
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame frame = new JFrame("Tree Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                TreeTest treeTest = new TreeTest();
                frame.add(treeTest);
                frame.pack();

                GraphicsDevice screen = MouseInfo.getPointerInfo().getDevice();
                Rectangle r = screen.getDefaultConfiguration().getBounds();
                int x = (r.width - frame.getWidth()) / 2 + r.x;
                int y = (r.height - frame.getHeight()) / 2 + r.y;
                frame.setLocation(x, y);

                frame.setVisible(true);

            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2902165
  • 303
  • 2
  • 14
  • What is the issue, explain in detail. – Sambit Nov 17 '19 at 18:29
  • Or take a look at: https://drive.google.com/open?id=1vNvKtzJcz-ipfwqBJIefy3OHwDUvhiAA – user2902165 Nov 17 '19 at 18:29
  • Per the [JTree API](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JTree.html) calling `new JTree()` in a default manner, without specifying a model or data, will return a JTree with "sample model" data, as you're seeing. To avoid seeing this, pass in a tree model into your JTree constructor. – Hovercraft Full Of Eels Nov 17 '19 at 18:34
  • Thanks alot, I never expected such behavior. But of course a look into the doc might have explaine it. – user2902165 Nov 17 '19 at 19:09

0 Answers0