Mikkel
Mikkel

Reputation: 1811

Add 3 panels to a frame in java with JSplitPane?

Trying to add 3 panels i created to a frame in Java with the JSplitPane. Have tried this with 2 panels, and that worked great, but with 3 it still does not do what i want.

Have read something about making 2 JSplitPanes and put the one in the other, but that does not actually work what i would like it to do.

My code shows that there are 3 panels, but the size are all wrong.. it should be filled out.

My code:

    frame = new JFrame(); // Create a new frame
    frame.setVisible(true); // Makes it visible     
    frame.setSize(900, 500); // Sets size         
    frame.setTitle(""); // Sets title
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); // Sets the window on the center of the screen   

    temp_panel = new JPanel(); // Creates new JPanel
    water_panel = new JPanel(); // Creates new JPanel
    power_panel = new JPanel(); // Creates new JPanel

    temp_panel.setBackground(Color.decode("#2ecc71")); // Sets color
    water_panel.setBackground(Color.decode("#3498db")); // Sets color
    power_panel.setBackground(Color.decode("#f1c40f")); // Sets color

    temp_label = new JLabel("This is Temperature");
    water_label = new JLabel("This is Water consumption");
    power_label = new JLabel("This is Power consumption");

    // Add labels on panel
    temp_panel.add(temp_label);
    water_panel.add(water_label);
    power_panel.add(power_label); 

    JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    JSplitPane splitPaneRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPaneLeft.setLeftComponent( temp_panel );
    splitPaneLeft.setRightComponent( water_panel );
    splitPaneRight.setLeftComponent( splitPaneLeft );
    splitPaneRight.setRightComponent( power_panel );

    splitPaneLeft.setEnabled(false);
    splitPaneLeft.setDividerSize(0);

    splitPaneRight.setEnabled(false);
    splitPaneRight.setDividerSize(0);

    // put splitPaneRight onto a single panel
    JPanel panelSplit = new JPanel();
    panelSplit.add( splitPaneRight );

    frame.add(panelSplit, BorderLayout.CENTER);

It should look like this, but just with 3 panels with 3 different colors instead of 2!

enter image description here

Hope someone can help

Upvotes: 0

Views: 3706

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

If you don't need to change the relative sizes of the components during runtime, don't use a JSplitPane. Instead create a container JPanel that uses GridLayout, say new GridLayout(1, 0) for 1 row and variable number of columns, add your three colored JPanels to the GridLayout-using JPanel, and add this then to the JFrame.

Upvotes: 2

Hans
Hans

Reputation: 2502

you could make one of the panels another JSplitPane, unfortunately there is no other solution for this.

Upvotes: 0

Related Questions