Swing JButton: Icons and text in one position - java

Is it possible to use JButton I can put the text is right in the icon, not above, below, left and right side of the icon. If possible how?

Related

Increase gap between icon and button 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.

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.

Moving JButton text over its icon by pixels

I am creating a desktop application in Java with lots of customized UI.
It also has a breadcrumb.
I've extended JButton class to customize it for my Breadcrumb buttons.
This is the screenshot of expected breadcrumb.
http://puu.sh/4MtvZ.jpg
The background of this breadcrumb is an ImageIcon.
But now, I am not able to perfectly align the JButton text over this background icon.
This is the screenshot of actual breadcrumb.
http://puu.sh/4MtDc.png
I've used following code to align the text.
setHorizontalTextPosition(CENTER);
setVerticalTextPosition(CENTER);
So, is their a way that I can move this text in pixels to its right position?
And also, I want root breadcrumb button "Schemes" to overlap some part of its preceding breadcrumb button "2000 Avenues" as shown in expected breadcrumb screenshot!
How can I achieve that?
Check out the Overlap Layout for one solution.
So, is their a way that I can move this text in pixels to its right position?
I don't understand this question. Why are you setting the alignment to center if you want it aligned to the left.
Edit:
I see. You are just using an Icon for the button outline and are then painting the text on top of the Icon. I thought your were using a custom shaped button with text). You can just use the default vertical/horizontal text position settings.
To shift the text to the left you can use:
button.setIconTextGap(10 - button.getIcon().getIconWidth());

How do I set Z-order for image in Swing Java

I have 3 image so I want, if I drag any image that should appear above all the other images. Which technique can use? please any idea???? By the way I am using "ImageIcon".
Thanks in advance.
If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. For an example of this, please see my code in a related question: dragging a jlabel around the screen
If I'm totally off base, sorry, but please correct my incorrect assumptions.
By the way I am using "ImageIcon".
I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel.
so I want, if I drag any image that should appear above all the other images
You need to add a MouseListener to the JLabel. When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel.
So the basic code in the MouseListener would be:
panel.setComponentZOrder(label, 0);

How do I get a button to align to the right in MigLayout

I am adding a button to a panel using Miglayout, and try what I might, I cannot get it to go to the right end of the panel. It insists on going flush left. Oddly, the demo is kind of short on such on a example (it only shows it in the context of other buttons on the same panel.
I have a panel like this:
dialog
->complex display retrieved from another class
OK Button here.
Except that it always insists on putting it like this:
dialog
->complex display retrieved from another class
OK Button here.
OK, I got the answer to this (finally). When adding the panel that contains the button add the component constraint of "gapbefore push." I had been trying that, but only on the button itself, not the panel.
panel.add(button, "skip(the amount of cells you have), al 100%")

Categories