I have a JList inside a JScrollPane that's placed in a JPanel (BorderLayout.CENTER) and putting that inside another JPanel's BorderLayout.EAST (this JPanel's CENTER contains another JPanel) and this whole JPanel is placed inside a JTabbedPane. Initially, it would look like this:
Now I add some books to the list:
If I go to another tab and come back, this happens:
What I don't understand is that, the JPanel containing the JList has both its minimum and maximum size set:
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(new JScrollPane(bookList), BorderLayout.CENTER);
listPanel.setMinimumSize(listPanel.getPreferredSize());
listPanel.setMaximumSize(listPanel.getPreferredSize());
checkOutPanel.add(listPanel, BorderLayout.EAST);
How can I prevent the JList from auto resizing?
Depending on what it is you trying archive, you can either use JList#setPrototypeCellValue or JList#setFixedCellWidth. These will feed back into the PeferredScrollableViewportSize method which will effect the scroll pane
Related
I searched for hours for an answer,tried every method I know about and didnt find an answer.
I'm working on eclipse, my class is extending JFrame and im trying to replace two components that I added to the frame(using gridbaglayout).
When I'm removing the first one,I can't get to add the second one to where the first one was.
The components are JButton with images.
how do I get to add and remove components wherever I want? (already tried using GridBagConstraints to add it to the place I just removed a component from)
As a workaround you could add to your main panel (using GridbagLayout) inherited panels with the buttons in it. Then when you want to replace these buttons (or whatever component) you dont replace them on main panel. You replace them in the inherited panels. Since you are not giving us code, a kind of pseudocode would be like:
JButton myBtn = new JButton(); //Theinitial button
JPanel mainPanel = new JPanel(new GridBagLayout()); //main panel
JPanel inheritedPanel = new JPanel(new BorderLayout())//borderlayout to fill the entire panel.
inheritedPanel.add(myBtn,BorderLayout.CENTER);
mainPanel.add(inheritedPanel, myConstraints);
JButton replacementBtn = new JButton;
inheritedPanel.remove(myBtn);
inheritedPanel.add(replacementBtn);
inheritedPanel.repaint();
inheritedPanel.revalidate();
The components are JButton with images.
Just replace the image by using the setIcon(...) method.
I am trying to add a text area to a Panel that has flow layout, but its not showing on my GUI...any ideas pls?
private void makeTypes() {
westPanel.setVisible(false);
centerPanel.setVisible(false);
northPanel.setVisible(false);
contentPane.add(westPanel, BorderLayout.WEST);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
// set panel layout and add components
centerPanel.setLayout(new FlowLayout());
northPanel.setLayout(new GridLayout(4, 1));
/**
* Adding East Panel
*/
eastPanel.setLayout(new GridLayout(4,1));
/**
* Adding text area to Centre Panel
*/
a1=new JTextArea();
centerPanel.add(a1);
a1.setVisible(true);
}
centerPanel is invisible:
centerPanel.setVisible(false);
And so nothing added to it will show.
I see that you call setVisible(true) on the JTextArea, but this will have no effect if it is being added to an invisible container since it too will be invisible. Other suggestions: Give your JTextArea decent row and column property values, something that could be done via its constructor that takes two ints (for row and column). And wrap the JTextArea within a JScrollPane and add that to the GUI. If this code is called during program run and not at startup, then call revalidate() and repaint() on the container after adding and removing components.
For more and better help, consider creating and posting a valid SSCCE.
I'm trying for quite some time now to find what the problem but to no avail ,its a quite simple one really, the BorderLayout won't add the button to the correct place on the screen (South).
I don't want to add the button to the JPanel itself, I want to make a secondary panel, add that panel to the main panel and the button to the secondary panel, here the simple code:
public class panelClass extends JPanel{
JPanel secondaryPanel = new JPanel();
JButton btn = new JButton("Test");
public panelClass(){
add(secondaryPanel);
secondaryPanel.setLayout(new BorderLayout());
secondaryPanel.add(btn, BorderLayout.SOUTH);
}}
The button shows up in the upper-middle part of the screen, like a regular
FlowLayout, instead in the lower-middle part like I expected.
this line is the problem
add(secondaryPanel);
default layout of jpanel is flow layout .so when you add secondary panel to main panel secondary panel added to upper-middle position of main panel.that's why you see button shows up in the upper-middle part of the screen, like a regular flowLayout.if you set a background color to secondary panel you can clearly see the problem yourself
add appropriate layout to your main panel .for example
setLayout(new BorderLayout());
add(secondaryPanel,BorderLayout.CENTER);
http://pastebin.com/VaaTRsuf
I would like to have the JList and JTextArea resize with the window, but the JPanel stays in the center.
Your LogView class extends JPanel and thus unless you change it, it uses JPanel's default layout, FlowLayout. Components held in a FlowLayout-using container do not change size when the container changes size, and so if you want this behavior, you don't need a component Listener -- you just need to change the layout manager for the LogView JPanel to BorderLayout or something similar that allows its held component to expand, that's it. One line of code:
public LogView(final JFrame contentPane) {
// .......
setLayout(new BorderLayout()); // add this, that's it
add(mainPanel);
}
Another option is to get rid of mainPanel as it doesn't appear to be necessary at all, to set the layout of your LogView object to be GridBagLayout and to add your components directly to the LogView object.
I'm building a Swing application in Java using NetBeans and I have a problem with layout. My main frame contains a JScrollPane which contains a JPanel called contentPanel which in turn contains a JPanel called listPanel. The listPanel is empty when the program starts, but when the user interacts with the program an unpredictable number of smaller JPanels are added to it. I've used the NetBeans GUI-builder to snap the top edge of listPanel to the top of contentPanel, and the same with the bottom edges.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane. The verticalScrollBarPolicy of my scrollpane is set to AS_NEEDED and its viewportView is set to contentPanel. What I think I need to do is to make contentPanel grow when more items are added to listPanel.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane.
The scrollbar will appear when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. When you add components dynamically you need to tell the scrollpane something has changed. So you basic code should be:
panel.add( subPanel );
panel.revalidate();
Or, because you are adding a panel to the sub panel, you may need to revalidate the scrollpane (I don't remember):
panel.add( subPanel );
scrollPane.revalidate();
The key is the revalidate() which tell the layout manager to recalculate its size.
Use a different LayoutManager. One that will allow for vertical growth like BoxLayout. Also remember that you can use multiple layouts and nest them inside of each other for different effects.