jQuery: Disable a button

At times, you may want the submit button of a form or one of its text inputs to be disabled until the user has performed a certain action (e.g., checking the “I’ve read the terms” checkbox).

Add the disabled attribute to your input so you can enable it when you want.  You can try clicking on the submit button to see its effect.

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <title>Check image load</title>      
        <script>
            $(function () {
                $('input[type="submit"]').prop('disabled', true);
            })
        </script>
    </head>
    <body>
        <form>
            <input type="text" name="first" value="Taylor Swift">
            <br><br>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

button

All you need to do is run the prop method again on the input, but set the value of disabled to false to enable the button.

$('input[type="submit"]').prop('disabled', false);

JavaFX: Button Sample

There is a button sample from Oracle website and I tested running it just to see its output.

If you have like to use images for your buttons, you can refer to this example.

Button Sample
package buttonsample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class ButtonSample extends Application {

    private static final Color color = Color.web("#464646");
    Button button3 = new Button("Decline");
    DropShadow shadow = new DropShadow();
    Label label = new Label();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Button Sample");
        stage.setWidth(400);
        stage.setHeight(300);

        label.setFont(Font.font("Times New Roman", 22));
        label.setTextFill(color);

        Image imageDecline = new Image(getClass().getResourceAsStream("orange.png"));
        Image imageAccept = new Image(getClass().getResourceAsStream("green.png"));

        VBox vbox = new VBox();
        vbox.setLayoutX(20);
        vbox.setLayoutY(20);
        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();

        Button button1 = new Button("Accept", new ImageView(imageAccept));
        button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
        
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        Button button2 = new Button("Accept");
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        button3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Declined");
            }
        });

        button3.addEventHandler(MouseEvent.MOUSE_ENTERED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        button3.setEffect(shadow);
                    }
                });

        button3.addEventHandler(MouseEvent.MOUSE_EXITED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        button3.setEffect(null);
                    }
                });

        hbox1.getChildren().add(button2);
        hbox1.getChildren().add(button3);
        hbox1.getChildren().add(label);
        hbox1.setSpacing(10);
        hbox1.setAlignment(Pos.BOTTOM_CENTER);

        Button button4 = new Button();
        button4.setGraphic(new ImageView(imageAccept));
        button4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        Button button5 = new Button();
        button5.setGraphic(new ImageView(imageDecline));
        button5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Declined");
            }
        });

        hbox2.getChildren().add(button4);
        hbox2.getChildren().add(button5);
        hbox2.setSpacing(25);

        vbox.getChildren().add(button1);
        vbox.getChildren().add(hbox1);
        vbox.getChildren().add(hbox2);
        vbox.setSpacing(10);
        ((Group) scene.getRoot()).getChildren().add(vbox);

        stage.setScene(scene);
        stage.show();
    }
}

Below screenshot is the output from running the program.

buttonsample

The green.png and orange.png graphic files are as below.

green orange

iPhone home button

An article in Digitimes of Taiwan has claimed that Apple would be ditching the home button and integrate the finger print recognition in the button to the touch screen.

The article said that Apple is experimenting with a new technology called Touch and Display Driver Integration.  If the rumour is true, it means Apple would need only one chip to control the display and its touch technology including forced touch and finger print recognition.

Electronic touch button has been around for a long time.  About 6 years back, I had bought a 24inch Samsung monitor and the monitor came with no buttons at all.  It took me a while to figure out how to switch on the the monitor as the power button on the bottom right was not easily recognisable as a power button.

If you have used a Samsung Galaxy before, it has already done away with mechanical buttons for a long time.  But for the iPhone, you would be wondering why the button is still there.

Now that Apple had integrated the touch ID to the button, it is obvious why the button is still there.

If you have an iPhone, you will find that if there is a part that could fail easily, it is the home button.  The reason could be that iPhone goes to sleep mode easily and this force us to touch the button each time we need to turn it on.

Mechanical button is prone to failure.  But because it is mechanical, you could feel the clicking sound each time you click on it.

Have you heard of a story on how Steve Jobs could pay attention to very little details.  It was said that he requested his iPod team to redesign the iPod headphone jack simply because it did not produce any clicking sound when you put the phone jack into the iPod.

I actually thought that having the mechanical button does make the phone feel more humane.  The one reason that I thought it would be good to have the button gone was that game gadget designers would be able to design a controller that could clip easily on both sides of the of the phone.

When it comes to gaming, I still prefer to play on a well designed controller rather than playing it on the touch screen.  Mechanical controller like a playstation controller Sixaxis, I believe, is still a better choice for many gamers.

JavaFX: Which Button Is Clicked

This entry is part 24 of 55 in the series JavaFX

To test which button is clicked, I have made use of setOnAction() method of the button object and called the relevant function accordingly.

package javafxapplication20;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class JavaFXApplication20 extends Application {

    Button movButton = new Button("Movie");

    Button gameButton = new Button("Game");

    Label lb = new Label("Which Do You Prefer?");

    @Override
    public void start(Stage stage) {

        BorderPane border = new BorderPane();

        border.setPrefSize(500, 400);

        movButton.setOnAction(e -> movieButton());
        
        gameButton.setOnAction(e -> gameButton());

        HBox hbox = addHorizontalBox();
        border.setTop(hbox);
        border.setLeft(addVBox());

        Scene scene = new Scene(border);
        stage.setScene(scene);
        stage.setTitle("Which Button");
        stage.show();
    }

    private void movieButton() {

        lb.setText("Movie Button Is Clicked");

    }
    
    private void gameButton() {

        lb.setText("Game is Chosen");

    }

    private HBox addHorizontalBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);   // Gap between nodes

        hbox.getChildren().addAll(movButton, gameButton);

        return hbox;
    }

    private VBox addVBox() {

        VBox vbox = new VBox();
        vbox.setPadding(new Insets(50)); // Set all sides to 10

        lb.setFont(Font.font("Consolas", FontWeight.BOLD, 24));
        vbox.getChildren().add(lb);

        return vbox;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

chosen

 

JavaFX: Removing CSS effects to a Button

This entry is part 23 of 55 in the series JavaFX

When a mouse is moved over a button, we can apply an effect such as changing the color of the button.

However, when the mouse is out of the button or out of any other nodes, we will want it to get back to its original state.

We can use remove() method if we have only one class or removeAll() method if we have more than one CSS class.

package javafxapplication19;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXApplication19 extends Application {

    Button btn = new Button("Codecrawl");

    DropShadow ds = new DropShadow();

    @Override
    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        StackPane stackpane = new StackPane();

        Scene scene = new Scene(stackpane, 300, 250);
        scene.getStylesheets().add("javafxapplication19/style.css");
        stage.setScene(scene);

        btn.setOnMouseEntered(e -> mouseEntered());

        btn.setOnMouseExited(e -> mouseExited());

        stackpane.getChildren().add(btn);

        stage.show();
    }

    public void mouseEntered() {

        btn.getStyleClass().add("button1");
        btn.setText("Mouse In");

    }

    public void mouseExited() {

        btn.getStyleClass().remove("button1");
        btn.setText("Mouse Out");

    }

    public static void main(String[] args) {
        launch(args);
    }

}

remove

Here is the simple style.css that we use to set the color of the button.

.button1 {
    
    -fx-base: blue;    
    
}