0

I'm trying to make a program thats based upon 5 Jsplitpanes and internal components, but they don't seem to get along well. Since when I run the application all the split panes are concentrated on the upper left corner

enter image description here

The code is really simple:

projectExplorer = new ProjectExplorer();
    editorExplorer = new EditorExplorer();
    favDownload = new FavDownload();
    favDownloadExplorer = new FavDownloadExplorer();
    //Define the listeners
    addComponentListener(new ResizeListener());

    horTopPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    horTopPane.setLeftComponent(projectExplorer);
    horTopPane.setRightComponent(editorExplorer);

    horBotPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    horBotPane.setLeftComponent(favDownload);
    horBotPane.setRightComponent(favDownloadExplorer);

    verticalPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    verticalPane.setTopComponent(horTopPane);
    verticalPane.setBottomComponent(horBotPane);


    setContentPane(verticalPane);
    new Timer().schedule(new TimerTask() {          
        @Override
        public void run() {
            resizeComponents();
        }
    }, 100);

the resizeComponents method is

public void resizeComponents(){
    int width = getWidth();
    int height = getHeight();
    horTopPane.setMinimumSize(new Dimension(width, (height/2)+(height/4)));
    horTopPane.setSize(horTopPane.getMinimumSize());
    horBotPane.setMinimumSize(new Dimension(width, (height/8)));
    horBotPane.setSize(horBotPane.getMinimumSize());
    projectExplorer.setMinimumSize(new Dimension(width/8, horTopPane.getHeight()));
    projectExplorer.setSize(projectExplorer.getMinimumSize());
    editorExplorer.setMinimumSize(new Dimension(width/2, horTopPane.getHeight()));
    editorExplorer.setSize(editorExplorer.getMinimumSize());
    favDownload.setMinimumSize(new Dimension(width/8, horBotPane.getHeight()));
    favDownload.setSize(favDownload.getMinimumSize());
    favDownloadExplorer.setMinimumSize(new Dimension(width/2, horBotPane.getHeight()));
    favDownloadExplorer.setSize(favDownloadExplorer.getMinimumSize());
}

it started just as setSize, but for some reason it didn't resize them at all, then I putted setMinimumSize and it didn't resize it either, but when i move the elements a bit on the program they all go to its minimum size, am I missing something?

I've already made a few Java GUI programs, but this one just doesn't want to work properly.

What did I do wrong?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Debels
  • 167
  • 1
  • 3
  • 15
  • Don't mess with the component sizing hints...Don't modify the UI from outside the context of the Event Dispatching Thread – MadProgrammer Jun 24 '14 at 02:55
  • I messed with the sizing since when I didn't have that it was the same as it shows now :/ and I wait the 0.1 seconds because if i do it from the start the width and height is 0 oddly, any suggestions? – Debels Jun 24 '14 at 03:00
  • All components have a zero size until the layout manger is invoked. The layout manager is invoked when you do a pack() or the frame is made visible. It is the layout managers job to set the size/location of components. – camickr Jun 24 '14 at 03:03
  • What does the `ResizeListener` do? – MadProgrammer Jun 24 '14 at 03:05
  • Override the `getPreferredSize` method of your custom components and return a "default" size – MadProgrammer Jun 24 '14 at 03:06

1 Answers1

1

To start with, don't mess with setMinimum/Maximum/PreferredSize, have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details

Next, don't update the state of UI components from any thread other than the Event Dispatching Thread, Swing is not thread safe. java.util.Timer will trigger event notification within it's own thread context.

Next, consider overriding (at least), getPreferredSize from your custom components and return a default value, for example...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SplitPaneTest {

    public static void main(String[] args) {
        new SplitPaneTest();
    }

    public SplitPaneTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final ProjectExplorer projectExplorer;
        private final EditorExplorer editorExplorer;
        private final FavDownload favDownload;
        private final FavDownloadExplorer favDownloadExplorer;
        private final JSplitPane horTopPane;
        private final JSplitPane horBotPane;
        private final JSplitPane verticalPane;

        public TestPane() {
            projectExplorer = new ProjectExplorer();
            editorExplorer = new EditorExplorer();
            favDownload = new FavDownload();
            favDownloadExplorer = new FavDownloadExplorer();

            horTopPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            horTopPane.setLeftComponent(projectExplorer);
            horTopPane.setRightComponent(editorExplorer);

            horBotPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            horBotPane.setLeftComponent(favDownload);
            horBotPane.setRightComponent(favDownloadExplorer);

            verticalPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            verticalPane.setTopComponent(horTopPane);
            verticalPane.setBottomComponent(horBotPane);

            setLayout(new BorderLayout());
            add(verticalPane);
        }
    }

    protected abstract class AbstractPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class ProjectExplorer extends AbstractPane {

        public ProjectExplorer() {
            setBackground(Color.RED);
        }

    }

    public class EditorExplorer extends AbstractPane {

        public EditorExplorer() {
            setBackground(Color.BLUE);
        }

    }

    public class FavDownload extends AbstractPane {

        public FavDownload() {
            setBackground(Color.MAGENTA);
        }

    }

    public class FavDownloadExplorer extends AbstractPane {

        public FavDownloadExplorer() {
            setBackground(Color.CYAN);
        }

    }

}

If you have to modify the divider's location consider using JSplitSpane#setDividerLocation(double) or JSplitSpane#setDividerLocation(int)

Take a look at How to Use Split Panes for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366