Here is my code to add to component (JTextArea and JList) to a panel and put it on the frame. Can I divide half/half by BorderLayout?
If yes why mine looks messy one stays up one down?
What is the other alternative?
Regards,
Bernard
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class SimpleBorder {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border etched = (Border) BorderFactory.createEtchedBorder();
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol = new JScrollPane(text);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel= new JPanel();
panel.add(scrol2,BorderLayout.WEST);
panel.add(scrol, BorderLayout.EAST);
panel.setBorder(etched);
frame.add(panel);
frame.setVisible(true);
}
}
The default Layout manager for JPanel is FlowLayout, so adding components without actually setting the layout to an instance of BorderLayout will produce unexpected results.
To get a half/half layout you could use GridLayout:
JPanel panel= new JPanel(new GridLayout(1, 2));
You must use a different LayoutManager, BorderLayout will not allow you add components that use only 50% of the visible space. I personally use GridBagLayout, which allows you to specify many parameters (free space distribution, new lines of components, alignment, etc.).
You should set the LayoutManager of your JPanel to BorderLayout first (only ContentPanes i.e frame#getContentPane() have a default layout of BorderLayout):
JPanel panel= new JPanel(new BorderLayout());
panel.add(scrol2,BorderLayout.WEST);
panel.add(scrol, BorderLayout.EAST);
panel.setBorder(etched);
Related
I tried to add a JPanel (with FlowLayout) to a JScrollPane but the ScrollBar is not working. I want to place buttons as grid but it places horizontally.
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(1000,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(pane);
for (int i=0;i<100;i++){
panel.add(new JButton("Label"));
}
}
}
I want to place buttons as grid but it places horizontally.
That's because you do not set the preferred size of the JPanel and because you add the JPanel to a JScrollPane you are effectively giving the JPanel infinite width and FlowLayout will lay out all its components in a single row until it reaches the width limit of the JPanel but because the width is infinite, all the JButtons appear on the same line. Also, because you set the horizontal scrollbar policy to NEVER, there is no way to scroll the JPanel.
You should call method setVisible(true) after you have added all the components.
Note that in the below code I use GridLayout rather than FlowLayout because FlowLayout will not display a grid of JButton. Also note that I call method pack() rather than method setSize().
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 10, 5, 5));
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
for (int i=0;i<100;i++){
panel.add(new JButton("Label"));
}
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
}
Here is a screen capture:
Note that, by default, JScrollPane will size itself so as to display all the JButtons. If you want the JScrollPane to only display a few rows, then you need to set its preferred size, for example
pane.setPreferredSize(new Dimension(710, 150));
EDIT
If you insist on using FlowLayout then you need to set the preferred size for both the JPanel and the JScrollPane.
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(710, 315));
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("Label"));
}
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pane.setPreferredSize(new Dimension(720, 160));
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
}
Here is a screen capture.
I'm working on a Swing GUI using the JLayeredPane. The JFrame has a JLayeredPane which contains two JPanels. Later I want to display some components in the JPanels, I cut this part of the code to make it shorter for you.
How do I resize the JFrame to the sizes of the JPanels? frame.pack() does not work and without the line setting the preferred size the GUI will show with minimal size.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class TestGUI {
private JFrame frame;
private JLayeredPane layeredPane;
private JPanel panelBottom;
private JPanel panelTop;
private MainMenuBar menuBar;
public TestGUI() {
// panel bottom:
panelBottom = new JPanel();
panelBottom.setSize(1000, 500);
panelBottom.setBackground(new Color(0, 100, 0, 100));
panelBottom.setLayout(new GridBagLayout());
// panel top:
panelTop = new JPanel();
panelTop.setSize(950, 450);
panelTop.setBackground(new Color(0, 0, 100, 100));
panelTop.setLayout(new GridBagLayout());
// layered pane:
layeredPane = new JLayeredPane();
layeredPane.add(panelBottom, 1);
layeredPane.add(panelTop, 0);
// frame building:
frame = new JFrame();
menuBar = new MainMenuBar();
frame.setJMenuBar(menuBar);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(1100, 600)); // <-- Without this the GUI will be very small
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(layeredPane, BorderLayout.CENTER);
frame.pack(); // <-- does not work!
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new TestGUI();
}
}
EDIT: I could just change the preferred size line to fit the biggest JPanel, but i ask the question because I want the JFrame to resize depending on the size of the JPanels dynamically.
As suggested in How to Use Layered Panes: Laying Out Components in a Layered Pane, "Although a layered pane has no layout manager by default, you can still assign a layout manager to the layered pane." Use OverlayLayout, seen here, for overlapping panels.
Alternatively, use JInternalFrame, which does allow you to pack() the internal frames individually, as shown here and here.
I am creating my IDE using a JTabbedPane. As i add tabs to my JTabbedPane, some titles are longer and some are shorter. This results in quite an ugly title length with some really long titles.
An example where i add a Pane to my JTabbedPane:
codes.addTab("", icon, scroll.get(scroll.size() - 1));
This results in this output when i run the program:
As you can see, The longer titled tabs make the title tab quite long and ugly. And when i add too many tabs, this happens:
Question: Am i able to fix or scale the title size like maybe restrict the length of title is its too long? Also maybe a left right arrow on too many files? Just like this:
If you want to set the tab's size, it would be a little complicated because in some way you should override the default behavior of the JTabbedPane paint() method family and the LookAndFeel you use. I want to tell it is not that ugly you think, because manu IDEs such as Eclipse or Intellij also have different length tab sizes for the source files:
Eclipse:
So don't worry about size.
But about placement of the tabs, you must use the following method to change the tab placement's layout:
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
You can add some SplitPanes to you newly hatched IDE to make it mode professional. Try the following snippet:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;
public class MainWindow extends JFrame {
private static final int FRAME_WIDTH = 1024;
private static final int FRAME_HEIGHT = 600;
public MainWindow() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(dim.width/2-FRAME_WIDTH/2, dim.height/2-FRAME_HEIGHT/2, FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
this.setLayout(new BorderLayout(4, 4));
//
JToolBar toolBar = new JToolBar();
toolBar.add(new JButton("Test"));
toolBar.add(new JButton("Test"));
toolBar.add(new JButton("Test"));
toolBar.add(new JButton("Test"));
JPanel northPanel = new JPanel();
northPanel.add(toolBar);
//
JPanel southPanel = new JPanel();
JLabel statusLabel = new JLabel("Test Status...");
southPanel.add(statusLabel);
//
this.add(northPanel, BorderLayout.NORTH);
this.add(southPanel, BorderLayout.SOUTH);
//
JPanel westPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(eastPanel);
splitPane.setRightComponent(westPanel);
splitPane.setDividerLocation(250);
splitPane.setContinuousLayout(true);
this.add(splitPane, BorderLayout.CENTER);
//
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
tabbedPane.addTab("MyFrame.java", new JTextArea());
tabbedPane.addTab("JButton.java", new JTextArea());
tabbedPane.addTab("Main.java", new JTextArea());
tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
tabbedPane.addTab("MyFrame.java", new JTextArea());
tabbedPane.addTab("JButton.java", new JTextArea());
tabbedPane.addTab("Main.java", new JTextArea());
tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
tabbedPane.addTab("MyFrame.java", new JTextArea());
tabbedPane.addTab("JButton.java", new JTextArea());
tabbedPane.addTab("Main.java", new JTextArea());
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setAutoscrolls(false);
//tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); Also try this in comparison with above line
westPanel.add(tabbedPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
MainWindow mw = new MainWindow();
mw.setVisible(true);
}
}
Good Luck.
you can align your title to the left like this :
JLabel tabLabel = new JLabel("Tab", JLabel.LEFT);
//add new label at set location
jTabbedPane.setTabComponentAt(0, tabLabel);
If you want more information check this link the answer is very complet.
Hope i helps you
EDIT :You can also limit the lenght of your title it's an other possibility
You can also follow this tutorial made by Oracle it's pretty simple
I am trying to add multiple panels to another panel. I want them to be on top of each other so I'm using JLayeredPane. I've added a button to each one. Two buttons should appear when it works.
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class PanelTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLayeredPane layers = new JLayeredPane();
mainPanel.add(layers);
panel2.setOpaque(false);
panel1.setOpaque(false);
panel1.setVisible(true);
panel2.setVisible(true);
panel1.add(new JButton("1111111111"));
panel2.add(new JButton("2"));
frame.setContentPane(mainPanel);
layers.add(panel1, new Integer(2));
layers.add(panel2, new Integer(3));
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Only the grey background of the mainPanel is visible. What am I doing wrong?
When adding a component to a JLayeredPane, you're essentially adding the component to a null layout using container. This means that you must fully specify both the component's size and its position, often solving both with a setBounds(...) call. Call this on panel1 and panel2, for example:
panel1.setBounds(10, 10, 100, 100);
panel2.setBounds(70, 70, 100, 100);
Edit:
setting bounds didn't make any difference
Setting the size (bounds) is required but you still have an additional problem.
You are adding the JLayeredPane to a JPanel which uses a FlowLayout. By default a FlowLayout respects the preferred size of the component added to it. Since JLayeredPane uses a null layout its preferred size is (0, 0) so there is nothing to paint.
Two solutions:
You don't need the JPanel, just use: frame.setContentPane(layers);
If you really want to use the panel then you need to change the layout manager: JPanel mainPanel = new JPanel( new BorderLayout());
I have the following layout:
The red, blue and green parts are JPanel. In the red part I have four JLabel. Now if I resize the JFrame, the title labels always stay in the center. But I would prefer if they are evenly distributed horizontaly in the red part. Which layout should I use for that?
Use a GridLayout(1, 0) for the top JPanel. The two numbers means 1 row and variable number of columns. If you are using JLabels, this will be sufficient, especially if you set the JLabels alignment constant to SwingConstants.CENTER. If you are using components that fill the grid slots, such as JButtons, then you may need to use other variants of the GridLayout constructor, such as GridLayout(1, 0, ?, 0) Where the ? is a number that tells the GridLayout how much horizontal separation there should be between slots.
The overall GUI would use a BorderLayout of course.
For more and better information on this, please check out Lesson: Laying Out Components Within a Container and A Visual Guide to Layout Managers
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class LayoutEg {
private static void createAndShowGui() {
String[] labelStrings = {"One", "Two", "Three", "Four"};
JPanel topPanel = new JPanel(new GridLayout(1, 0));
for (String labelString : labelStrings) {
// create labels and center the text
topPanel.add(new JLabel(labelString, SwingConstants.CENTER));
}
topPanel.setBackground(Color.red);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.blue);
// setting preferred size for demonstration purposes only
centerPanel.setPreferredSize(new Dimension(700, 400));
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.green);
// main panel uses BorderLayout
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.PAGE_START);
mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
JFrame frame = new JFrame("LayoutEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Use a BorderLayout for the frame's content pane, and use a GridLayout with 1 row and four columns for the title panel.
See http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html and http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html for tutorials.
If you don't want the same width assigned to each label, but in fact want the same space between every label, you may also use a BoxLayout, and add a glue between each label (and between the border of the panel and the label:
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(Box.createHorizontalGlue());
p.add(new JLabel("Label"));
p.add(Box.createHorizontalGlue());
p.add(new JLabel("Long Label"));
p.add(Box.createHorizontalGlue());
p.add(new JLabel("Very long Label"));
p.add(Box.createHorizontalGlue());
p.add(new JLabel("Extremely long Label"));
p.add(Box.createHorizontalGlue());
Use a BorderLayout for the main frame and use the NORTH, CENTER and SOUTH places.
To create empty space like in the bottom panel, use Box.createVerticalStrut(size).
Use BoxLayout to place the labels packed in a row.
Finally use GridBagLayout to center a panel in another Panel.
Here is a short example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Prototype extends JFrame {
public Prototype() {
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.LINE_AXIS));
labelPanel.add(new JLabel("First"));
labelPanel.add(Box.createHorizontalStrut(10));
labelPanel.add(new JLabel("Second"));
labelPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
labelPanel.setOpaque(false);
JPanel bottomPanel = new JPanel();
bottomPanel.add(Box.createVerticalStrut(15));
bottomPanel.setBackground(Color.GREEN);
JPanel centerPanel = new JPanel();
centerPanel.add(Box.createRigidArea(new Dimension(200, 300)));
centerPanel.setBackground(Color.BLUE);
JPanel northPanel = new JPanel(new GridBagLayout());
northPanel.add(labelPanel);
northPanel.setBackground(Color.RED);
JPanel panel = new JPanel(new BorderLayout());
panel.add(northPanel, BorderLayout.NORTH);
panel.add(centerPanel, BorderLayout.CENTER);
panel.add(bottomPanel, BorderLayout.SOUTH);
add(panel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Prototype();
}
}
Use a MigLayout.
No, seriously, I used to go crazy trying to get Java layouts working. Then I started maintaining some software that uses MigLayouts, and after a week or so I could do anything I wanted. There is an excellent cheat sheet that looks pretty intimidating, but it's incredibly helpful.
With a MigLayout, to have evenly spread labels in a JPanel you would:
// the "fillx" means that the layout will expand to take up all available horizontal space
JPanel panel = new JPanel(new Miglayout("fillx"));
// the "growx" means that the component will expand to take up all available horizontal space
add(new JLabel("Work items"), "growx");
add(new JLabel("Change set"), "growx");
add(new JLabel("Files"), "growx");
add(new JLabel("Source code"), "growx");
gridLayout, put the labels into the grid layout and set rows to 1 and columns to 4 (1 row for each Label)
http://www.javabeginner.com/java-swing/java-gridlayout-class-example
GridBagLayout is one approach. Set the weightx value to 1.0.
For labels to be distributed evenly horizantally - in the red panel - use BoxLayout with X_AXIS direction and put 'glue' between each label - More here -
http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html