Hej guys!

I have a little problem with layouting Swing components. I just need to add JTree: tree to the UPPER corner of JScrollPane: scrollpanesearch, and 4 buttons to the BOTTOM corner. What I have now is JTree: tree on the LEFT, and 4 buttons on the RIGHT (see Figure attached to this post).

JScrollPane scrollpanesearch = new JScrollPane(new TreeEditTest(), ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollpanesearch.setBounds(20, 40, 300, 400);
public TreeEditTest() {    
        // construct tree
        TreeNode root = makeSampleTree();
        model = new DefaultTreeModel(root);
        tree = new JTree(model);
        tree.setEditable(false);
 
        // add scroll pane with tree to content pane
        JScrollPane scrollPane = new JScrollPane(tree,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scrollPane);
 
        // make button panel
        JPanel panel = new JPanel();
        addSiblingButton = new JButton("Add Sibling");
        addSiblingButton.addActionListener(this);
        panel.add(addSiblingButton);
        addChildButton = new JButton("Add Child");
        addChildButton.addActionListener(this);
        panel.add(addChildButton);
        deleteButton = new JButton("Delete");
        deleteButton.addActionListener(this);
        panel.add(deleteButton);
        editButton = new JButton("Edit");
        editButton.addActionListener(this);
        panel.add(editButton);
        add(panel);
    }

Any advises will be appreciated. Plz help.

Edited 6 Years Ago by LianaN: n/a

Attachments folder_tree.png 5.8 KB

You can probably use BorderLayout for that with your tree in the center and buttons at south.

There are plenty of other layout options if you want to gain more control over positioning. Just take a look at the contents listing at the left side of that tutorial link.

The default layout option for JPanels (which you use in your class) is FlowLayout with a default flow of left to right, so, as Ezzaral says, use a different layout.

Edited 6 Years Ago by masijade: n/a

If you need a very precise layout, you can do setLayout(null) and then use the setBounds method for coordinate positioning.

If you need a very precise layout, you can do setLayout(null) and then use the setBounds method for coordinate positioning.

No, that will not give a "very precise layout". That will create a very brittle layout that will "break" as soon as the font is changed, or the app is run under a different display size, text size, os, whatever, as most of the components sizes will alter, at least slightly, under those conditions. At the very least it will take alot of work by the programmer to take all the variables into consideration, and resizing will be a real pain. The layout managers manage all of this for you. The "null" layout is not an option, except in very specific instances.

Then I have another question. How could I define the size of components, such as JPanel without using setBounds or setSize in order to make my application good looking under different display sizes and different operation systems?

No, that will not give a "very precise layout". That will create a very brittle layout that will "break" as soon as the font is changed, or the app is run under a different display size, text size, os, whatever, as most of the components sizes will alter, at least slightly, under those conditions. At the very least it will take alot of work by the programmer to take all the variables into consideration, and resizing will be a real pain. The layout managers manage all of this for you. The "null" layout is not an option, except in very specific instances.

By arranging them properly (GridBagLayout with proper constraint settings and properly designed components will let you do nearly anything) and letting the layout manager do it.

This question has already been answered. Start a new discussion instead.