Java ScrollPane/JPanel - java

I have a JPanel with multiple Object(custom class extends Jpanel) objects in it. The JPanel has a grid layout with 7 rows and 1 column. I'm trying to add a JPanel with 7 object in it to another JScrollPane so I can scroll to view all of the objects, but it's doing strange things. The scroll bar doesn't show up no matter how many objects are in the JPanel. Any ideas? Thanks in advance.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
public class Main {
#SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
JLabel title = new JLabel("Game", JLabel.CENTER);
title.setPreferredSize(new Dimension(60,60));
title.setBorder(BorderFactory.createLineBorder(Color.black,5));
frame.add(title,BorderLayout.NORTH);
frame.setSize(850,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Grid g = new Grid();
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(1,3));
jp.add(g);
JPanel test = new JPanel();
test.setLayout(new GridLayout(7,1));
test.add(p1);
test.add(p2);
test.add(p3);
test.add(p4);
test.add(p5);
test.add(p6);
test.add(p7);
JScrollPane jsp = new JScrollPane(test);
jsp.setViewportView(test);
jsp.getVerticalScrollBar().setUnitIncrement(50);
jsp.setCorn
jsp.setVerticalScrollBarPolicy(22);
jp.add(jsp,BorderLayout.EAST);
frame.add(jp);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

The jp JPanel uses JPanel's default FlowLayout and this may prevent you from resizing your JScrollPane and seeing that it actually is working properly. Why not either add the JScrollPane to the JFrame's contentPane or make jp use a BorderLayout? Also you don't need to set the JScrollPane's viewportView as you're already doing this by passing "test" into its constructor.

Your code don't compile. Please look at the following code. It can scroll vertically and horizontally.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
class MyPanel extends JPanel {
MyPanel(Color c) {
setBackground(c);
}
}
public class Test {
public static void main(String[] args) {
JPanel panel = new JPanel();
// the size of this panel is larger than the frame
panel.setPreferredSize(new Dimension(500, 2000));
panel.setLayout(new GridLayout(7, 1));
// add 7 sub panels
panel.add(new MyPanel(Color.magenta));
panel.add(new MyPanel(Color.cyan));
panel.add(new MyPanel(Color.blue));
panel.add(new MyPanel(Color.green));
panel.add(new MyPanel(Color.yellow));
panel.add(new MyPanel(Color.orange));
panel.add(new MyPanel(Color.red));
JScrollPane scroll = new JScrollPane(panel);
scroll.setViewportView(panel);
scroll.getVerticalScrollBar().setUnitIncrement(50);
JFrame frame = new JFrame("Test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scroll);
frame.setVisible(true);
}
}

Related

How to add Two components on the right side of the splitted plane in Java Swing

I want to add two components on the right side, i.e. JTextField and JTable on right side of the split plane. The below code is not working. What shall I do?
The result of the above code :( but if I add component (table) in the JScrollPane then it's showing table on right side.
Don't set the layout manager to null. Use an appropriate layout manager. I suggest using BorderLayout.
The below code is not a complete solution, just an example to get you started.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
public class SpliTest {
private void buildAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
JScrollPane treeScrollPane = new JScrollPane(tree);
JTable table = new JTable(2, 4);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane tableScrollPane = new JScrollPane(table);
JTextField textField = new JTextField(20);
JPanel right = new JPanel(new BorderLayout());
right.add(textField, BorderLayout.PAGE_START);
right.add(tableScrollPane, BorderLayout.CENTER);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScrollPane, right);
frame.add(splitPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new SpliTest().buildAndDisplayGui());
}
}
Here is a screen capture.

No error shown but JButton and Label can't be seen

I'm new to coding and I am facing this problem where it doesn't show the JButton and JLabel that i added in the GUI. What have i done wrong and how do i fix it?
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class MainMenu {
public static void main (String []args) {
JFrame frame = new JFrame ("Main Menu");
frame.setSize(480,720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2,5,5));
JButton meals = new JButton ("Meals");
JLabel label = new JLabel ("Welcome back!");
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(meals);
panel.add(label);
frame.add(panel);
}
}
That happens because you frame.setVisible(true); before adding any components to it. You should add the components to the frame first, and then, use the setVisible method.
panel.add(meals);
panel.add(label);
frame.add(panel);
frame.setVisible(true); //visible after components added
Fixed version of your code below. Hopefully it works. I tested it in my IDE.
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 5, 5));
JButton meals = new JButton("Meals");
JLabel label = new JLabel("Welcome back!");
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(meals);
panel.add(label);
frame.add(panel);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

How do I lock the size of the components in a re-sizable frame when using GridLayout?

I want to make the frame resizable but keep the initial size of components in a GridLayout. How do I do this?
Initial Size. This is the size I want to keep:
When the frame is resized, the size of the components changes. I don't want this:
The following is an mcve that demonstrates a solution using GridBagLayout as proposed by MadProgrammer.
Including such mcve in the question makes helping much easier and your chances to get good answers higher.
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class SwingMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout()); //place desired content in a GridbagLayout
frame.add(new TestPane());
frame.pack();
frame.setVisible(true);
}
}
class TestPane extends JPanel{
TestPane() {
Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);
setBorder(padding);
setLayout(new GridLayout(2, 2, 10, 10));
add(new JLabel("Celsius"));
add(new JTextField(10));
add(new JLabel("Fahrenheit"));
add(new JTextField(10));
}
}

Prevent JPanel within JScrollPane from growing horizontally

I have a JPanel with FlowLayout that I'm dynamically filling with identical components (JButtons in the MWE). The JPanel is inside a JScrollPane. As I add components, I'd like them to fill left to right, kicking down to the next row once the top row would become wider than the JScrollPane.
My problem is that FlowLayout is instead widening the JPanel ad nauseum, to which the JScrollPane responds by adding a horizontal scroll. How do I prevent this?
Edit: I've seen WrapLayout; I was hoping for a solution within standard Java since I'm using NetBeans GUI Builder for my application.
MWE based on this answer:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JScrollPane;
public class MWE extends JFrame implements ActionListener {
JPanel panel;
JScrollPane pane;
public MWE() {
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
this.panel = new JPanel();
this.pane = new JScrollPane();
this.panel.setLayout(new FlowLayout(FlowLayout.LEFT));
this.pane.setViewportView(this.panel);
add(pane, BorderLayout.CENTER);
JButton button = new JButton("CLICK HERE");
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
this.panel.add(new JButton("Button"));
this.panel.revalidate();
validate();
}
public static void main(String[] args) {
MWE mwe = new MWE();
}
}
Try setting a preferred size for the panel :
this.panel.setPreferredSize(new Dimension(500, 500));

How to make JTextArea Stick to the window

Hello I would like to make this TextArea stick to the windows size whene I resize it by mouse, the same way as lower buttons does. This is the code it is perfectly working no bugs, please have a glance at it.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Rozklady extends JFrame {
public Rozklady() {
super();
}
public void createGUI(){
setPreferredSize(new Dimension(400,150));
JPanel jp = new JPanel();
// jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
jp.setLayout(new GridLayout(0,1));
JPanel gora = new JPanel();
JPanel dol = new JPanel();
pack();
JTextArea jt1 = new JTextArea("JF1");
gora.add(jt1);
jt1.setPreferredSize(new Dimension(getWidth(),getHeight()/2));
dol.setLayout(new BorderLayout());
JPanel lewo = new JPanel();
JPanel prawo = new JPanel();
JPanel srodek = new JPanel();
dol.add(lewo, BorderLayout.EAST);
dol.add(prawo,BorderLayout.WEST);
dol.add(srodek, BorderLayout.CENTER);
lewo.setLayout(new GridLayout(2,2));
prawo.setLayout(new GridLayout(2,2));
srodek.setLayout(new GridLayout(0,1));
for(int i = 0; i < 4; i++){
lewo.add(new JButton(i+""));
prawo.add(new JButton(i+""));
if(i < 3){
srodek.add(new JTextField("JF"+i));
}
}
jp.add(gora);
jp.add(dol);
add(jp);
setVisible(true);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Rozklady().createGUI();
}
});
}
}
Use BorderLayout for you gora panel. Put text area to the center:
gora.setLayout(new BorderLayout());
gora.add(jt1, BorderLayout.CENTER);
// declare a GridLayout in constructor, one component will 'fill the container'
JPanel gora = new JPanel(new GridLayout());
JPanel dol = new JPanel();
// this should be called after all components are added! BNI
pack();
JTextArea jt1 = new JTextArea("JF1");
// be sure to use a scroll pane for multi-line text components
gora.add(new JScrollPane(jt1));
// ..
Stretching a single component to fill the available space can be achieved various was. Two common ways are using either BorderLayout as mentioned by AlexR or GridLayout. See this answer for sample code. I prefer GridLayout because it is shorter (less typing). ;)

Categories