Java Swing - Making a scrollable JPanel inside a JFrame - java

I have a custom component called FixtureComponent that extends JPanel, it is basically a JPanel containing a number of controls placed inside it, each with it's own size and location. What I am trying to do is to place a number of FixtureComponent vertically in my JFrame as follows:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
main frame = new main();
FixtureComponent comPanel = new FixtureComponent();
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.setSize(300, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
for (Integer i = 0; i < 20; i++)
{
frame.getContentPane().add(comPanel);
}
frame.setVisible(true);
}
});
}
The problem I am getting is when I run the above code, I get a single FixtureComponent placed at the top of the JFrame instead of getting 20 FixtureComponents placed vertically above each other.
And I would like to also know in case of that I successfully got the above code to work, how to add a scroll bar to scroll across the FixtureComponent?
Thank you.

Create and add a JScrollPane to the frame, setting the JScrollPane context to the content you need scrolling, in the example below this is a JPanel named container.
Add your FixtureComponent objects to container, and boom. Here's the code:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Main frame = new Main();
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JScrollPane scroller = new JScrollPane(container);
scroller.setPreferredSize(new Dimension(200, 1000));
for (Integer i = 0; i < 20; i++) {
FixtureComponent fixture = new FixtureComponent();
container.add(fixture);
}
frame.setLayout(new BorderLayout());
frame.add(scroller, BorderLayout.WEST);
frame.setSize(300, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

Related

Scrollbars in BorderLayout.PAGE_START / NORTH

Is there a way such that a component that is added to the BorderLayout.PAGE_START area will become scrollable if (and only if) there's not enough space?
I have attached a minimal example. If you resize the complete frame the label in the center will become scrollable, the label at the top won't.
Unfortunately, I can't change the BorderLayout.PAGE_START positioning, as this is given by a framework. However I do have full control over the creation of myComponent.
public static void main(String[] args){
JPanel panel = new JPanel(new BorderLayout());
JComponent myComponent = new JScrollPane(new JLabel("<html>START-START<br><br>START-START</html>"));
panel.add(myComponent, BorderLayout.PAGE_START);
panel.add(new JScrollPane(new JLabel("<html>CENTER-CENTER<br><br>CENTER-CENTER</html>")), BorderLayout.CENTER);
final JFrame mainframe = new JFrame("Test");
mainframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainframe.getContentPane().add(panel);
javax.swing.SwingUtilities.invokeLater(() -> {
mainframe.pack();
mainframe.setVisible(true);});
}
Is there a way such that a component that is added to the BorderLayout.PAGE_START area will become scrollable if there's not enough space?
Yes there is. Make its preferred height less than its actual height. BorderLayout respects the preferred height of the component contained at PAGE_START, so it doesn't matter how many lines you put in your JLabel, it will be displayed at its preferred height – without scrollbars.
Try the following.
public static void main(String[] args) {
JPanel panel = new JPanel(new BorderLayout());
JComponent myComponent = new JScrollPane(new JLabel("<html>START-START<br><br>START-START</html>"));
myComponent.setPreferredSize(new Dimension(100, 20));
panel.add(myComponent, BorderLayout.PAGE_START);
panel.add(new JScrollPane(new JLabel("<html>CENTER-CENTER<br><br>CENTER-CENTER</html>")),
BorderLayout.CENTER);
final JFrame mainframe = new JFrame("Test");
mainframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainframe.getContentPane().add(panel);
javax.swing.SwingUtilities.invokeLater(() -> {
mainframe.pack();
mainframe.setVisible(true);
});
}
EDIT
The below code doesn't give the best result but I think it is the way to go. You just need to play around with the different sizes of the different components.
I added a ComponentListener to the content pane of the JFrame. When the JFrame is resized, you need to recalculate the heights of the top component and the center component according to your needs and then update the relevant component sizes. Note that the below code is not a complete solution but hopefully enough to allow you to arrive at a complete solution.
public static void main(String[] args) {
final JFrame mainframe = new JFrame("Test");
JLabel topLabel = new JLabel("<html>START-START<br><br>START-START");
JScrollPane topPane = new JScrollPane(topLabel);
mainframe.add(topPane, BorderLayout.PAGE_START);
JLabel centerLabel = new JLabel("<html>CENTER-CENTER<br><br>CENTER-CENTER");
centerLabel.setMinimumSize(centerLabel.getPreferredSize());
JPanel centerPane = new JPanel();
centerPane.add(centerLabel);
mainframe.add(centerPane, BorderLayout.CENTER);
javax.swing.SwingUtilities.invokeLater(() -> {
mainframe.pack();
mainframe.setVisible(true);
final Dimension centerPaneDim = centerPane.getPreferredSize();
final Dimension topLabelDim = topLabel.getPreferredSize();
mainframe.getContentPane().addComponentListener(new ComponentListener() {
#Override
public void componentShown(ComponentEvent e) {
// Do nothing.
}
#Override
public void componentResized(ComponentEvent e) {
Dimension size = e.getComponent().getSize();
if (size.height < centerPaneDim.height + topLabelDim.height + 10) {
topPane.setPreferredSize(new Dimension(topLabelDim.width, 10 + size.height - centerPaneDim.height));
}
else {
topPane.setPreferredSize(new Dimension(topLabelDim.width, topLabelDim.height + 10));
}
e.getComponent().revalidate();
e.getComponent().repaint();
}
#Override
public void componentMoved(ComponentEvent e) {
// Do nothing.
}
#Override
public void componentHidden(ComponentEvent e) {
// Do nothing.
}
});
});
}

Java Swing 13 GridLayout does not exist?

I can't seem to get a swing GridLayout to work in java 13. The error is that GridLayout cannot be resolved to a type in the following code:
import javax.swing.*;
public class GameFrame extends JFrame {
public static final void NewFrame() {
new GameFrame();
}
public GameFrame() {
this.setSize(1600, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("The Game");
this.setVisible(true);
this.setResizable(false);
JPanel MainPanel = new JPanel();
MainPanel.setLayout(new GridLayout());
}
}
The issue is caused by the class not being imported.
import java.awt.GridLayout;
Since it is not in the swing package it doesn't get imported with the star import.
Also it is better to use explicit imports.
This might be related to the fact that panel is empty. Try running this code and it should work.
public class GridLayoutTest {
private static JButton[] arrayBtn;
public static void main(String[] args) {
// the frame that contains the components
JFrame frame = new JFrame("GridLayoutTest from JCG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the size of the frame
frame.setSize(350, 350);
// set the rows and cols of the grid, as well the distances between them
GridLayout grid = new GridLayout(5, 3, 10, 10);
// what layout we want to use for our frame
frame.setLayout(grid);
// add a text field with a specified text to the frame
JTextArea text = new JTextArea();
text.setText("Result");
text.setEditable(false);
frame.add(text);
// add buttons to the frame
frame.add(new JButton("+"));
frame.add(new JButton("="));
arrayBtn = new JButton[10];
// add JButtons dynamically
for(int i=0; i < arrayBtn.length; i++) {
arrayBtn[i] = new JButton(Integer.toString(i));
frame.add(arrayBtn[i]);
}
frame.setVisible(true);
}
}

JFrame large grid map

I'm trying to make a large grid map of 160x120 JButtons with JFrame but it's too much to fit on the window.
How can I overcome this?
public class DisplayTable extends JFrame {
public static void main() {
JFrame frame = new JFrame("puzzle layout");
//frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(3200, 800);
JPanel panel = new JPanel();
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.setBorder(new EmptyBorder(0,0,0,0));
panel.setLayout(new GridLayout(12,16));
for(int i=0; i<120; i++) {
for(int j=0; j<160; j++) {
JButton temp = new JButton("1");
panel.add(temp);
}
}
frame.add(panel);
frame.setVisible(true);
}
}
Use a JScrollPane to wrap the panel ... also, you should use pack instead of setSize, but only after you've added all your components to the screen
See How to use scroll panes for more details

JLabel doesn't show up

I'm working on a program but my JLabel doesn't show up. My JButton works perfectly (it appears) but for some reason the JLabel does not appear. I have checked on internet but I Haven't found anything.
package com.hinx.client;
import java.awt.Color;
import javax.swing.*;
public class Main {
public static void main(String [] args)
{
createWindow();
}
static void createWindow()
{
//Create panel
JPanel content = new JPanel();
content.setLayout(null);
//Build the frame
JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 400);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(content);
frame.setVisible(true);
//Add the login button
JButton login = new JButton("Login");
login.setBounds(0, 342, 150, 30);
//Create login label
JLabel loginlabel = new JLabel("Login Area");
//Create login panel
JPanel loginpanel = new JPanel();
loginpanel.setLayout(null);
loginpanel.setBounds(0, 0, 150, 400);
loginpanel.setBackground(Color.gray);
loginpanel.add(login);
loginpanel.add(loginlabel);
content.add(loginpanel);
}
}
I have checked on internet but I Haven't found anything.
JFrame is visible before JComponents (frame.add(content);) are added / created
move code line frame.setVisible(true); (better everything about JFrame) to the end of constuctor
Set a layout for your panel. Per example :
loginpanel.setLayout(new BorderLayout());
You can learn more about layouts here.
Here's what I get :
Use layouts. FlowLayout should be fine in this case. Do not call setBounds() and do not set layout as a null.
Add label and button on JPanel
Then add JPanel on JFrame
Call pack() instead of setSize()
Call setVisible(true) in the end.
Good luck!
You are making setLayout null.
JPanel loginpanel = new JPanel();
loginpanel.setLayout(null);
Use this,
JPanel loginpanel = new JPanel();
loginpanel.setLayout(new BorderLayout());
Run the UI on the EDT instead of running on the main thread. Read this post.
Example:
public static void main(String [] args)
{
Runnable r = new Runnable() {
#Override
public void run() {
createWindow();
}
};
EventQueue.invokeLater(r);
}

setting the size of a JPanel

I have a class that extends a JPanel called Row. I have a bunch of Row added to a JLabel, the code is the following:
JFrame f=new JFrame();
JPanel rowPanel = new JPanel();
//southReviewPanel.setPreferredSize(new Dimension(400,130));
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(test1);
rowPanel.add(test1);
rowPanel.add(test2);
rowPanel.add(test3);
rowPanel.add(test4);
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize());
rowPanel.setMinimumSize(rowPanel.getPreferredSize());
f.setSize(new Dimension(300,600));
JScrollPane sp = new JScrollPane(rowPanel);
sp.setSize(new Dimension(300,600));
f.add(sp);
f.setVisible(true);
where test1...etc is a Row. However when I resize the window the layout of the Row somehow becomes messy (it resizes as well)... how can I prevent this from happening?
Read the Swing tutorial on Using Layout Managers. Each layout manager has its own rules about what happens when the container is resized. Experiment and play.
In the case of a BoxLayout it should respect the maximum size of the components added to the panel so you can do:
childPanel.setMaximumSize( childPanel.getPreferredSize() );
If you need more help post your SSCCE demonstrating the problem.
I took the code in http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java and adapted it with what you are trying to do, only using buttons instead of custom JPanels:
public class BoxLayoutDemo {
public static void addComponentsToPane(Container pane) {
JPanel rowPanel = new JPanel();
pane.add(rowPanel);
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(addAButton("Button 1"));
rowPanel.add(addAButton("Button 2"));
rowPanel.add(addAButton("Button 3"));
rowPanel.add(addAButton("Button 4"));
rowPanel.add(addAButton("5"));
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize());
rowPanel.setMinimumSize(rowPanel.getPreferredSize());
}
private static JButton addAButton(String text) {
JButton button = new JButton(text);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
return button;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The end result is this:
As you can see, the button row is perfectly aligned. If you resize the JFrame, they stay aligned. Is that what you are looking for?

Categories