I tried to set the position of a JScroll Pane vertically but it seems like it's capped at some point. Horizontally it works just fine.
JPanel panel = new JPanel(null);
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setPreferredSize(new Dimension(800, 800));
JLabel l = new JLabel("test1");
l.setBounds(40,40,100,100); panel.add(l);
JLabel l2 = new JLabel("test2");
l2.setBounds(440,440,100,100);panel.add(l2);
JScrollPane scroll = new JScrollPane(panel);
scroll.getVerticalScrollBar().setValue(500);
scroll.getHorizontalScrollBar().setValue(500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
picture: http://i.imgur.com/nIqBlrv.png
I can set the vertical number to any I want. It wont go beyond this position.
Hopefully someone can help me. Ty already.
try this:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class VerticalScrollPane {
public static void main(String[] args) {
JPanel panel = new JPanel(null);
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setPreferredSize(new Dimension(800, 800));
panel.setVisible(true);
JLabel l = new JLabel("test1");
l.setBounds(40,40,100,100); panel.add(l);
JLabel l2 = new JLabel("test2");
l2.setBounds(440,440,100,100);panel.add(l2);
JScrollPane scroll = new JScrollPane(panel);
scroll.getViewport().setViewPosition(new Point(500,350)); // Use this
JFrame frame = new JFrame();
frame.getContentPane().add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scroll, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Result:
Vertical ScrollPane Java
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 am trying to set the buttons to the center using the Box.createHorizontalStrut() method. However if I use this.getWidth()/2 is does not work. How can I do to center it in the frame.
Code
package ch17;
import java.awt.Color;
import java.awt.Container;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Q17_1 extends JFrame{
JButton left = new JButton("<=");
JButton right = new JButton("=>");
JPanel p1 = new JPanel();
JRadioButton rb1 = new JRadioButton("Red");
JRadioButton rb2 = new JRadioButton("Yellow");
JRadioButton rb3 = new JRadioButton("White");
JRadioButton rb4 = new JRadioButton("Gray");
JRadioButton rb5 = new JRadioButton("Green");
JPanel p2 = new JPanel();
Message m = new Message("Welcome to Java");
public Q17_1(){
setLayout(new GridLayout(3,1));
p1.setBorder(new TitledBorder("Select Message Panel Background"));
ButtonGroup group = new ButtonGroup();
group.add(rb1);group.add(rb2);group.add(rb3);group.add(rb4);group.add(rb5);
rb1.setMnemonic('R');rb2.setMnemonic('Y');rb3.setMnemonic('W');rb4.setMnemonic('G');
rb5.setMnemonic('N');
p1.setLayout(new GridLayout(1,5,5,5));
p1.add(rb1);p1.add(rb2);p1.add(rb3);p1.add(rb4);p1.add(rb5);
p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
add(p1);
add(m);
p2.add(Box.createHorizontalStrut(250));
p2.add(left);
p2.add(Box.createHorizontalStrut(5));
p2.add(right);
add(p2);
left.addActionListener((ActionEvent) -> {
m.moveLeft();
repaint();
});
right.addActionListener((ActionEvent)-> {
m.moveRight();
repaint();
});
rb1.addActionListener(m);
rb2.addActionListener(m);
rb3.addActionListener(m);
rb4.addActionListener(m);
rb5.addActionListener(m);
}
public static void main(String[] args) {
Q17_1 frame = new Q17_1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I have tried, this.getWidth()/2, p2.getWidth()/2, etc. However they don't work and the buttons are still starting from the beginning of the left side.
You could use a combination of Borderlayout, FlowLayout or GridBagLayout
For summary:
setLayout(new BorderLayout());
//...
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
//...
add(p2, BorderLayout.SOUTH);
The reason I might consider using BorderLayout of GridLayout for the core layout is it will give all the remaining space to the component in the CENTER position. This might not be what you want, but it's why I've used it.
Both GridBagLayout and FlowLayout will layout it's containers around the centre of the container, GridBagLayout doing it vertically and horizontally, FlowLayout doing it only horizontally (by default)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;
public class Q17_1 extends JFrame {
JButton left = new JButton("<=");
JButton right = new JButton("=>");
JPanel p1 = new JPanel();
JRadioButton rb1 = new JRadioButton("Red");
JRadioButton rb2 = new JRadioButton("Yellow");
JRadioButton rb3 = new JRadioButton("White");
JRadioButton rb4 = new JRadioButton("Gray");
JRadioButton rb5 = new JRadioButton("Green");
JPanel p2 = new JPanel();
JLabel m = new JLabel("Welcome to Java");
// Message m = new Message("Welcome to Java");
public Q17_1() {
setLayout(new BorderLayout());
p1.setBorder(new TitledBorder("Select Message Panel Background"));
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
group.add(rb4);
group.add(rb5);
rb1.setMnemonic('R');
rb2.setMnemonic('Y');
rb3.setMnemonic('W');
rb4.setMnemonic('G');
rb5.setMnemonic('N');
p1.setLayout(new GridLayout(1, 5, 5, 5));
p1.add(rb1);
p1.add(rb2);
p1.add(rb3);
p1.add(rb4);
p1.add(rb5);
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
p2.add(left);
p2.add(right);
add(p2, BorderLayout.SOUTH);
left.addActionListener((ActionEvent) -> {
// m.moveLeft();
// repaint();
});
right.addActionListener((ActionEvent) -> {
// m.moveRight();
// repaint();
});
// rb1.addActionListener(m);
// rb2.addActionListener(m);
// rb3.addActionListener(m);
// rb4.addActionListener(m);
// rb5.addActionListener(m);
}
public static void main(String[] args) {
Q17_1 frame = new Q17_1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
But If I want to use BoxLayout for practice for p2. What should I put for the arguments in order to center the buttons???
Because the container's size dynamic, you could use some horizontal glue instead
p2.add(Box.createHorizontalGlue());
p2.add(left);
p2.add(right);
p2.add(Box.createHorizontalGlue());
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 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();
}
});
}
}
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