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());
Related
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);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JPanel topPanel = new JPanel(new FlowLayout());
.....
JPanel centrePanel = new JPanel(new FlowLayout(10, 0));
........
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(100, 160));
centrePanel.add(glListScrollPane);
........
........
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(0, 2));
......
........
panel.add(topPanel, BorderLayout.CENTER);
panel.add(centrePanel, BorderLayout.CENTER);
panel.add(bottomPanel, BorderLayout.CENTER);
frame.add(panel);
frame.add(standardButtonPanel);
public void lockScreen(boolean editable) {
standardButtonPanel.button1.setVisible(editable);
......
}
When doing edit and un-edit. the panel is changing its position a little bit.
I have used BoxLayout as I wanted to have the components have there own size and users can resize the screen also.
Is there any other approach? Where I can fix the layout problem?
Instead of using setVisible, try using setEnabled as it dosent hide the button (hence does not affect the UI) but makes it so that the end-user cannot press the button.
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 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.
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.