How to add JPanel into another JPanel with vertical scrollpane at runtime? - java

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( );

Related

Adding JScrollPane to GridBagLayout

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.

Java Swing - JScrollPane fit Viewport width to JScrollPane width

I have been trying to make the content of a JScrollPane shrink in width, eg. i have set the the HorizontalScrollBarPolicy to NEVER, but that just ends up in no ScrollBar appearing and the content not being dispalyed anymore.
What i expect it to look like is this:
This is my MCVE:
final JPanel panel = new JPanel( new MigLayout( new LC().flowX().gridGapX( "20" ).fill() ) );
panel.add( new JXTitledSeparator( "Test" ), new CC().growX().spanX().wrap() );
panel.add( new JLabel( "Shrink me please!" ), new CC().minWidth( "1" ) );
panel.add( new JLabel( "Never shall anyone ever be able to shrink me." ), new CC().growX() );
final JScrollPane scrollPane = new JScrollPane( panel );
scrollPane.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
final JFrame frame = new JFrame( "Test" );
frame.getContentPane().add( scrollPane );
frame.setVisible( true );
frame.pack();
I sadly have no clue how to do this, the JViewport of the JScrollPane doesn't offer any methods which might helpful, neither does the JScrollPane itself afaik.
Also i have already tried to achieve the same thing using BoxLayout and FlowLayout instead of MigLayout in order to verify that MigLayout isn't the cause of the problem.
You need to implement the Scrollable interface on your panel.
You would want to implement the getScrollableTracksViewportWidth() method to return true.
This will force the width of the panel to match the width of the viewport. Then each component on the panel will be sized based on the rules of the layout manager.
If you don't want to implement the Scrollable interface yourself then you can use the Scrollable Panel which provides method that allows you to control the scrollable properties.
Try setting the Max width for Shrink Label.
You don't need scroll bar?
if need to use scrollbar as needed from scroll pane constants
final JFrame frame = new JFrame( "Test" );
frame.setLayout(new BorderLayout());
frame.add( scrollPane , BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
Used border layout, and moved the pack before visible.

How to control size of JTextField in Burp Extension?

I'm developing Burp extension and add additional tab. I have to return java.awt.component, so i decided javax.swing.JPanel would be nice. It must be a JLabel and JTextField on my Tab, code here:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,Y_AXIS));
JLabel label = new JLabel("hostname : ");
panel.add(label);
JTextField tf = new JTextField("text");
panel.add(tfHost);
I wanted little text and textfield on top left, but my TextField stretched on all my screen. What do i have to do to fix it? Maybe i have to change layout manager?
The problem is a BoxLayout will allow components to grow to fill the available space to the panel.
So the easiest solution is to add your panel to another panel that will respect the size of the BoxLayout panel.
Something like:
JPanel wrapper = new JPanel(); // uses FlowLayout by default.
wrapper.add( panel );
frame.add( wrapper );
Now when you add the wrapper panel to the frame, the wrapper panel will grow in size, but it will not affect the components added to the wrapper panel.

JScrollpane does not notice another JScrollpane

I have a JFrame which contains a JSplitPane in a JScrollPane (so the user can scroll if the window is to big). The JSplitPane contains a JTabbedPane as the top component and graphics as the bottom component.
Now i want to read a .csv und display it in my JTabbedPane. I can scroll through the list with a second JScrollPane. Here comes the problem, when i import the .csv in my programm, the first JScrollPane seems not to notice that there is a second JScrollPane for scrolling the list and then my window gets a lot of free space to scroll.
JFrame frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(tabbedPane);
splitPane.setBottomComponent(graphics());
JScrollPane scrollPane = new JScrollPane(splitPane);
frame.add(scrollPane);
frame.setVisible(true);
When i import the .csv I add a new JPanel to the tabbedPane. The JPanel contains a list from the data from the .csv
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
// here comes the code for the list
tabbedPane.add(scrollPane);
I hope you understand my problem, it is hard to explain.
Edit: Pictures from before and after importing the .csv may help you to understand.
Get rid of the first scroll pane. Add the split pane directly to the CENTER of the BorderLayout used by the frame. As the frame resizes all the space will be allocated to the split pane.
You can then use:
splitPane.setResizeWeight(1.0);
Now all the extra space will go to the second component as the frame is resized. Therefore the scrollbar for that component will appear/disappear as required.

scrollpane blocks buttons in BorderLayout?

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?

Categories