Change the component weight dynamically in GridBagLayout - java

Suppose now there are some components inside a JPanel and the layout is arranged using GridBagLayout. Is it possible to change the weight(weightx or weighty) of the components dynamically (e.g. after pressing a button)? Thank you.

Remove and add the component with a new GridBagConstraint. After that call
panel.revalidate();
panel.repaint();
Use the method to get current constraints public GridBagConstraints getConstraints(Component comp)

Related

Using Grix, gridy to remove JTextField

I want to know if its possible to remove a JTextField using
GridBagLayout and removing like this.
PANEL.remove(gridx.5, gridy.5) ---< is this possible?
Or how can I remove with this sort of logic.
No there is no method that does that, you will need to write your own.
The Container class has a method getLayout(). This will allow you to get the GridBagLayout instance for your panel.
The Container class also has a method getComponents()
So once you get all the components in an array you iterate through the array. For each component you would:
Use the getConstraints() method of the GridBagLayout.
Then check the gridx and gridy value of the GridBagConstraints object to see what clumn/row the component is in.
Remove the component from the panel if it meets your criteria.
After the loop is finished you invoke revalidate() on the panel.
Edit:
Somewhere in your code you need to set the layout manager to the GridBagLayout. Then you add components to the panel using your GridBagConstraints.
Then in the future when you want to remove the component from the panel you need to reference:
the JPanel you added the components to
the GridBagLayout of the above panel
I gave you the methods you need to accomplish this. So did you read the API for the methods I suggest you need to use?
The basic code would be:
GridBagLayout layout = (GridBagLayout)panel;
Component[] components = panel.getComponent();
for (each component in the array)
{
GridBagConstraint gbc = layout.getConstraints( component )
if (gbc.gridX == ?? && gbc.gridY = ??)
{
// remove the component from the panel
}
}
panel.revalidate();
So I would suggest you create a method that passes in the row and column of the component you want to find. Then you can change the if condition to access these parameters.

Remove All Elements from a GridY zone

I am trying to create a program that sets JLabels, use the GridBagLayout.
I want to create a button that when pressed removes everything from a certain gridY location. How do I do this. Thanks.
Simple. When you add these components to their parent container keep them in a structure (maybe a List). Something like:
JPanel container = new JPanel(new GridBagLayout());
//...
List<Component> components = new ArrayList<>();
// Add components to container and to list...
for (Component c : components) {
container.remove(c);
}
container.revalidate();
container.repaint();
The Container class has a method getLayout(). This will allow you to get the GridBagLayout instance for your panel.
The Container class also has a method getComponents()
So once you get all the components in an array you iterate through the array. For each component you would:
Use the getConstraints() method of the GridBagLayout.
Then check the gridy value of the GridBagConstraints object to see what row the component is in.
Remove the component from the panel if it meets your criteria.
After the loop is finished you invoke revalidate() on the panel.

The panel is expanded when removing/adding components to it

When I provide this code to mouseClicked method in MouseListener interface,
the changeableContentPanel is expanded. Could anyone help me?
if (!changeableContentPanel.isAncestorOf(aMO)) {
aMO = new AccountsManagementOptions();
changeableContentPanel.removeAll();
changeableContentPanel.validate();
changeableContentPanel.repaint();
aMO.setLocation(5, 100);
changeableContentPanel.add(aMO);
changeableContentPanel.validate();
changeableContentPanel.repaint();
}
When you remove/add components to the panel you should be using:
panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();
That is you should be using revalidate() instead of validate() and you only need to invoke the code once after all changes to the panel have been made.
the changeableContentPanel is expanded.
Yes, because the layout manager is invoked and the size of the panel will recalculated based on the size of the component added to the panel.
Could anyone help me?
If you don't want the size to change then you should be using a CardLayout on your panel. The panel size will be fixed to the size of the largest card added to the panel. Then you just swap components.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Java add more than one JPanel objects into JFrame

I am a beginer and I dont know how to add more objects into JFrame.
How could I add more than one JPanel objects into JFrame?
Below is what I have tried.
Thanks for your help.
public class Init extends JFrame{
public Init(){
super("Ball");
Buttons t = new Buttons();
JumpingBall b1 = new JumpingBall();
JumpingBall b2 = new JumpingBall();
t.addBall(b1);
t.addBall(b2);
add(b1);
add(b2);
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
Assuming that JumpingBall extends JPanel, you might want to have a look at the java layout managers here: Link.
The default Layout for a JFrame is the BorderLayout and if you didn't specify where you want to add your component, The BorderLayout will put it in the center by default. In BorderLayout, you cannot have more that one component in the same area. So, in your example you will end up having only the second JumpingBall panel in your frame. If you want to have more than one component at the center, then you will have to create a JPanel and add those components to it using different Layout. The common three Layouts are the BorderLayout, FlowLayout and GridLayout Please have a look at the provided link above to see how the components are arranged.
You can add a number of JPanel objects in a JFrame, using the add method. If only one is displayed, you might need to change your Layout options or use a Layout Manager (Look here for more).
You are seeing only one because it overlapping each other. Just provide setbound(x,y,x1,y1) for you panel component and you will see your panel at location.
or use setLayout(new FlowLayout()); which is going to order your component in respective to other so you will not override each-other.

Store components in ArrayList

I want to store components like JButton, JTextField, JTextArea etc. all in same ArrayList and later loop through it and add every component to JFrame. I tried storing them in ArrayList but when I looped through it and added every component it included to my frame, nothing showed up in frame. Does anyone know how this can be done? Thanks in advance.
go ahead with this:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
List<Component> components = new ArrayList<Component>();
components.add(new JButton("test1"));
components.add(new JButton("test3"));
components.add(new JButton("test3"));
frame.setLayout(new FlowLayout());
for(Component component: components)
frame.getContentPane().add(component);
frame.pack();
frame.setVisible(true);
}
}
add a layout manager to your frame
call pack() to resize the frame according to your components
set the frame visible
Try declaring the ArrayList like this:
List<JComponent> = new ArrayList<JComponent>();
The above works because JComponent is a common ancestor for JButton, JTextField, JTextArea - that is, it's a super class common to all JComponents.
It's not clear to me: why do you want to add the components first to an ArrayList? add them directly to the JFrame.
Do you know how to use Layout Managers?
A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.
Using Layout Managers

Categories

Resources