JScrollBar _horizontalScroll;
_verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
_horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(true);
I have a code shown above, here vertical scroll bar is working fine, but horizontal scroll bar is not working (doesn't appear on my Swing GUI).
You never add your horizontal scrollbar.
Try adding the entire panel into the JScrollPane.
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
And you can add your listeners using
scrollPane.getHorizontalScrollBar().addAdjustmentListener(this);
Related
Everytime I click a button in my program, I add 5 new rows to the JPanel. Eventually, the rows overflow and I would like to add a JScrollPane so I can scroll down and see the new rows.
I know how to get it working for a TextArea but I can't seem to figure out how to make it work when I have a GridBagLayout. Below, I will attach the code for setting up my panels.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm);
JScrollPane scrollpane = new JScrollPane(panelForm);
panelMain.add(scrollpane);
When I run, my code I get a box enclosing the GridBagLayout, but the scrollpane is nowhere to be seen.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
You would typically add the scroll pane directly to the frame. There is no need for the "panelMain".
panelMain.add(panelForm);
That line is not needed. A component can only belong to a single parent. You later add "panelForm" to the scrollpane.
So the basic code would be:
JPanel panelForm = new JPanel(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane(panelForm);
frame.add(scrollpane);
but the scrollpane is nowhere to be seen.
The scrollbars only appear when needed by default. Adding empty panels will not cause the scrollbar to be displayed. Click on your button a few times and add your child components to the "panelForm" using the appropriate GridBagConstraints. You will then need to use:
panelForm.revalidate();
panelForm.repaint();
The revalidate() causes the layout manager to be invoked so the scrollpane can determine is scrollbars are required or not.
I'm trying to insert a new panel into another panel in runtime everytime I press a button. My problem is the original panel runs out of space and I can't see the new panels I'm adding.
What I've tried so far:
Using scrollpane for vertical scrolling with no success.
Using flowlayout-no luck. Tried disabling horizontal scrolling-keep pushing the new panel to the right (can't get to it because there is no scrolling).
Tried using borderlayout-no luck.
testpanel t = new testpanel();
t.setVisible(true);
this.jPanel15.add(t);
this.jPanel15.validate();
this.jPanel15.repaint();
This code suppose to insert the t panel into jpanel15.
With flowlayout it pushes the t panel downwards just like I want it to but with no vertical scroll.
PS: I'm using netbeans in order to create my GUI.
My problem is the original panel runs out of space and I cant see the new panels i'm adding. Tried using scrollpane for vertical scrolling with no success.
A FlowLayout adds components horizontally, not vertically so you will never see vertical scrollbars. Instead you can try the Wrap Layout.
The basic code to create the scrollpane would be:
JPanel main = new JPanel( new WrapLayout() );
JScrollPane scrollPane = new JScrollPane( main );
frame.add(scrollPane);
Then when you dynamically add components to the main panel you would do:
main.add(...);
main.revalidate();
main.repaint(); // sometimes needed
Use JScrollPane instead of the (outer) JPanel
Or have a BorderLayout for the JPanel, put in a JScrollPane at BorderLayout.CENTER as the only control. The JScrollPane takes a regular JPanel as view.
In any case you will then add the control to the JScrollPane. Suppose your JScrollPane variable is spn, your control to add is ctrl:
// Creation of the JScrollPane: Make the view a panel, having a BoxLayout manager for the Y-axis
JPanel view = new JPanel( );
view.setLayout( new BoxLayout( view, BoxLayout.Y_AXIS ) );
JScrollPane spn = new JScrollPane( view );
// The component you wish to add to the JScrollPane
Component ctrl = ...;
// Set the alignment (there's also RIGHT_ALIGNMENT and CENTER_ALIGNMENT)
ctrl.setAlignmentX( Component.LEFT_ALIGNMENT );
// Adding the component to the JScrollPane
JPanel pnl = (JPanel) spn.getViewport( ).getView( );
pnl.add( ctrl );
pnl.revalidate( );
pnl.repaint( );
spn.revalidate( );
Im trying to add a Scroll bar to my JList. I want only 4 headings to be available on my JList at a time. When I add a JList and run my program, the JList disappears from the screen. Can someone help me fix this problem? I am using Java Eclipse.
This is my code:
songList1 = new JList (ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
songList1.setBounds(300,100,100,200);
panel.add(new JScrollPane(songList1);
You can try one of the following:
set panel's layout or
set scroll panes bounding area by scrollPane.setBounds()
Just an oversight:
songList1 = new JList(ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane songList1ScrollPane = new JScrollPane(songList1)
//songList1ScrollPane.setBounds(300, 100, 100, 200);
songList1ScrollPane.setBounds(10, 10, 100, 200);
panel.setLayout(null); // Absolute positioning
panel.add(songList1ScrollPane);
...
add(panel); // Or so
Of course the panel should have an appropiate layout.
I added JScrollPane to my JPanel but I wanted to make scroll bar visible at the start of the program not only when the text is going out of the screen is that possible?
jpMiddle = new JPanel();
JTextArea ta = new JTextArea(20,43);
JScrollPane sp = new JScrollPane(ta);
jpMiddle.add(sp);
JScrollPane sp = new JScrollPane(ta,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
That's for the scrollbars to be active always, you can substitute the ALWAYS for AS_NEEDED to ensure they appear only when the contents of ths scrollpane overflows.
The API exists for one reason, though...
I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?