Simple question that I could not find a simple answer to:
I have a JPanel filled with different components. If i do the following
JPanel panel = new JPanel();
//Code to add componenets to the panel.
panel.setLayout(new MigLayout("wrap 5");
panel.removeAll();
Does this also remove the layout manager? Or are just the componenets removed?
Best regards, Goatcat
Docs saying
Removes all the components from this container. This method also notifies the layout manager to remove the components from this container's layout via the removeLayoutComponent method.
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.
Related
public void tabbedPane(){
JPanel tab1 = new JPanel();
JButton btn = new JButton("Buton - 1");
btn.setPreferredSize(new Dimension(50, 20));
btn.setLocation(0, 10);
tab1.add(btn);
JTabbedPane tabPanel = new JTabbedPane();
tabPanel.addTab("tab1", null, tab1);
tabPanel.addTab("tab2", tab1);
tabPanel.addTab("tab3", btn);
tabPanel.setPreferredSize(new Dimension(450, 150));
tabPanel.setLocation(50, 0);
mainPanel.add(tabPanel);//Main panel on frame
}
When I run my application, I see only tab2 and tab3 pane, and I have many issues:
tabPanel.setLocation doesn't work
tabPanel.addTab("tab1" ...) doesn't work
btn.setPreferredSize(new Dimension(50, 20)); when I
click "tab2" it works correctly, however when I click "tab3" it doesn't change button
size.. why?
and i use null layout
tabPanel.setLocation doesn't work
Don't use setLocation(...) but instead use nested components and layout managers to achieve a pleasing and easy to maintain GUI layout.
tabPanel.addTab("tab1" ...) doesn't work
With Swing, you can only add a component to one container, that's it. The component will only show up in the last container that it was added to.
btn.setPreferredSize(new Dimension(50, 20)); when I click "tab2" it works correctly, however when I click "tab3" it doesn't change button size.. why?
Again, you will want to study the layout managers
and i use null layout
You almost never want to do this as this will make your application not look correct on any platform but your own and will make it very very difficult to maintain and upgrade. It is much better to use the layout managers and let them do the heavy lifting of laying out and sizing components for you.
What is your objective with this?
A JTabbedPane is used to organize views, I see you're trying to add a JPanel as a first tab, this is the 'main goal' of the JTabbedPanes.
tabPanel.addTab("Tab 1", tab1);
Try to add the tab like this, you're passing a 'null' value as the icon, which must not affect at all, but if you're not using an icon, then just add the panel as a tab with the intended name.
On second adding, you're adding again the same component (tab1).
On third adding, you're trying to add a component already on a container (tab1). This will make this component to appear only in the last container you add it to. Besides, component is a JButton. I cannot see the goal of a JButton as a tab.
For the setLocation(x, y) issue, check the layout you're using on the container.
Again, I think the main issue here is that you're not correctly approaching your problem, or you're not using the required tools.
I'm using Netbeans IDE to make a gui application. I have a JFrame with a JPanel inside it. After a button click I want to display a different JPanel inside the first. The other JPanel is in a different file. How would I go about doing this? If this is not practical I don't mind replacing the first JPanel with the second one.
I've tried the following but it doesn't seem to work. I'm new to Java and Gui programming so I would appreciate any help I can get.
private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
JPanel2 jPanel2 = new JPanel2();
JPanel1.add(jPanel2);
}
See the javadoc of the Container#add method:
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.
So it is not sufficient to add the panel, but you must also validate the hierarchy again, e.g. by calling
JPanel1.validate();
JPanel1.repaint();
Using a CardLayout as #Andrew suggested in his answer is probably a better alternative then manually replacing panels
Two side-notes:
Learn and respect Java naming conventions (e.g. instances of a class start with a lowercase). so your JPanel1.add call would become jPanel1.add
There is in most cases no need to extend the Jxxx Swing classes. Looking at your class names JPanel1 and JPanel2 you are exactly doing that. It is better to use the available API to customize those classes then to extend them.
You will also have to add the following code such as your changes to take effect:
jPanel1.validate();
jPanel1.repaint();
Use a CardLayout, as shown here.
newPanel obj = new newPanel ();
setLayout(new BorderLayout());
add(obj ,BorderLayout.EAST ,1);//3rd argument is index
repaint();
revalidate();
So I have a panel and depending upon users entry they are populated with an x number of jlabels. Now the problem is, when the user entered information the labels successfully populate but they do not display properly in the panel; they don't even show.
Only when I resize the frame they appear?
It's been a while since I did Swing programming and I am trying to remember the method which you are supposed on a container after you add components. I think it's revalidate().
usually you have to call:
JPanel yourPanel = new JPanel();
yourPanel.repaint();
yourPanel.validate();
invalidate marks a component as needing to be relaid out soon because the component or one of its children has been resized or become visible or invisible. invalidate is called on a component automatically when children components are added/removed.
validate checks that a container is valid and if not, calls doLayout or invalidateTree to calculate positions and sizes of children components. validate effectively redoes the layouts if necessary, deciding on new sizes and locations of all components in the container.
After adding/removing components from a container, validate must be called on the parent to let the LayoutManager redo the layout. Calling validate does not schedule a repaint, so you may need to call repaint after the validate.
I am adding and deleting components dynamically in a JPanel.
Adding and deleting functionality works fine but when I delete the component it deletes the last component rather than the component to be deleted.
How can I solve this issue?
Interestingly enough I am coming across the same issue and I am surprised people are upvoting the other answer, as he is clearly asking about dynamically created Components, not components already created under a variable name which is obtainable, instead of anonymously created objects.
The answer is pretty simple. Use
getComponents() to iterate through an array of components added to the JPanel. Find the kind of component you want to remove, using instanceof for example. In my example, I remove any JCheckBoxes added to my JPanel.
Make sure to revalidate and repaint your panel, otherwise changes will not appear
Component is from java.awt.Component.
//Get the components in the panel
Component[] componentList = panelName.getComponents();
//Loop through the components
for(Component c : componentList){
//Find the components you want to remove
if(c instanceof JCheckBox){
//Remove it
clientPanel.remove(c);
}
}
//IMPORTANT
panelName.revalidate();
panelName.repaint();
Using the method Container.remove(Component), you can remove any component from the container. For example:
JPanel j = new JPanel();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
j.add(btn1);
j.add(btn2);
j.remove(btn1);
Please help me create my own custom layout, container, component, layout manager...
Example:
Containers and Layout Managers
Create a window frame.
Nest panels within a frame for better layout control.
Create and display buttons in a panel.
List two component attributes that are controlled by a layout manager.
Set the layout manager for a container.
Place components in a panel using BorderLayout, GridLayout, and FlowLayout.
Name one advantage of each of the layout managers.
Create panels with titles.
i was search on google but can't find any that match my requirement
Thanks for your help
Edit: I was found with keyword "Open Source UI"
Updated: 31, Oct 2016
I would like to updated some information to make it clearly for someone who concern. Back in 6 years ago what i want to know is how to build "UI Framework" from beginning.
If you have interesting like me i would like recommend Android UI Framework is good start because of open source and well document. Enjoy deep dive in to legacy code :) Good luck
Create a window frame
new JFrame();
Nest panels within a frame for better layout control
final JFrame jframe = new JFrame();
final JPanel innerOne = new JPanel();
jframe.add(innerOne);
innerOne.add(otherComponents);
Create and display buttons in a panel
innerOne.add(new JButton("Hello World!"));
List two component attributes that are controlled by a layout manager
Obviously check out JavaDoc of BorderLayout: BorderLayout.NORTH and SOUTH
Set the layout manager for a container
innerOne.setLayout(...);
Place components in a panel using BorderLayout, ...
Just apply the layout, and add providing the arguments for the LayoutManager:
innerOne.setLayout(new BorderLayout());
innerOne.add(..., BorderLayout.NORTH);
Name one advantage of each of the layoutmanager.
Check out the JavaDoc's. They are really helpful in these situations.
Create panels with titles.
innerOne.setBorder(new TitledBorder("Hello World"));
You could use the following set of tutorials: http://java.sun.com/docs/books/tutorial/uiswing/.