So I have a JPanel filled whit JButtons and only Jbuttons:
How do I make it so when only button 1 and button 20 are Visible so the button layout becomes
and if I were to have Jbuuton 15 Visible it would come in between JButton 1 and 20.
I'm trying to just make it anchor to the top and left and set the spacing to 0 but it does this:
I can't really give a code as it is made in NetBeans generated code and the program setting buttons to visible are depending on a lot of other stuff so making it independent would take a lot of time.
Edit: Using a method to add the JButtons to a grid via a method in my case
public static void RedoGridLayout(){
GridLayout UpgradesLayout = new GridLayout(0,5);
CookieclickerGUI.Panel_Upgrade.setLayout(UpgradesLayout);
for(int i = 0; i<upgrades.size(); i++){
Upgrades upgrade = upgrades.get(i);
JButton Button = upgrade.getJButton();
if(Button.isVisible()){
CookieclickerGUI.Panel_Upgrade.add(Button);
System.out.println("Added...\n" + Button + "\nButton to the grid");
}
}
}
this how ever adds the button in a grid at the Bottom and not the top, so if you want to see the buttons you need to scroll down.
EDIT 2:*
The buttons got placed at the bottom as the other buttons were still in the panel (ofc they were) so by doing .removeAll();
before placed the buttons at the top left but extended to fit the whole panel. that I fixed by adding all buttons that were !.isvisible()
after making the button correct shape.
The Answer to my Question was GridLayout. it put my buttons in the Grid that I wanted.
The solution to the grid being placed at the bottom was because a simple mistake by me adding the grid to the layout when it contained a lot of buttons being set to .setvisible(false);
so the program placed it after all buttons. this was fixed by a simple .removeAll();
removing all JComponents that were stored in it.
Note that I stored all my JComponents in a JButton array as I only had buttons and later did a for loop running thru the array checking if it was visible or not and if it was it got added to the grid.