I need to extract the panel of my JScrollPane - java

I need to know which method gives you the panel panelA from my scrollpane.
For example, I have:
PanelExample panelA = new PanelExample();
JScrollPane scrollpane = new JScrollPane(panelA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.getPanel(); //???
scrollpane.getComponent(); //???
I guess it should be a get method, but I don't know.

You get the component from the JViewport of the JScrollPane:
Component c = scrollPane.getViewport().getView();
Read the section from the Swing tutorial on How to Use Scroll Panes for information on how the scroll pane works and the components involved in the layout of the scroll pane.

Related

Scrollable JPanel without scrollBar

I have a problem with scroling JPanel,
I have a lot of labels and fields which are generated dynamicly, but my frame can't show it all.
My code:
JPanel showPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(showPanel);
add(scrollPane);
scrollPane.setBounds(0, 0, 400, 400);
scrollPane.setVisible(true);
scrollPane.add(Jbuttons);
And Im adding a lot of these buttons but my scrollPane won't show it.
I don't have any scrollBar, with text area I didn't have any problems.
Do you have any idea?
scrollPane.setBounds(0, 0, 400, 400); looks like you're using a null layout, don't this
scrollPane.add(Jbuttons); isn't how you should be adding content to the scroll pane, instead, add it to the showPanel which is already inside the JScrollPane. JScrollPane contains a single component, the JViewport, you can not "add" components to the JScrollPane, you must set the JViewports view to what you want shown and the manipulate this view
Take a look at How to Use Scroll Panes for more details

JScrollpane doesn't display properly

I have added a scroll pane to the main panel of my frame. But it doesn't display properly, here's what I get that appears on the right:
http://postimage.org/image/extp3ncql/
here is the code:
JScrollPane jScrollPane = new JScrollPane(area);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
pane.add(jScrollPane, BorderLayout.EAST);
EDIT: Forgot to mention that area is a label.
1) Use another proper LayoutManager, I'd suggesst use Box or directly BoxLayout
or
2) all areas excluding BorderLayout.CENTER acepted PreferredSize came from JComponent
3) if your area is JTextArea the you can pretty to set JTextArea(int rows, int columns)

make a jscrollpane (containing a jtable) fill its container

I have a JTable with autoResizeMode set to AUTO_RESIZE_LAST_COLUMN. I have added it to a panel by creating a JScrollPane with the JTable as its child widget, and then adding the JScrollPane to the panel.
I would like to set the size of the JScrollPane viewport to that of the parent JPanel, and have the JTable resize its last column dynamically.
JPanel have got implemented FlowLayout by defaut, you can place JScrollPane to the BorderLayout.CENTER
I think if you make the JPanel use GridLayout(1,1) and add the JScrollPane to it then you will get the desired result.
The first answer on this link How to make a JTable fill entire available space? worked perfectly for me.
I did the following
myPanel = new JPanel(new GridLayout());
myJtable = new JTable(MyTableModel);
myPanel.add(new JScrollPane(myJtable));

How does one make an applet scroll?

I have been trying to use JScrollPane with my applet, but it doesn't work. I have a JPanel to which I add 20 buttons, and I want to be able to scroll up and down this JPanel. Instead, the scrollbars do not appear. When I use setPreferredSize they still did not appear even though only about 3 of the buttons are being displayed and the rest are cut off. If I do not use setPreferredSize, there might as well not be any scrollbars because I have to make the window big enough to see all of the buttons. If I try to make the scrollbars always visible, they appear but do nothing. I tried the exact same code with JFrame instead of Applet, and it works fine, but I need it to be an applet. Is JScrollPane incompatible with applets? (Note: I tried to use an outer JPanel and add the scrollable panel to it, but it changed nothing). Changing the layouts also doesn't fix the problem. I have attached a simplified version of my code, but it displays the same errors.
Here is the code I have:
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();
You never all the panel to the scroll pane
You never add the scroll pane to the applet
The basic code should be:
JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
You are adding components to a Panel so you shouldn't expect to see a scroll pane wihout showing the scrollpane. What you want to do is then add that panel to a scrollpane which would be added to ur main container.
From your code, i think your problem is
add(scrollPanel);
your should be doing this
add(scroll);`
This is because you only added the panel to the frame which does not contain any scrollpane. Since you have added the panel unto the scrollpane, you should add the scrollpane and not the panel to the main container.
It sounds like you are using Swing components (JScrollPane, JPanel, ...) in an AWT container (Applet). Try using JApplet instead.

how can I scroll my JFrame using the JScrollbar?

I have a problem.
I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar?
Put all the components in one panel (instead of in the JFrame)
Add this panel to a JScrollPane
Add the JScrollPane to your frame.
I should be something like:
JPanel container = new JPanel();
container.add(panel1);
container.add(Panel2);
JScrollPane jsp = new JScrollPane(container);
frame.add(jsp);
It depends on the layout you are using with your JFrame. If you want to add working scrollbar to your panel you should look at the JScrollPane class and the Scrollable interface which must be implemented by scrollable components.
The How to use scroll panes chapter in the swing tutorial may also be an interesting reading: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

Categories

Resources