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);
}
}
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 have problem with displaying components on my JFrame. I'm closing my current window and opening new one and want to display jLabel on it but nothing is happening. Code is below :
Frame[] nF = DBChooser.getFrames();
nF[0].setVisible(false);
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel ("Processing...");
textLabel.setLayout(null);
pan.setLayout(null);
windoow.setLayout(null);
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
I appreciate any help
Why you need so many setLayout(null); ? I remove them and it worked
public class DBChooser extends Frame {
public static void main(String args[]) {
Frame[] nF = DBChooser.getFrames();
// nF[0].setVisible(false);
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel("Processing...");
// textLabel.setLayout(null);
// pan.setLayout(null);
// windoow.setLayout(null);
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
}
}
It is because you set a null layout to window and panel without specifying any width, lenght or position, either use some LayoutManager or set these properties (eg. bounds). A null LayoutManager means that you need to set everything yourself, because there is nothing (no LayoutManager) that would place your elements automatically. This example uses a BorderLayout, which creates a nice effect:
the code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel("Processing...");
textLabel.setLayout(null);
pan.setLayout(new BorderLayout());
windoow.setLayout(new BorderLayout());
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
}
}
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
When I run this program, I don't see a scrollbar on the Label. What am I missing?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Util1
{
public static void main(String[] args)
{
new Util1();
}
public Util1()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ExamplePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ExamplePane extends JPanel
{
public ExamplePane()
{
final JPanel panel = new JPanel(new GridBagLayout());
final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");
message.setPreferredSize(new Dimension(500, 50));
JScrollPane scroller = new JScrollPane( message, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setViewportView(message);
panel.add(scroller);
add(panel);
}
}
}
To see a scrollbar wrap the "message" JLabel into JPanel and then add this JPanel to JScrollPane like bellow:
public ExamplePane() {
final JPanel panel = new JPanel(new GridBagLayout());
final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");
message.setPreferredSize(new Dimension(500, 50));
final JPanel messagePanel = new JPanel();
messagePanel.add(message);
JScrollPane scroller = new JScrollPane(messagePanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setPreferredSize(new Dimension(100, 50));
panel.add(scroller);
add(panel);
}
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();
}
});
}
}