Increase gap between icon and button java - java

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.

Related

Formatting JButtons in JSplitPane

I'm having trouble getting my JButtons to appear where I want. For some reason all my buttons are appearing in the middle of the Panel when I want it to appear flushed with the top of the Panel, centered. I've tried manipulating different layout positioning such as BorderLayout.NORTH and GridBagConstraints.NORTH however I do not know if I utilizing it on the right components.
Currently my code has A Frame holding a JLayeredPane holding a JSplitPane with two JPanels in it so it has gotten quite confusing.
Do you have any suggestions on how I can get the buttons to appear on the top of the Panel?
I currently have the panel using a GridBagLayout
Read the section from the Swing tutorial on How to Use GridBagLayout for a working example and information about the constraints used by the layout manager.
For some reason all my buttons are appearing in the middle of the Panel
Specifically read the section on the weightx/y constraints, which states that the components will clump together in the center unless you use non-zero values.

Centering a JButton using Box Layout.X_AXIS

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.

Java set component location in GroupLayout

I'm trying to make a marquee like effect in my program, the text is determined by the content of a file which will result a dynamic lenght/width for JLabel, and the problems are:
I use drag n drop and GroupLayout since it will auto resize the component, but it doesn't allow me to use setBounds nor the setLocation method.
I tried to change it to null layout, yes I can use the setBounds or the setLocation, and now the problem is that the JLabel cannot auto resize its width to fit the text length.
My marquee text would goes from right to the left screen, and will do the run 2 times.
Any suggestion here? Thanks :)

Java Swing FlowLayout Alignments

I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself.
Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
See the constructor FlowLayout(int align).
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.
It seems you are after a FlowLayout.RIGHT as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
Consider switching to MigLayout which is a much more powerful layout mechanism. MigLayout basically lets you position you components within a flexible grid, and you can set the specific alignment of a component within each grid cell.
When you want alignment of subcomponents, it also often makes sense to put them inside a nested JPanel. You can then use a separate layout for this JPanel (BorderLayout perhaps?) which will enable you to get the exact alignment that you want.
setHorizontalAlignment of AbstractButton sets the horizontal alignment of the icon and text not the position of the button. AbstractButton's default is SwingConstants.CENTER.
If you want to align the button..set the position while adding it to the panel or frame..something like this....
p.add(button, BorderLayout.SOUTH);//using `BorderLayout`
Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

How can I add a gap around the edge of a Jbutton?

I want to leave the default border on my JButtons, but put empty space around them as well. I'm using a vertical BoxLayout.
I originally said nothing about the borders, and got single pixel LineBorders, which I want, but the buttons all butted up against each other.
I then tried button[i].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)). Rather than adding blank space around the button, it made the buttons' areas expand. It also removed the LineBorder.
I then tried: button[i].setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5), button.getBorder()))
This gave me back the LineBorder, but rather than adding blank space outside the line, it just extended the buttons' areas beyond the line!
I realise I can add blank boxes to space my buttons out, but I want space on the sides of them as well, which is why I want to add an EmptyBorder. I'm new to Swing, so maybe there's an entirely better way of doing this that I don't know about :)
I'm using Jython, but the API should be the same as from Java.
Conceptually, the "empty borders" you want to add are not really part of the button (for example, they should not be clickable).
This is actually a matter of layout, so you should probably check the documentation of the layout manager you are using. For example:
Some layout managers, such as FlowLayout, BorderLayout, or GridLayout, have hgap and vgap properties to specify the horizontal and vertical gaps between components.
With GridBagLayout you would set the insets of a GridBagConstraints object.
With BoxLayout you would add "rigid areas", "glue", and "fillers" (see the Box class).
And so on.
I think it's simpler to add the button to a panel and set the empty border to the panel.

Categories