Skip to content

#08.1 Button

Valkryst edited this page Nov 10, 2017 · 14 revisions

Create a Button

import com.valkryst.VTerminal.Panel;
import com.valkryst.VTerminal.builder.PanelBuilder;
import com.valkryst.VTerminal.builder.component.ButtonBuilder;
import com.valkryst.VTerminal.font.Font;
import com.valkryst.VTerminal.font.FontLoader;

import java.io.IOException;
import java.net.URISyntaxException;

public class Driver {
    public static void main(final String[] args) throws IOException, URISyntaxException, InterruptedException {
        final Panel panel = new PanelBuilder().build();


        final ButtonBuilder buttonBuilder = new ButtonBuilder();
        buttonBuilder.setPosition(10, 10);
        buttonBuilder.setText("Click Me");
        buttonBuilder.setOnClickFunction(() -> System.out.println("You clicked a button."));

        panel.addComponents(buttonBuilder.build());


        Thread.sleep(50);

        panel.draw();
    }
}

Code Explanation

final ButtonBuilder buttonBuilder = new ButtonBuilder();

Constructs a new ButtonBuilder. You can view the documentation here.

You can reuse the builder, so you won't need to create a new ButtonBuilder every time you want to create a new button.


buttonBuilder.setPosition(10, 10);

This tells the builder to place the button at position (10x, 10y).


buttonBuilder.setText("Click Me");

This sets the text on the button, so this button will display "Click Me".


buttonBuilder.setOnClickFunction(() -> System.out.println("You clicked a button."));

This is an optional setter which gives the button a function to run when it's clicked.

In this case, the function will print "You clicked a button." when the button is pressed.


panel.addComponents(buttonBuilder.build());

This builds the button and adds it to the panel.

Result

Clone this wiki locally