1

I want to use two JPanels in one JFrame, with an invisible horizontal line between them. I played a little bit and got this:

public class Application { 

    public static void main(String[] args)
    {   

         JFrame jframe = new JFrame();
         jframe.setSize(500,700);
         jframe.setVisible(true);
         jframe.setTitle("Title");
         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jframe.setResizable(false);


         JSplitPane splitPane = new JSplitPane();
         JPanel leftPanel = new JPanel();
         JPanel rightPanel = new JPanel();
         splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);  
         splitPane.setDividerLocation(250);                    
         splitPane.setLeftComponent(leftPanel);                  
         splitPane.setRightComponent(rightPanel); 
         jframe.add(splitPane);


    }
}

Now, the first problem is how can I turn off the "resizability" of the Line between the panels? And how do I make it "invisible"? Maybe use something else than split pane?

Second of all, how do can I work with only one side of the JPanel? (I am working on an application that lets you draw a circle on the left hand side).

This seems like an easy question but I am relatively new to Java.

MassU
  • 51
  • 1
  • 2
  • 5

2 Answers2

4

As said before in a comment by @MadProgrammer you can use BorderLayout or GridBagLayout but as you're placing the "split" line right in the middle of both panels you could use GridLayout which will make both panels be of the same size no matter if the window is resized.

I didn't tried with GridBagLayout but I did an example on how you could achieve this pane separation without using a JSplitPane.

With GridLayout all you need to do is add the elements to the left pane (in my example I used a JLabel to differentiate them) while in BorderLayout you need to specify that the panel where you'll be painting the circle to be aligned to the left (WEST constant) as I did.

However if you use BorderLayout approach and add text or elements to the right pane, they will be aligned to the right, you can fix it by "boxing" the elements in another pane with a different Layout.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Application {

    private JFrame frame;
    private JPanel containerPane;
    private JPanel topPane;
    private JPanel bottomPane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Application().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("Example of 2 panels");
        containerPane = new JPanel();
        topPane = new JPanel();
        bottomPane = new JPanel();

        containerPane.setLayout(new GridLayout(2, 1));
        topPane.setLayout(new GridLayout(1, 2));
        bottomPane.setLayout(new BorderLayout());

        topPane.add(new JLabel("Left side"));
        topPane.add(new JLabel("Right side"));

        bottomPane.add(new JLabel("Left side"), BorderLayout.WEST);
        bottomPane.add(new JLabel("Right side"), BorderLayout.EAST);

        topPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using GridLayout"));
        bottomPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using BorderLayout"));

        containerPane.add(topPane);
        containerPane.add(bottomPane);

        frame.add(containerPane);

//      frame.pack();
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I didn't call pack() in this example because the size of both panels (or JLabels in this case was not tall enough to show the difference:

Using pack():

enter image description here

Calling setSize():

enter image description here


Additional tips

  1. Don't forget to place your program on the Event Dispatch Thread (EDT), I did it by writing these lines on the main method:

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Application().createAndShowGui();
        }
    });
    
  2. Don't place all your code on the constructor, otherwise it will be hard to maintain

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 2
    My money's on this answer – Hovercraft Full Of Eels Jan 17 '17 at 00:02
  • Wow, thanks so much. But now my Applications seems quite buggy. Either Circles aren't displayed at all or at complete different locations(and on the whole frame). I edited my post with the new classes. – MassU Jan 17 '17 at 10:57
  • @MassU this question was related on how to place two JPanels on the same JFrame, which was answered in this answer, please ask another question with your new code and the problems you have. Also don't forget to accept the answer – Frakcool Jan 17 '17 at 13:43
2

It looks like you can use GridLayout to do this. Here is what i think,

public class Application {
    public static void main(String[] args) {

        JFrame jframe = new JFrame();
        jframe.setTitle("Title");
        jframe.setResizable(false);

        //This creates one row and two equally divided columns
        GridLayout gridLayout = new GridLayout(0, 2);
        jframe.setLayout(gridLayout);
        gridLayout.layoutContainer(jframe);

        JPanel leftPanel = new JPanel();
        leftPanel.add(new Label("Left side"));
        jframe.add(leftPanel);

        JPanel rightPanel = new JPanel();
        rightPanel.add(new Label("Right side"));
        jframe.add(rightPanel);

        jframe.setSize(800, 500);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

Here is how it looks: enter image description here

The panels will not resize as well as there is no line visible that seprates them.

hashmap
  • 400
  • 1
  • 9
  • Thank you, that is what I was looking for. But It seems this task isn't as easy as I thought. How can I work with the left side now? I have a class DrawCircle in which one can draw a Circle with a Mouse Click. leftPanel.add(c) doesn't work. – MassU Jan 16 '17 at 23:24
  • 2
    @MassU: understand that this answer has some code that doesn't follow Swing best practices including calling `setSize(...)` on a component and also calling `setVisible(true)` on the JFrame *before* adding all components to the GUI, something that can prevent some components from showing. Please strongly consider Frakcool's answer. – Hovercraft Full Of Eels Jan 17 '17 at 00:03
  • Definitely, agree @Hovercraft Full Of Eels . My code does not follow best format. I didn't edited the MassU's code. – hashmap Jan 17 '17 at 00:11