Scroll content of JPanel - java

I went to multiple articles about this problem but i can't fix it.
According to this article: How to make scrollable to jPanel
i made this:
nieuw.add(onder, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(nieuw);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(300,300,300,300);
hoofd.add(scrollPane);
add(hoofd);
onder contains the content, so i added a JScrollPane to it, but the scrollbar doesn't appear (see image).
I hope you guys know the answer, thank you in advance!!

I fixt it with scrollPane.setPreferredSize(new Dimension(580,320));
so my final code:
JPanel hoofd = new JPanel();
JPanel onder = new JPanel();
JPanel nieuw = new JPanel();
nieuw.add(onder, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(nieuw);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(580,320));
scrollPane.setBounds(300,300,300,300);
hoofd.add(scrollPane);
add(hoofd);

Related

pinned footer inside JScrollPane

I would like to pin my footer panel to the bottom of my JPanel, i have tried the following.
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(header, BorderLayout.PAGE_START);
pane.add(table, BorderLayout.CENTER);
pane.add(footer, BorderLayout.PAGE_END);
JScrollPane SP = new JScrollPane(pane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
What am i missing here?
This should work
JPanel child = new JPanel(new BorderLayout());
child.add(header, BorderLayout.PAGE_START);
child.add(table, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(child, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel pane = new JPanel(new BorderLayout());
pane.add(scrollPane, BorderLayout.CENTER);
pane.add(footer, BorderLayout.PAGE_END);
I need to do footer with scroll like on the table
A JScrollPane doesn't support this type of functionality. So you somehow need to fake it.
Maybe you can use a second JScrollPane as the footer component. Then you share the model of the horizontal JScrollBar. Using RSDinh's code as a starting point you might do something like:
//pane.add(footer, BorderLayout.PAGE_END);
JScrollPane footerScrollPane = new JScrollPane(footer);
JScrollBar horizontal = footerScrollPane.getHorizontalScrollBar();
horizontal.setModel(scrollPane.getHorizontalScrollBar().getModel());

Having Multiple Panels with scroll panes

I created two panels and a main panel. Each panel contains a very large image, and I wanted both of them to be scroll-able to see the rest of the image. But when I add the two panels in the main panel and run it, the first panel is soo big that it covers the second panel. How would I implement ScrollPane for both panels?
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(jPanelSouth, BorderLayout.SOUTH);
add(mainPanel);
//where would I use this?
//scrollPane.setViewportView();
}
}
Each panel contains a very large image>
//JPanel mainPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
You may want to use a GridLayout so that each scroll pane takes up half the frame so as much of each image as possible is displayed.
//JScrollPane scrollPane = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane(jPanelNorth);
The easiest way to use the scroll pane is to create the scrollpane with the component you want displayed and the scrollpane will add the component to the viewport for you.
//mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(scrollPane); // don't need the constraint when using GridLayout.
Then you add the scrollPane to the main panel, since the scrollpane contains the panel with the image.
it seems to use grid layout is much better than using border layout , in this case :
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
//1. use GridLayout with 2 rows and 1 column .
JPanel mainPanel = new JPanel(new GridLayout(2,1));
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
//2.you should place .setViewportView() here :
scrollPane.setViewportView(jPanelNorth);
scrollPane2.setViewportView(jPanelSouth);
mainPanel.add(scrollPane);//is in the top ("North")
mainPanel.add(scrollPane2);//next ("South")
//3.use setContentPane instead of add()
setContentPane(mainPanel);
}
}

Adding JScrollPane to JFrame/JPanel

I looked on many questions and websites but I can not find the answer.
I have a JPanel. I would like to add a scroll bar, so I thought I would use a Jscrollpane.
public class TheFrame extends JFrame {
public ThePanel canvas;
public TheFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//-------------------------------------
JScrollPane scroll = new JScrollPane(canvas);
scroll.setViewportBorder(new LineBorder(Color.RED));
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll, BorderLayout.SOUTH);
//-------------------------------------------------
canvas = new ThePanel();
setSize(700, 400);
this.add(canvas, BorderLayout.CENTER);
setVisible(true);
}
At the moment, the scroll is just appearing at the bottom. The border shows that it is only a small area at the bottom. I am trying to put the Jpanel into a Jscrollpane. So the border is around the whole application area. ThePanel extends JPanel. Thank you for any assistance.
JScrollPane scroll = new JScrollPane(canvas);
add(scroll, BorderLayout.SOUTH);
canvas = new ThePanel();
this.add(canvas, BorderLayout.CENTER);
A couple of problems:
the canvas variable is null when you create the scrollpane to nothing is added to the scrollpane
a component can only have a single parent so when you add the canvas to the "CENTER" you remove it from the scrollpane.
The structure of the code should be:
canvas = new ThePanel();
JScrollPane scrollPane = new JScrollPane( canvas );
add(scrollPane, BorderLayout.CENTER);
setVisible( true );
That is, you add the canvas to the scrollpane and the scrollpane to the frame.
Add canvas to scroll, and add scroll to this. JScrollPane wraps the component, it doesn't magically add itself to the component.
Example:
JFrame frame = new JFrame();
JPanel pane = new JPanel();
JScrollPane scroller = new JScrollPane(pane);
frame.add(BorderLayout.CENTER, scroller);
scroller.setWheelScrollingEnabled(true);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.setVisible(true);

JScrollPane In JFrame with two panels

I want to add a scroll bar to my JTextArea but it just won't show up. I have read a lot of stuff on forums but all in vain. Any suggestions are highly appreciated.
Thanks in advance. Below is my code.
JPanel pan, pan2;
JTextArea text = new JTextArea();
JTextField fname = new JTextField(18);
JLabel filename = new JLabel("Filename");
JButton view = new JButton("View");
public FileReading() {
setLayout(new BorderLayout());
pan = new JPanel();
pan2 = new JPanel();
JScrollPane scroll = new JScrollPane(text);
//scroll.setBounds(400,400,400,400);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
text.setEditable(false);
scroll.setViewportView(text);
pan2.add(scroll);
//scrollpane.setViewportView(text);
pan2.setLayout(new BorderLayout());
//pan2.add(scrollpane);
pan.setLayout(new FlowLayout());
pan.add(filename, FlowLayout.LEFT);
pan.add(fname, FlowLayout.CENTER);
pan.add(view, FlowLayout.RIGHT);
view.addActionListener(this);
fname.addActionListener(this);
pan2.add(text, BorderLayout.CENTER);
pan2.add(pan, BorderLayout.SOUTH);
//BorderLayout.EAST
//add(pan, BorderLayout.SOUTH);
add(pan2);//, BorderLayout.CENTER
setVisible(true);
}
public static void main(String args[]) {
FileReading frame = new FileReading();
frame.setTitle("Enter The Full Path to the File");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,400,400,400);
//frame.setSize(400,400);
}
You need to add scroll (JScrollPane) to the pan2 not to the text (JTextArea)
try this
pan2.add(scroll, BorderLayout.CENTER);
in place of pan2.add(text, BorderLayout.CENTER);
EDIT
JTextArea gets added automatically when we add JScrollPane into the panel, as you have added text (JTextArea) inside JScrollPane
here -> JScrollPane scroll = new JScrollPane(text);
Can try this
add(scroll);//, BorderLayout.CENTER
add(pan, BorderLayout.SOUTH);
instead of
add(pan2);//, BorderLayout.CENTER
This way we are directly adding the scrollpane to main frame and putting other things below

Java JTextArea that auto-resizes and scrolls

I have a JTextArea in a JPanel. How can I have the JTextArea fill the whole JPanel and resize when the JPanel resizes and scroll when too much text is typed in?
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); //give your JPanel a BorderLayout
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space
See http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html or http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html for more details on JScrollPane
Place the JTextArea inside of a JScrollPane, and place that into the JPanel with with a layout that fixes the size. An example with a GridBagLayout, for instance could look like this:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane();
GridBagConstraints cons = new GridBagContraints();
cons.weightx = 1.0;
cons.weighty = 1.0;
panel.add(scrollPane, cons);
JTextArea textArea = new JTextArea();
scrollPane.add(textArea);
This is only a rough sketch, but it should illustrate how to do it.

Categories