Creating JSplitPane with 3 panels - java

I would like to create a window, with 3 jPanels, separated by splitPane-s. The left, and the right one should be resizable by the user, and the one in the middle should fill the remaining space.
I've created it, but if I move the first splitPane, then the second one is also moving. And I'm not sure, if I use the best method for what I want.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel centerPanel2 = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerPanel2, rightPanel);
centerPanel.setLayout(new BorderLayout());
this.add(sp, BorderLayout.CENTER);
centerPanel.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
}

What you're doing looks pretty weird to me i.e adding the centerPanel to the split pane, then adding the split pane to the centerPane. Not sure, but I think that the latter negates the former.
All you need to do is add the first split pane to the second split pane.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.BLUE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.CYAN);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.GREEN);
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, rightPanel);
this.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MyWindow();
}
});
}
}

Related

Two Split pane place on a tabpane

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
public class MenuPage extends JFrame{
final static int extraWindowWidth = 100;
JSplitPane jSplitPane, jSplitPane2,jSplitPane3,jSplitPane4;
JPanel jPanel2a, jPanel2b, jPanel3;
public static JFrame frame;
public MenuPage(){
}
private final JList<String> list1=new JList<>(new String[]{"A","B"});
private final JList<String> list2 = new JList<>();
private final List<DefaultListModel> models = new ArrayList<>();
public void addComponentToPane(Container pane) {
JTabbedPane tabbedPane = new JTabbedPane();
// Create the "cards".
JPanel card1 = new JPanel() {
// Make the panel wider than it really needs, so
// the window's wide enough for the tabs to stay
// in one row.
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
return size;
}
};
DefaultListModel<String> model1 = new DefaultListModel<>();
model1.addElement("A1");
model1.addElement("A2");
model1.addElement("A3");
models.add(model1);
DefaultListModel<String> model2 = new DefaultListModel<>();
model2.addElement("B1");
model2.addElement("B2");
models.add(model2);
list2.setModel(model1);
list1.addListSelectionListener((ListSelectionEvent e) -> {
if (!e.getValueIsAdjusting()) {
list2.setModel(models.get(list1.getSelectedIndex()));
}
});
jPanel2a = new JPanel();
jPanel2b = new JPanel();
jPanel3 = new JPanel();
jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);
jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);
jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);
jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);
JPanel card3 = new JPanel();
JPanel card4 = new JPanel();
JPanel card5 = new JPanel();
JPanel card6 = new JPanel();
JPanel card7 = new JPanel();
JPanel card8 = new JPanel();
card3.add(jSplitPane3,jSplitPane4);
tabbedPane.addTab("Main", jSplitPane2);
tabbedPane.addTab("Leagues",jSplitPane4);
tabbedPane.addTab("Teams",card3 );
tabbedPane.addTab("Ratios", card4);
tabbedPane.addTab("Competitions", card5);
tabbedPane.addTab("Analyze", card6);
tabbedPane.addTab("Help", card7);
tabbedPane.addTab("About", card8);
pane.add(tabbedPane, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
public static void createAndShowGUI() {
// Create and set up the window.
frame = new JFrame("BetAnalyzer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(594, 310);
// Create and set up the content pane.
MenuPage demo = new MenuPage();
demo.addComponentToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
frame.setBounds(100, 100, 635, 251);
}
}
I want the list1, list2, and jpanel3 which takes part 2 JSplitPane to be in the league tab There is something wrong in my code. Just jSplitPane3 or jSplitPane4 could be appear in the layout.
Also, I tried to add two JSplitPane into Box then I put the Box in tabbedpane but this's not working neither.
Your code has several issues.
do not inherit without reason
Your class inherits from JFrame but its does not extend its behavior (it does not override a public or protected method of JFrame).
Components can only be added once
In contrast to the other points this is not an issue per se but part of your problem.
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
// ...
card3.add(jSplitPane3,jSplitPane4);
When the second statement is executed the jSplitPane3 is removed from the jSplitPane4 and only shown as content of card3.
use of standard layouts
The (dummy) content of your panels is rather small so that their preferedSize is less then the available space in the parent container.
The JPanels default Layout is FlowLayout which reduces each of its components to its preferedSize.
You should set the Layout of your JPanels to BorderLayout which allows its content to stretch out over the complete space.
In turn you cannot use JPanels varag method .add(Component, component)
anymore.
here are the relevant changes:
jPanel2a = new JPanel(new BorderLayout());
jPanel2b = new JPanel(new BorderLayout());
jPanel3 = new JPanel(new BorderLayout());
jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);
jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jSplitPane3, jPanel3);
// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);
// jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
//
// jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);
//
// jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);
JPanel card3 = new JPanel(new BorderLayout());
JPanel card4 = new JPanel(new BorderLayout());
JPanel card5 = new JPanel(new BorderLayout());
JPanel card6 = new JPanel(new BorderLayout());
JPanel card7 = new JPanel(new BorderLayout());
JPanel card8 = new JPanel(new BorderLayout());
// card3.add(jSplitPane3);
card3.add(jSplitPane4,BorderLayout.SOUTH);
tabbedPane.addTab("Main", jSplitPane2);

Adding a JLabel to a JFrame that is using BoxLayout

I am creating a JFrame that contains a JPanel with a grid of buttons. Everything works fine but I then want to add a JLabel above the panel of buttons but the label never appears. It does appear if I don't use BoxLayout, though. Any help is appreciated.
The first code part below is my JFrame class:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
Panel panel = new Panel();
Dimension frameDim = new Dimension(1000, 1000);
Dimension labelDim = new Dimension(100, 20);
Box box = new Box(BoxLayout.Y_AXIS);
JLabel label = new JLabel("Tic Tac Toe");
JPanel pane = new JPanel();
public Frame() {
pane.add(label);
pane.setPreferredSize(labelDim);
pane.setMinimumSize(labelDim);
add(pane);
box.add(Box.createVerticalGlue());
box.add(panel);
box.add(Box.createVerticalGlue());
add(box);
setTitle("Tic Tac Toe");
setSize(frameDim);
setMinimumSize(frameDim);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
The code below is my JPanel class with buttons:
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panel extends JPanel {
private static final long serialVersionUID = 2L;
private int i;
JButton[] button = new JButton[9];
GridLayout layout = new GridLayout(3, 3);
Dimension dim = new Dimension(500, 500);
public Panel() {
for (i = 0; i<9; i++) {
button[i] = new JButton();
add(button[i]);
}
setPreferredSize(dim);
setMinimumSize(dim);
setMaximumSize(dim);
setLayout(layout);
}
}
The last code part below is the Main class:
public class RunGame {
public static void main(String[] args) {
new Frame();
}
}
add(box);
This is adding the label direct to the JFrame, the content pane of which is laid out using a BorderLayout. The default when adding to a component to a border layout without any constraint is the CENTER, which can only display a single component. To fix it use:
pane.add(box);
you can use the BorderLayout when adding your label into Pane into the Frame and remove that box thing as below
public Frame()
{
//create label and add it to the frame
JLabel label = new JLabel("Tic Tac Toe");
label.setHorizontalAlignment( JLabel.CENTER );
add(label, BorderLayout.NORTH);
//create buttonsPanel and add it to the frame
JPanel buttons = new JPanel();
buttons.setLayout( new GridLayout(3, 3));
for (int i = 0; i < 9; i++)
{
buttons.add(new JButton(""+i));
}
add(buttons, BorderLayout.CENTER);
//setup the title, other properties for the frame etc..
setTitle("Tic Tac Toe");
setSize(1000, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
hope this helps better :)

Java JScrollPane in JSplitPane does not work

I'm having this piece of code, I want to add a scrollbar for CanvasBoard, but it does not show the scroll bar. CanvasBoard and PointCanvas extends JPanel.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cb = new JPanel();
JScrollPane scrollPane = new JScrollPane(cb);
scrollPane.setPreferredSize(new Dimension(2000, 600));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane,
new JPanel());
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
Dimension minimumSize = new Dimension(600, 600);
cb.setMinimumSize(minimumSize);
frame.add(splitPane);
frame.setSize(1200, 600);
frame.setVisible(true);
}
Do you have any idea why?Thanks!
The default for a JScrollPane is to have the scroll bars show up when needed.
I changed a few things in your example and added commands to make the scroll bars visible.
Here's your Swing layout.
And here's the modified code.
package com.ggl.testing;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class ScrollPaneTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ScrollPaneTest());
}
#Override
public void run() {
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cb = new JPanel();
JScrollPane scrollPane = new JScrollPane(cb);
scrollPane
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(2000, 600));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPane, new JPanel());
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
Dimension minimumSize = new Dimension(600, 600);
cb.setMinimumSize(minimumSize);
frame.add(splitPane);
frame.setSize(1200, 600);
frame.setVisible(true);
}
}

I need to make my JPanel resize dynamically as new components are being added to it

I need to let users add more text fields to my JFrame so once the size of the frame has exceeded its original value a scroll pane would step in. Since I cannot add JScrollPane to JFrame in order to enable scrolling I decided to put the JPanel on the JFrame and pass the JPanel object into the JScrollPane constructor. Scrolling now works fine but only until it has reached the borders of the JPanel. The thing is the size of JPanel stays as is and is not stretching dynamically. What happens is the buttons in my code are using up all the space of the JPanel being the size of 300x300 but what I want to do is have JPanel stretch once these controls have used up its original space. Please advise.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
public class Skrol {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400,400));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
for(int i=0;i<100;i++)
{
JButton b = new JButton("Button "+i);
p.add(b);
}
f.add(jsp);
f.setSize(new Dimension(600,600));
f.setLocation(300, 300);
f.setVisible(true);
}
}
I changed the Layout in your JPanel to GridLayout, so the Size of it is just handeld by the Layoutmanager depending on the components on the panel.
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(0, 5));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
for (int i = 0; i < 100; i++) {
JButton b = new JButton("Button " + i);
p.add(b);
}
f.add(jsp, BorderLayout.CENTER);
f.setLocation(300, 300);
f.setVisible(true);
f.pack();
Choose the Miglayout option under Layouts. i found this to be the easiest way
https://i.stack.imgur.com/XKHVu.png

Java: vertical alignment within JPanel

I am trying to vertically align (center) both JLabels inside one JPanel.
JPanel panel = new JPanel();
panel.setPreferredSize(size);
JLabel label1 = new JLabel(icon);
JLabel label2 = new JLabel("text");
panel.add(label1);
panel.add(label2);
I have tried using setAligmentY() with no success. Both labels always appear on the top of JPanel.
UPD: Labels should be located next to each other like using FlowLayout, but in the middle of the JPanel.
Use a GridBagLayout with the default constraints. Here is a small demo code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestVerticalAlignement {
protected void initUI() {
final JFrame frame = new JFrame();
frame.setTitle("Test vertical alignement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
panel.add(label1, gbc);
panel.add(label2, gbc);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestVerticalAlignement().initUI();
}
});
}
}
you can see this answer:
https://stackoverflow.com/a/18073909/189411
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Use gridlayout, simple.
That should work.
Consider my following example:
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
/*
<applet code=AJ07 width=450 height=450>
</applet>
*/
public class AJ07 extends JApplet{
Container c=null;
public void init(){
JPanel pTop=new JPanel();
JPanel pLeft=new JPanel();
JPanel pCenter=new JPanel();
JPanel pProperties=new JPanel();
pLeft.setLayout(new GridLayout(20,1));
c=this.getContentPane();
JButton bNew=new JButton("New");
pTop.add(bNew);
JButton bOpen=new JButton("Open");
pTop.add(bOpen);
JButton bSave=new JButton("Save");
pTop.add(bSave);
JButton bSaveAll=new JButton("Save All");
pTop.add(bSaveAll);
JButton bRun=new JButton("Run");
pTop.add(bRun);
JButton bStop=new JButton("Stop");
pTop.add(bStop);
JButton bPause=new JButton("Pause");
pTop.add(bPause);
JButton bText=new JButton("TextBox");
pLeft.add(bText);
JButton bButton=new JButton("Button");
pLeft.add(bButton);
pProperties.setLayout(new GridLayout(20,1));
pProperties.add(new Label("BackColor"));
pProperties.add(new Label("ForeColor"));
c.add(new TextArea(),BorderLayout.CENTER);
c.add(pTop,BorderLayout.NORTH);
c.add(pLeft,BorderLayout.WEST);
c.add(new Label("Project Loaded Successfully!"),BorderLayout.SOUTH);
c.add(pProperties,BorderLayout.EAST);
//c.add(pCenter,BorderLayout.CENTER);
}
}
for which the output is as follows:

Categories