Drawing border around JLabel when selected, like the Buttons - java

I was trying to paint border around JLabel when it is clicked. Just like JButtons are painted.
I thought it would be easy but I failed to do the job.
I tried to figure out what happens to JButtons when clicked by putting breakpoints in source codes. But I got lost, however, I have a feeling that javax.swing.plaf and its subpackages are what I need.
Am I right? Is there a simpler way to do the job.
Here is an example:

You could add MouseListener to your label and setup a border in mousePressed/mouseReleased methods. Here is a simplified example:
label.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent arg0) {
label.setBorder(BorderFactory.createLineBorder(Color.black));
}
#Override
public void mouseReleased(MouseEvent arg0) {
label.setBorder(null);
}
});
Also, as an alternative you can make a button with a flat style that will look like a label. This answer can be useful.

Related

Change ImageIcon in JLabel using Timer

I have two classes: logic and the JFrame. In frame I have a JLabel and a JButton, and I would like to:
When this button is clicked, the ImageIcon in the label changes after a determined time using a Swing Timer, like if it is flashing. To do it I loaded two images with different brightness (img1b and img1). I tried to make the timer change the image twice with different delays, but I was unsuccessful. I also put a listener in the button and implemented the actionPerformed as below:
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnImg1)) {
logic.piscaImagen(img1, lblImg1);
logic.piscaImagen(img1b, lblImg1);
In logic class:
public void piscaImagen(ImageIcon img, JLabel lbl) {
Timer timer = new Timer(1250, null);
timer.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt){
if(lbl.getIcon() != img){
lbl.setIcon(img);
}
}
});
timer.setRepeats(false);
timer.start();
}
But when I run it nothing changes in the logic.piscaImagen. Any tips?
logic.piscaImagen(img1, lblImg1);
logic.piscaImagen(img1b, lblImg1);
It looks to me like you are starting two Timers. So the first Timer fires and it changes the image, then the second timer fires and it restores the image so basically you only see the first image.
All you need is one Timer. Each time the Timer fires you change the image. So the basic code in your Timer would be:
if (lbl.getIcon() == img1)
lbl.setIcon(img1b);
else
lbl.setIcon(img1);
Or for a more flexible solution you can use the Animated Icon.
The Animated Icon will allow you to specify a List of Icons to display. Then when the Timer fires the next Icon in the List is displayed. You can set the Animated Icon for continuous display or you can control the number of cycles.
EDIT: answer inaccurate, repaint() not necessary - see comments.
You're missing the repaint() call that tells the program it needs to update the display.
#Override
public void actionPerformed(ActionEvent evt) {
if(lbl.getIcon() != img){
lbl.setIcon(img);
lbl.repaint();
}
}
(your if statement was also missing a closing brace, unsure what impact that would have / if it was a typo)

JButton style same as words in a text

I want to make a java application, that will contain some text. I want some of these words to act like buttons. My idea is to put all the text in a text panel and the words that I want to act like buttons to be buttons. But i cant get a button to have the same style as the text. The button its always lower than the text from that line. It's possible to fix that ?
Ty.
Example:
John went to the shop.
"shop" is a button and can be clicked, and it looks exactly like the other words.
My problem:
I can't find how to make a JButton look like the other words.
You could just add a mouse listener to your JLabel with your text. That would respond to a mouse click action. You could also had a hover listener to change the border to give your user some indication that the text is clickable and remove the border when you are no longer hovering.
final JLabel lblNewLabel = new JLabel("Click Me!");
lblNewLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0)
{
System.out.println("Label Clicked!");
}
#Override
public void mouseEntered(MouseEvent e)
{
lblNewLabel.setBorder(BorderFactory.createEtchedBorder(1));
}
#Override
public void mouseExited(MouseEvent e)
{
lblNewLabel.setBorder(null);
}
});

Hover over multiple buttons in Java?

Is it possible, in Java, when you hover over a single button, to make the program think you are hovering over multiple buttons?
I'm using a multi-dimensional array with buttons and want to be able to have 5 buttons be hovered over at a time. (All the buttons near the actual hover).
Any ideas on how to do this?
Note: I'm not using JButtons, just regular buttons. (awt.Button)
EDIT
I obviously wasn't clear enough, and I apologize for that.
Here is a screenshot of what I'm looking for:
So, the cursor is hovering over the first gray space, and all of the space next to it have a different background, however, they are not considered as being hovered over, which if what I need.
Assuming you are using a MouseListener, when the mouseEntered(MouseEvent e) method is called on the master button, explicitly call the same method on all of the listeners of all of the other buttons, passing the event you have been given. Ditto for the mouseExited(MouseEvent e) method.
It's up to you to maintain a reference from the master button to the subordinate buttons.
The subordinate buttons' listeners will receive an event that refers to the master button. If necessary, create your listeners with a reference to the button that they are attached to, so that you can operate on that button when receiving an event.
EDIT:
This is the kind of thing I'm talking about. Does it help?
final List<Button> subordinateButtons = Arrays.asList(new Button(), new Button(), new Button());
Button myButton = new Button();
myButton.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
for (Button subordinateButton : subordinateButtons) {
subordinateButton.setBackground(Color.GRAY);
}
}
public void mouseExited(MouseEvent e) {
for (Button subordinateButton : subordinateButtons) {
subordinateButton.setBackground(Color.LIGHT_GRAY);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
There's no reason why you can't keep a reference from a MouseListener to a List<Button>. If it's the business of the listener to work on those buttons then design your classes so that it happens.

how to change the color of text of tab on mouse over of JtabbedPane?

I need to change the color of text of tab of JtabbedPane on MouseOver.
Is is possible using the Mouse Listener or is there any different property to do that?
Thanks
Jyoti
There is not a built-in property or method to do this.
One option is to create a custom JLabel (or other component) add a MouseListener that would change the color on mouse entry/exit.
Example, something like this:
class CustomMouseOverJLabel extends JLabel{
public CustomMouseOverJLabel(String text) {
super(text);
addMouseListener(new MouseAdapter(){
#Override
public void mouseEntered(MouseEvent e) {
setForeground(Color.BLUE);
}
#Override
public void mouseExited(MouseEvent e) {
setForeground(Color.RED);
}
});
}
}
Then when you make a call to addTab(title, item), also set custom title components like so:
yourTabbedPane.setTabComponentAt(index, new CustomMouseOverJLabel("title"));
The tabbed pane tutorial has an example of tabs with custom components that might help.

Using mouse Motion Listener on JButton?

hii
I have used Image as JButton for set in to panel
but now i want to use mouse motion listener actions on that image
for that purpose what can i do
following is the code for image
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
panel5.add(buttonForPicture,BorderLayout.CENTER);
is there any another way to do that
or
...
I'm not sure exactly what you're asking?
Your button is like any other JButton:
buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
}
});
That catches movement events for the whole button, not just the image.
Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. Search the API for methods containing "icon" to see what your options are.
If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example.

Categories