I have a JPanel that can have either 1 or 2 buttons, depending on what's going on in the program at the time. I'm using Box Layout.X_AXIS to line up the 2 buttons configuration, and it works great. When I switch to 1 button however, the single button is on the far left of the window. I've tried a bunch of different things to get the button centered, but the only thing that seems to work is this:
JButton yesBtn = new JButton("Continue");
btnPane.setLayout(new BoxLayout(btnPane, BoxLayout.Y_AXIS));
yesBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
The problem with this is that the vertical location of the button changes doing it this way, so it doesn't match where the 2 buttons would sit vertically. Is there a way to center the button using the X_AXIS layout?
I've tried a bunch of different things to get the button centered,
The easiest way is to add "glue" BEFORE the first component and AFTER the last component. Then it will work for 1 component or multiple components.
Read the section from the Swing tutorial on Invisible Components as Filler for more information and examples.
Related
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.
I am doing a program in java in which you have to select a category with a button.
I'm trying to put an icon and text in the JButton, but I can't quite get the alignment right. I want a wider space to the left between the left side of the button and the icon.
I have read that you can create an invisible line border to solve this, but my JButton already has a border.
choose_Animals = new JButton ("ANIMALS");
choose_Animals.setIcon(categoriesIcon[0]);
choose_Animals.setHorizontalAlignment(SwingConstants.LEFT);
choose_Animals.setIconTextGap(20);
choose_Animals.setOpaque(false);
choose_Animals.setContentAreaFilled(false);
choose_Animals.setForeground(Color.BLACK);
choose_Animals.setBorder(border);
choose_Animals.setFocusable(false);
choose_Animals.setFont(p);
choose_Animals.setBounds(90, 220,470, 85);
choose_Animals.addActionListener(this);
this.add(choose_Animals);
Do you know if what I ask for is possible with a null layout?
Positioning the text/icon of a button has nothing to do with a layout manager. These are properties of the component itself.
And yes, you should be using a layout manager, not a null layout. The time to learn using layout managers is now, not some time in the future.
I have read that you can create an invisible line border to solve this,
You can use an EmptyBorder to give addition space to one of the 4 inset positions.
but my JButton already has a border.
You can use a CompoundBorder to combing your current Border with the EmptyBorder
Read the section from the Swing tutorial on How to use Borders for more information and examples.
When I start my program I have 6 panels. Then I have some radio buttons and depending on the choice some panels hide. In this example I want to hide all panels except 1 and 2. When I click the radio button instead of hiding the rest of the panels and leave the first 2 panels in the current position, it moves them.
private void MonocButtonItemStateChanged(java.awt.event.ItemEvent evt) {
Panel3.setVisible(false);
Panel4.setVisible(false);
Panel5.setVisible(false);
Panel6.setVisible(false);
}
Propably the panel that contains the show/hide panels has FlowLayout manager. When components get invisible the container lays out from scratch the components again.
To get around with this you can use an AbsoluteLayout or make the panels invisible in a diferent way so you cannot see them but they are occuping space.
Looks like switching to Absolute Layout fixed the problem! Solved!
I'm working on a simple app that has:
2 TextFields with Labels: name and description
Add button
Panel that is filled with name/description labels and with radio buttons to select records and manipulate with them (edit / delete -> these buttons appear when radio button is selected).
I tried the following layouts composition:
2 Panels with BoxLayout (YAXIS) which contain text field and their labels
Panel with GridBagLayout to have 2 panels with text fields and a button to add data.
Panel with BoxLayout (YAXIS) to be filled with records after button press
Panel with GridBagLayout that contains radio button and 2 labels with results of name and description text fields.
Here is a screenshot of what I came to:
As you can see there's a problem - Labels have no word wrap. If I use JTextArea for word wraping, then white background appears (setBackground(null) doesn't help).
I think that GridBagLayout is not good choice here or even there are too many inner panels. In fact it seems this task is common enough. What are good practices for apps UI building like that one?
The Problem you got with your JLabel is easy to handle. Because JLabels accept HTML Tags you can use this mechanic to automaticly wrap your text:
label.SetText(String.format("<html><div WIDTH=%d>%s</div><html>", width, text));
For the general Layout I prefer the GridbagLayout. With the GridbagLayout you only need on Layout-Typ. GridBagLayout is flexible and easy to use for your case.
If the Layout will get more complex you will probably need to stack different Layouts, to get the best handling.
I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().