I have a JScrollPane that has a view component which uses SpringLayout.
final JPanel panel = new JPanel(new SpringLayout());
// add stuff to panel here
final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(scrollPane);
The JScrollPane doesn't seem to work, any help would be greatly appreciated!
Quoting from How to Use Scroll Panes
Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.
so you would have to either call setPreferedSize(Dimension d) on JScrollPane instance
final JPanel panel = new JPanel(new SpringLayout());
// add stuff to panel here
final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(300, 300));
add(scrollPane);
or override getPreferredSize() of your JPanel/ component used as view port
final JPanel panel = new JPanel(new SpringLayout()) {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
// add stuff to panel here
final JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scrollPane);
Other notes:
do not extend JFrame class unnecessarily.
simply call add(..) on JFrame instance as the call is forwarded to contentPane.
Related
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());
My code is essentially as follows
JPanel x = new JPanel();
JPanel y = new JPanel();
JPanel rowPanel = new JPanel(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(y);
Container c = frame.getContentPane();
rowPanel.add(x, BorderLayout.LINE_START);
frame.add(rowPanel);
c.add(scrollPane);
frame.setVisible(true);
except that its in a for loop to create a lot of those in a gridlayout on the frame. The thing I want to do is put that scroll pane in the rowPanel as center but I'm pretty sure I have to add the scroll pane with the container and I don't know how to specify to the container to add the scroll pane there
So as it turns out I don't have to add the scroll pane with a container, I can add it directly to the panel by just puttting
rowPanel.add(scrollPane, BorderLayout.CENTER);
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);
}
}
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);
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.