Hover over multiple buttons in Java? - 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.

Related

How to check which button in my JFrame was clicked if I have more than one button with the same text?

I am designing Reversi game in java, and the structure is as follows:
a. A JFrame with 64 buttons in it. The buttons are stored in an array.
b. The JButtons will have black circles or white circles.
So whenever a move is to be made, the program will highlight those boxes where a move can be made, but how can I know which button (I want to know the index of that button) has been clicked when all are highlighted the same way?
From my understanding, you are attempting to detect when a specific JButton is pressed.
The simplest way to do this is by implementing an ActionListener.
public class ExampleClass implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonNameOne)
System.out.println("Button One was pressed");
else if (e.getSource() == buttonNameTwo)
System.out.pringln("Button Two was pressed);
}
}
Detecting an action
The actionPerformed(ActionEvent e) method will activate whenever any button is pressed.
Recording source of action
When it is pressed, it automatically detects the source of this action (the button) and stores it in parameter "e".
Using recorded source of action
By simply doing e.getSource() you are able to get the component which invoked this method and compare it to pre-existing components in your program.
Customized arguments
With each if statement, you are able to customize and personalize the result of the condition (which is if the button being pressed is equal to a specific button). Do this by putting arguments within the body of each conditional statement:
if (e.getSource == sayHiButton)
System.out.println("Hi");
You probably have one ActionListener added to all buttons. Then the ActionEvent getSource passed to performAction has info. That is ugly, like testing the button text.
What is more normal is to use Action (take a look) and setting different actions bearing the 64 states.
public BoardAction extends AbstractAction {
public BoardAction(int x, int y) { ... }
#Override
public void actionPerformed(ActionEvent e) {
...
}
}
JButton button = new JButton(new BoardAction(x, y));
In an Action you can also specify the button caption, and an Action can also be (re)used in a JMenuItem and such.
Because of the extra indirection needed, most examples use an ActionListener,
but swing interna use Action quite often. For instance having an edit menu with cut/copy/pase and a toolbar with cut/copy/paste icons, context menus.

How to create a general action that changes tabs in GUI?

I have several actionListeners throughout my code for when I push a button, it changes tabs between the ones I have.
However, I would like to create a general action that depending on which button was pressed (through an int), it changed to a different tab. This is the current actionListener I have.
JButton btnSaveAddESS = new JButton("Save");
btnSaveAddESS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tabbedBackground.setSelectedIndex(0);
tabbedBackground.setEnabledAt(1, false);
}
});
With this, I would like to create a general action, however , while creating the action as a different class, I am not able to access the TabbedPane (tabbedBackground) component.
How can I implement this, avoiding actionListeners?
Thanks,
Nhekas
changeTab(int i){
tabbedBackground.setSelectedIndex(i);
tabbedBackground.setEnabledAt(i, false);
}
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(Jbutton.getText());
changeTab(int i);
}
what you need is actually a method which handles the operation pass an int to the method changetab and the method will change the selectedTab

How do you construct a MouseEvent?

I know this question is very simple, but I cannot figure out how to construct it. It seems to have a lot of parameters, and I not only don't know what to put in them, but I also don't even know what a component is. It would be great if someone could explain using basic concepts. Thanks
There are 3 steps you need:
1. You have to implement the Mouselistener interface in your GUI class
2. Add the listener to a GUI element which it should listen to
3. Implement the event listener for your specific mouse event (will also be required by the compiler after you add the interface to your class)
For help: tutorial from Oracle https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
MouseEvent can not be effectively used without MouseListener.
To use MouseListener, you have to either use:
public class StackTest implements MouseListener{
public static void main(String[] args){
}
public void mouseClicked(MouseEvent arg0){
}
public void mouseEntered(MouseEvent arg0){
}
public void mouseExited(MouseEvent arg0){
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0){
}
Or, you would use
component.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0){
}
public void mouseEntered(MouseEvent arg0){
}
public void mouseExited(MouseEvent arg0){
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0){
}
});
Either works, just as long as the component is able to have a MouseListener implemented into it. Those five methods are required too, and they all have their own invokes.
mouseClicked
This is invoked when the user presses any button on the mouse within the component.
mouseEntered
This is invoked when a user's mouse enters the component's region.
mouseExited
This is invoked when a user's mouse leaves the component's region.
mousePressed
This is invoked when a user presses their Left Mouse Button on the component.
mouseReleased
This is invoked when a user releases any of the buttons on the mouse after it has been pressed over the component
NOTE: mouseClicked will ALWAYS be invoked before mouseReleased, and the same goes with mouseEntered and mouseExited, respectively.
These methods will also be invoked regardless of whether or not there is any code in them.
Now, a component is an object that descends from the Component class. Components are things such a JButtons, JPanels, JFrames, etc. and all the modern components you see are from from the javax.swing package. For example, you may have seen the JOptionPane.
The JOptionPane is just a JFrame, a JLabel, and a JButton.
All of those are components that can have an Event Listener. An event listener does exactly what it says. It listens for specific events. MouseListener for example, listens for events involving the user's Mouse.
You can learn more about components here

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);
}
});

How to stop JButton being enabled when mouse hovers over it?

I created a frame using NetBeans. The frame has two buttons A and B. Button A is initially disabled. It is to be enabled only when button B is clicked.
public newFrame() { //newFrame is the name of the frame that has buttons A&B
initComponents();
btn_A.disable();
}
private void btn_BActionPerformed(java.awt.event.ActionEvent evt)
{
btn_A.enable();
}
The problem is that button A becomes active/enabled when the mouse is moved over it ie inspite of whether button B is clicked or not. How can i fix this?
I want button A to be enabled only after button B is clicked and not as a result of any other event.
Use btn_A.setEnabled(false) instead of btn_A.disable()
btn_A.enable() is a deprecated method.
To do this task, you could replace it by btn_A.setEnabled(false); to disable the button and btn_A.setEnabled(true); to enable the button.
Also, one more suggestion is, add statements like the following in your method if you feel something wrong happening:
System.out.println("Some statement relevant to the method");
The main aim of those extra statements being you know when the method was actually executed.
Try the following code:
button. addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
button.setEnable(true);
}
public void mouseExited(MouseEvent me) {
button.setEnable(false);
}
});

Categories