0

I made a Java swing application. My main class does the whole SwingUtilities.invokeLater(new Runnable() stuff.

My second class where everything is I've used:

JPanel container = (JPanel) getContentPane();

Then added all the bits by calling..

container.add([name of component]

I'd now like to get this entire 'application' into a JSplitPane. Hence I want my application on one side and something else on the right side.

How do I do this?

    public class one{
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            JFrame f = new App("main");
            f.setSize(1920,1080);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
}


public class App extends JFrame {
        SuppressWarnings("empty-statement")
        public App(String title) {
        super(title);
        JPanel container = (JPanel) getContentPane();

        container.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JButton new button = new JButton("new");
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        //ENTER LOTS OF CONSTRAINTS
        container.add(new, c);

        JButton next to button = new JButton("next to");
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 2;
        //ENTER LOTS OF CONSTRAINTS
        container.add(new, c);

        //I HAVE LOTS OF SWING COMPONENTS AND BUTTONS about 30

}

I want all of this in the left of the split pane?

how do I do this?

Jay
  • 2,107
  • 4
  • 20
  • 24
  • 2
    One way is to put content into two containers (e.g. `JPanel`) and add them to a split-pane, then add the split pane to the content pane. For better help sooner, post an [SSCCE](http://sscce.org/) of current code (rather than code snippets & descriptions). – Andrew Thompson Nov 19 '12 at 01:00
  • Is that better? I just wrote up some mock code to demo my point. – Jay Nov 19 '12 at 01:11
  • I can't see any reason why you should create that `one` class just to put main method inside. – Branislav Lazic Nov 19 '12 at 01:13
  • the 'one' class is there to multithread the application...I think – Jay Nov 19 '12 at 01:14
  • 1
    See also this [example](http://stackoverflow.com/a/10110232/230513). – trashgod Nov 19 '12 at 01:27

1 Answers1

5
  1. Don't extend from JFrame, you're not adding any functionality to, instead, move all you application components and logic to a separate JPanel
  2. Create an instance of a JSplitPane, add the "main" panel to it, add you secondary pane to it
  3. Create an instance of a JFrame, add the split pane to it...

UPDATED

enter image description here

public class TestSplitPane {

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

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

                JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                pane.setLeftComponent(new MainPane());
                pane.setRightComponent(new JLabel("On the right"));

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

    public class MainPane extends JPanel {

        public MainPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            JButton newButton = new JButton("new");
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 2;
            //ENTER LOTS OF CONSTRAINTS
            add(newButton, c);

            JButton next = new JButton("next to");
            c.gridx = 1;
            c.gridy = 1;
            c.gridwidth = 2;
            //ENTER LOTS OF CONSTRAINTS
            add(next, c);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Any chance you can type our a quick SSCCE for me please? – Jay Nov 19 '12 at 01:34
  • *"I just applied the methods to my code and it's worked!"* That makes a change, I mean, of course it did ;) – MadProgrammer Nov 19 '12 at 02:08
  • =] Any reason to why; when I grade the split in the middle to the left it won't budge and resize all the swing components? – Jay Nov 19 '12 at 02:23
  • The split pane may be restricted by the panels preferred/min size – MadProgrammer Nov 19 '12 at 02:27
  • Well If I resize the whole window it scales perfectly but refuses to scale in the pane. Really not sure =S – Jay Nov 19 '12 at 02:32
  • This has to do with the layout manager begin used to layout the components. `GridBagLayout` will "size" components to there preferred size where space allows, but won't generally (unless you specify otherwise), grow the component – MadProgrammer Nov 19 '12 at 02:49
  • It is 'growing' the component though rather well. It's just refusing to shrink it! – Jay Nov 19 '12 at 02:52