Skip to content

#08.1 Button

Valkryst edited this page Oct 25, 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 Font font = FontLoader.loadFontFromJar("Fonts/DejaVu Sans Mono/20pt/bitmap.png",
                                                     "Fonts/DejaVu Sans Mono/20pt/data.fnt",
                                                     1);

        final PanelBuilder builder = new PanelBuilder();
        builder.setFont(font);
        final Panel panel = builder.build();


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

        panel.addComponent(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.setColumnIndex(10);
buttonBuilder.setRowIndex(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.addComponent(buttonBuilder.build());

This builds the button and adds it to the panel.

Result

Clone this wiki locally