Using Grix, gridy to remove JTextField - java

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.

Related

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.

JPane inside JScrolledPane. No vertical scrollbar when needed

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.

Replace Component in GridBagLayout

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.

JScrollPane does not update scrollbar

I am trying to create a JScrollPane that contains a JPanel that will be increasing and decreasing in height. When it becomes larger than the size of the JScrollPane, it should create a vertical scroll bar which will allow me to scroll through the entire JPanel. However, I am having difficulty achieving this. Yes, I know I am not using LayoutManagers. No, I will not be using them, and I need a solution that does not involve their usage.
Here are the two button's AbstractActions that add and subtract from the JPanel:
class AddACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.setSize(420,info.getHeight() + 40);
info.add(new SubPanel); // Adds another JPanel into the main JPanel (for content input)
gui.repaint();
infoS.validate();
}
}
class RemoveACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.remove(subPanel()); // This would remove the last JPanel added to the main JPanel
info.setSize(420,info.getHeight() - 40);
gui.repaint();
infoS.validate();
}
And here is the code for the main JPanel and the JScrollPane:
final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)
This is the second project I've been trying to learn GUI by doing. I am a complete novice in Swing and am only intermediate in Java. Sorry if I am making a blindingly obvious mistake.
1) Use LayoutManagers (+1 to #kleopatra and #GagandeepBali comments)
The absence of LayoutManagers only guarantees your GUI's will look very trashy (especially when run on other OSes/builds) and being a Novice you should rather learn the correct way than learn the wrong way and get into bad habits like calling setSize() etc.
Have a read on these links to get you started:
A Visual Guide to Layout Managers
Concurrency in Swing
2) See this example for how to use a JScrollPane, it simply adds a JPanel with buttons to a JScrollPane which in-turn is added to the JFrame.
3) Also see this example for how to make the JScrollPane vertically scroll-able only.
4) For more on JScrollPanes have a look here: How to Use Scroll Panes.
5) As for how it interacts with LayoutManager, if you do not explicitly set its size via setPreferredSize(Dimension d) the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners)
6) On your usage of validate():
validate() is used when new JComponents are added to a visible component
revalidate() is used when JComponent is removed/added from a visible component
revalidate() covers validate() too
Thus always use this:
//add or remove component(s)
revalidate();
repaint();
References:
http://www.daniweb.com/software-development/java/threads/405568/validate-vs-revalidate
LayoutManager is not required to solve the problem. The problem in Thrfoot's example is in these lines:
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
The program appears to recognize there is a need for scroll bars (it would show the scroll bar if your setting was VERTICAL_SCROLLBAR_AS_NEEDED), but the actual scrolling does not work (the scroll bar slider is not there).
To fix this, first set the preferred size of info, then construct the infoS.
Example:
info.setPreferredSize(420,600);
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
The idea is to set the preferred size of the info panel before it is used for the scroll pane. This is the same reason to set the size and location of infoS before adding to the gui:
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)

Change the component weight dynamically in GridBagLayout

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)

Categories

Resources