Question: * * * JAVAFX * * * How can this be cleaned up and make the observeStack and stackListView reflect the stack more effectively?: package

***JAVAFX*** How can this be cleaned up and make the observeStack and stackListView reflect the stack more effectively?:
package jthomas270m13a1;
import java.util.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*This is closer but I know it's not perfect - mainly, the stack doesn't fill right
and the listview doesn't add them to the top or always remove them*/
public class VisualStack extends Application{
Button push = new Button("PUSH");
Button pop = new Button("POP");
Button peek = new Button("PEEK");
TextField textInput = new TextField();
Label errorText = new Label();
Label title = new Label("My Visual Stack. Stack Size: 23");
Label popLabel = new Label();
Label peekLabel = new Label();
Label error = new Label("Error:");
Label stackLabel = new Label("Stack");
Stack stack = new Stack<>();
ObservableList observeStack = FXCollections.observableArrayList();
ListView stackListView = new ListView<>(observeStack);
Pane backgroundPane = new Pane();
public void start(Stage primaryStage){
stack.ensureCapacity(23);
errorText.setMinSize(600,10);
//errorText.setTextFill(Color.BLACK);
errorText.setBackground(Background.fill(Color.WHITE));
title.setTextFill(Color.WHITE);
error.setTextFill(Color.WHITE);
stackLabel.setTextFill(Color.WHITE);
popLabel.setTextFill(Color.WHITE);
peekLabel.setTextFill(Color.WHITE);
textInput.setMinSize(10,10);
push.setMinSize(100,10);
push.setOnAction(e ->{
stack.push(textInput.getText());
observeStack.add(textInput.getText());
textInput.clear();
});
pop.setMinSize(100,10);
pop.setOnAction(e ->{
try{
if(!stack.isEmpty()){
popLabel.setText((String)stack.pop());
observeStack.remove(stack.pop());
stack.pop();
}
}
catch(Exception er){
errorText.setText(er.toString());
}
});
peek.setMinSize(100,10);
peek.setOnAction(e ->{
try{
if(!stack.isEmpty()){
peekLabel.setText((String)stack.peek());
stack.peek();
}
}
catch(Exception er){
errorText.setText(er.getMessage());
}
});
backgroundPane.setBackground(Background.fill(Color.GREEN));
backgroundPane.getChildren().addAll(title,push, pop,peek,
textInput, popLabel,peekLabel,stackListView, stackLabel,
error, errorText);
title.setLayoutX(30);
title.setLayoutY(30);
push.setLayoutX(30);
push.setLayoutY(80);
textInput.setLayoutX(150);
textInput.setLayoutY(80);
pop.setLayoutX(30);
pop.setLayoutY(130);
popLabel.setLayoutX(150);
popLabel.setLayoutY(130);
peek.setLayoutX(30);
peek.setLayoutY(180);
peekLabel.setLayoutX(150);
peekLabel.setLayoutY(180);
stackLabel.setLayoutX(350);
stackLabel.setLayoutY(50);
stackListView.setLayoutX(350);
stackListView.setLayoutY(80);
error.setLayoutX(30);
error.setLayoutY(500);
errorText.setLayoutX(80);
errorText.setLayoutY(500);
Scene scene = new Scene(backgroundPane,800,600);
primaryStage.setTitle("Visual Stack");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!