Button underline text on hover - java

I am working with JavaFX, and I have a Scene object I created in SceneBuilder with a bunch of Button objects. I want the text of each of these Button objects to be underlined when the mouse cursor enters their area in the window, and to remove the underlining when the cursor leaves.
I know I can just type:
public class Controller {
#FXML
private Button exitButton;
public void exitButtonMouseEntered() {
this.exitButton.setUnderline(true);
}
public void exitButtonMouseLeft() {
this.exitButton.setUnderline(false);
}
}
However, doing this for each Button object is time-consuming, and it seems like there must be an easier way to force all the Button objects to have the same behavior given the same conditions.
How can I make a controller method that will affect all the Buttons in the Scene?

Apply the following css:
Button:hover{
-fx-underline: true;
}

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 make text fields visible upon button click?

I have two text fields that I make invisible when the form is initialised.
What I want to happen is the following.
and then when the button is clicked, they appear like so.
I have tried making the text fields not visible when the form initialised then triggering a action performed event when the button is clicked making the text fields visible again.
import javax.swing.JFrame;
public class Weather extends javax.swing.JFrame {
public Weather() {
initComponents();
this.jTextField3.setVisible(false);
this.jTextField10.setVisible(false);
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(true);
this.jTextField3.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Weather().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
etc..
etc...
}
In C#, this method of making things visible and invisible works but the same logic doesn't apply to java. Nothing happens when I click the button. The two text fields just stay invisible.
When I don't make the text fields not visible when the form is initialised and make them invisible upon button click via the button clicked actionevent method, it works.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(false);
this.jTextField3.setVisible(false);
}
Why can I only make the text fields invisible via button click but I can't make the text fields visible via button click?
That's probably because the window and the GUI was already created with the buttons invisible. When you just set them to visible true it doesn't know how to rewrite them. You can try:
this.revalidate(); //Here this being the jframe
this.repaint();

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

JavaFx, Not to display content at the beginning

I am writing a javafx program, for moving text. The program display the background (some red points) and text at the beginning.
Now, I would like to add a menu to select the content of text to display. In addition, I want the scene to display nothing at the beginning, then after I select content of text, the program starts to display everything...
Is there any special command to implement this?
PS: I add the contents (text and shapes) -> "Group" -> "root".
I think what you want is something along this:
public class PersonOverviewController implements Initializable{
#FXML
BorderPane paneWithControls;
#FXML
MenuItem menuItem;
#FXML
Label text;
#FXML
private void initialize() {
paneWithControls.setVisible(false); //hide content
menuItem.setOnAction(new EventHandler<ActionEvent>() { //implementing action listener
#Override
public void handle(ActionEvent event) {
text.setText("Some text you want to display"); //set value for controls
paneWithControls.setVisible(true); //display content
}
});
}
}
EDIT:
If you really want to "hide" the scene you can do something like this:
primaryStage.setScene(null);
and later on:
primaryStage.setScene(sceneObject1);
However this is not good way to achieve result you are looking for. Once you have the scene, just change the root of it, or set properties of it's controls. I have mistaken this also when I was learning JavaFX.

How to get the source of all JButton components from an ActionEvent?

I'm trying to implement a simple window that contain two buttons Yes and No.
When clicking on Yes I want to disable the No button and when pressing on No I want to disable the Yes button.
I've implemented:
JButton btnYes = new JButton("Yes");
contentPane.add(btnYes);
btnYes.setActionCommand("Yes");
btnYes.addActionListener(this);
...the same for the No button...
Now I'm catching the event in this method:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Yes"))
{
//I know how to get the button that caused the event
//but I don't know how to disable the OTHER button.
JButton source = (JButton)e.getSource();
//Handle the source button...
}
}
In the above method I have an access to the button that caused the event, but not to the other button.
What is the best way of getting the buttons?
You should just implement ActionListener as a nested class of your Dialog's class, in this case you will have full access to all fields of outer class (in which you should store reference to buttons when your create them).
The bad dirty solution (that should NOT be used) still exists: to navigate to battens through getParent() of JButton and then through getChildren() of parents childrens. Just to show that it is possible anyway.
You could use a JButton array as class member variable and to check which instance didnt cause the event:
for (JButton button: buttonArray) {
if (button != source) {
button.setEnabled(false); // disable the other button
}
}

Categories