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.
Related
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.
ive got a JPane within a JScrolledPane. When i add content to JPane , JScrollPane doesnt show scrollbar. I tried repaint() and revalidate() but it didnt help.
static void ladowaniePaneli()
{
int b;
for(b=0;b<o;b++)
{
bgPanel[b] = new JBackgroundPanel();
nowyPanel[b] = new JPanel();
((FlowLayout)bgPanel[b].getLayout()).setVgap(0);
nowyPanel[b].setPreferredSize(new Dimension(790,518));
nowyPanel[b].setOpaque(false);
vertical[b] = new JScrollPane(nowyPanel[b]);
vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
vertical[b].setPreferredSize(new Dimension(789,517));
vertical[b].setOpaque(false);
vertical[b].getViewport().setOpaque(false);
bgPanel[b].add(vertical[b]);
}
}
It makes sense that scrollbars are never seen since you restrict the size of the contained component so that it's always trivially larger than the scrollopane's viewport:
nowyPanel[b].setPreferredSize(new Dimension(790,518));
Solution: don't do that.
if i dont use setPreferredSize method components wont warp to another line
You can try the Wrap Layout.
pairs should be warped to new line if they exceed JScrollPane width
Components are layed out individually. I you want a group of components to wrap then you would need to add the components to a separate panel first. Then add the panel to the panel using the WrapLayout.
I created a Class that extends from JPanel which layout property is GridBagLayout. I basically need to display an array of JLabels (grid) but i'm not able to replace a component once it's created. I've tried to remove the component that I want to change, and then place the new one but the result is weird. For example i've created an array of 10x10 JLabels and want to replace position[0][0] and position[9][9] with this code:
//At first i have the jpanel layout completely filled
this.remove(0);
this.add((JLabel) start, 0); //This was created with GridBagLayout.gridx = 0 and GridBagLayout.gridy = 0 and it's fine
this.remove(99);
this.add((JLabel) end, 99); //This was created with GridBagLayout.gridx = 9 and GridBagLayout.gridy = 9 and it's out of grid
this.revalidate();
But only position[0][0] looks fine. What should i do to replace a component?
In GridBagLayout, each component is associated with GridBagConstraints. Simple removing a component and replacing it with a component at the same position won't work, as the new component will receive a new GridBagConstraints
What you can do, however, is get the constraints associated with a give component.
Component toRemove = getComponent(0);
GridBagLayout layout = (GridBagLayout)getLayout();
GridBagConstraints gbc = layout.getConstraints();
remove(toRemove);
add(new JLabel("Happy"), gbc, 0);
This will add the component using the same constraints as those used by the component you are removing.
This of course all assumes that you are assigning the gridx/gridy constraints...
I solve my problem by using:
this.setComponentZOrder(newItem, 0);
So, I actually didn't remove the object on Jpanel but I placed the new one over the old object.
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
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)