Get Multiple Results from Custom Dialog -- JavaFX - java

I've got a little problem with my JavaFX code. I'm sure you all know that you can get the input from a TextInputDialog with an Optional< String > and .showAndWait(). But what should I do when I have a custom dialog with multiple TextFields and a ChoiceBox? How do I get the results from all of them when clicking OK? I thought about a List<String> but I didn't manage to do it..
Code (Custom Dialog):
public class ImageEffectInputDialog extends Dialog {
private ButtonType apply = new ButtonType("Apply", ButtonBar.ButtonData.OK_DONE);
private ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
public ImageEffectInputDialog(String title) {
setTitle(title);
setHeaderText(null);
GridPane dPane = new GridPane();
Label offsetX = new Label("Offset X: ");
Label offsetY = new Label("Offset Y: ");
Label color = new Label("Shadow Color: ");
TextField offsetXText = new TextField();
TextField offsetYText = new TextField();
ChoiceBox<String> shadowColors = new ChoiceBox<>();
shadowColors.getItems().add(0, "Black");
shadowColors.getItems().add(1, "White");
dPane.setHgap(7D);
dPane.setVgap(8D);
GridPane.setConstraints(offsetX, 0, 0);
GridPane.setConstraints(offsetY, 0, 1);
GridPane.setConstraints(offsetXText, 1, 0);
GridPane.setConstraints(offsetYText, 1, 1);
GridPane.setConstraints(color, 0, 2);
GridPane.setConstraints(shadowColors, 1, 2);
dPane.getChildren().addAll(offsetX, offsetY, color, offsetXText, offsetYText, shadowColors);
getDialogPane().getButtonTypes().addAll(apply, cancel);
getDialogPane().setContent(dPane);
}
}
Code (where I want the results)
if(scrollPane.getContent() != null && scrollPane.getContent() instanceof ImageView) {
// ImageEffectUtil.addDropShadow((ImageView) scrollPane.getContent());
ImageEffectInputDialog drop = new ImageEffectInputDialog("Drop Shadow");
//Want the Results here..
}
I hope someone might be able to help.

First of all, in order to obtain different values of different types (generic solution) just define a new data structure, say Result, which contains fields like offsetX, offsetY and whatever else you need. Next, extend Dialog<Result> instead of just Dialog. Finally, in the constructor of your ImageEffectInputDialog you need to set result converter, as follows:
setResultConverter(button -> {
// here you can also check what button was pressed
// and return things accordingly
return new Result(offsetXText.getText(), offsetYText.getText());
});
Now wherever you need to use the dialog, you can do:
ImageEffectInputDialog dialog = new ImageEffectInputDialog("Title");
dialog.showAndWait().ifPresent(result -> {
// do something with result object, which is of type Result
});

Related

How to access outside variables in an action event handler (javafx button)?

I'm a bit of a javafx noob. I want to make it so this button updates the "GraphView" object called "viewGraph", but I don't know how to properly do this because I can't access outside variables in the setOnAction event. I would also want to make startVertice and endVertice outside of the event class as well but I don't know how to do that.
I would think the answer would be to somehow pass the variables in using parameters but I don't know how to do that.
I tried looking stuff up and trying random syntax stuff but I still dont know what to do. It seems simple though.
WeightedGraph<Campus> graph = new WeightedGraph<>(vertices, edges);
Label startCampus = new Label("Starting Campus: ");
Label endCampus = new Label(" Ending Campus: ");
TextField startText = new TextField();
TextField endText = new TextField();
Button displayShortestPathBtn = new Button("Display Shortest Path");
HBox input = new HBox();
input.getChildren().addAll(startCampus, startText, endCampus, endText, displayShortestPathBtn);
input.setPadding(new Insets(25, 0, 0, 0));
VBox vbox = new VBox();
GraphView viewGraph = new GraphView(graph);
vbox.getChildren().addAll(viewGraph, input);
vbox.setPadding(new Insets(50, 50, 25, 50));
displayShortestPathBtn.setOnAction((event) -> {
int startVertice = Integer.parseInt(startText.getText());
int endVertice = Integer.parseInt(endText.getText());
WeightedGraph<Campus>.ShortestPathTree shortestPathGraph = graph.getShortestPath(startVertice);
ArrayList<Integer> path = (ArrayList)shortestPathGraph.getPath(endVertice);
/*
What I want to do:
viewGraph = new GraphView(graph, path);
*/
});
primaryStage.setTitle("Final Exam");
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
I just want to be able to access the outside variables but idk if that's possible or if I'm approaching the problem correctly in the first place.

Java - Validation of TextField

(SOLVED) Issue 1: I am trying to add a simple verification to my 2 TextFields by checking its values. However, with this code below, I think whats happening is that the try/catch is called as the program starts up (which I tested with the System.out.println() code), therefore always resulting in an error. How can I make it such that this is called only after button 'Finish' is pressed?
(UNSOLVED) Issue 2: Following on from my first issue, how can I make it such that if either my if or my try/catch returns an 'error', then pressing the 'Finish' button doesn't end the code?
Code:
Dialog<Pair<String, Integer>> dialog = new Dialog();
dialog.setTitle("Add new values");
dialog.setHeaderText("Please input name and number");
ButtonType finishButton = new ButtonType("Finish", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(finishButton, ButtonType.CANCEL);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
TextField name = new TextField();
name.setPromptText("Name");
TextField size = new TextField();
number.setPromptText("Number");
grid.add(new Label("Name:"), 0, 1);
grid.add(eventName, 1, 1);
grid.add(new Label("Number:"), 0, 3);
grid.add(eventSize, 1, 3);
dialog.getDialogPane().setContent(grid);
//verification code below
if (eventName.getText() == null || eventName.getText() == "") {
grid.add(new Label("Name is required!"), 0, 0);
}
try {
int size = Integer.parseInt(eventSize.getText());
} catch (NumberFormatException e) {
grid.add(new Label("Size is required!"), 0, 1);
System.out.println("Test failed");
}
This is the code I am trying to learn off from: Here
Firstly, you must compare Strings using the .equals() method. I believe, but am not 100% certain, that the check for null is unnecessary. So, change:
if (eventName.getText() == null || eventName.getText() == "")
to
if (eventName.getText().equals(""))
I am unfamiliar with the Dialog class. However, when I need to implement something like this I like to use JDialog, and put it in a while loop:
JPanel p = new JPanel(new GridLayout(2,2));
JTextField nameField = new JTextField(5);
JTextField numberField = new JTextField(5);
JLabel nameLabel = new JLabel("Name");
JLabel numberLabel = new JLabel("Number");
p.add(nameLabel);
p.add(nameField);
p.add(numberLabel);
p.add(numberField);
while(true){
int result = JOptionPane.showConfirmDialog(null, p, "Please enter Name and Number.", JOptionPane.OK_CANCEL_OPTION);
if(result == JOptionPane.OK_OPTION){
if(nameField.getText().equals("")){
JOptionPane.showConfirmDialog(null, "Invalid input!");
}
else break;
}
}
This code should guide you on how you might be able to check for different inputs, and validate them accordingly. See JOptionPane for more details on the different dialogs you can open.
Hope this helps you.
Dont know if this will help but i made a button that sounds like what your trying to do
//taking input from pop up box
JTextField InputPosX = new JTextField(5);
JTextField InputNegX = new JTextField(5);
JTextField InputY = new JTextField(5);
JPanel ChangeAxisPanel = new JPanel();
ChangeAxisPanel.add(new JLabel("Max X:"));
ChangeAxisPanel.add(InputPosX);
ChangeAxisPanel.add(Box.createHorizontalStrut(15)); // a spacer
ChangeAxisPanel.add(new JLabel("Min X:"));
ChangeAxisPanel.add(InputNegX);
ChangeAxisPanel.add(Box.createHorizontalStrut(15)); // a spacer
ChangeAxisPanel.add(new JLabel("Y:"));
ChangeAxisPanel.add(InputY);
int result = JOptionPane.showConfirmDialog(null, ChangeAxisPanel,
"Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
//if ok is pressed
if (result == JOptionPane.OK_OPTION) {
if(!(InputPosX.getText().isEmpty())){
defaultPosX=Integer.parseInt(InputPosX.getText());
}
if(!(InputNegX.getText().isEmpty())){
defaultNegX=Integer.parseInt(InputNegX.getText());
}
if(!(InputY.getText().isEmpty())){
defaultY=Integer.parseInt(InputY.getText());
}
}
}
});
most of this was gathered from
Here its a good link for gui input windows. also if you are looking for a simpler method you may want to look into jbutton's you can use it to call this window
Jbutton
anyways hope this helped

JavaFx Charts highlight entire curve on hover

I am using a logarithmic scale for a graph and I want to be able to make the individual curves a little wider when hovered over and on top of all the other curves as well as the color key's value too. Below is a picture better illustrating what I want to do (with sensitive data redacted)
Is something like this even possible? And if so what direction should I move in to achieve this?
The first of your cases, which is applying the visual changes when the mouse hovers over the curve, is possible by modifying the Node that represents the Series on the chart, which is a Path. You can apply the changes to the stroke of the Path making it darker, wider and bringing in to the front when the mouse enters and reverting them when the mouse leaves
The second, which is applying the visual changes when hovering over the legend items is still possible but it's not as clean a solution, at least it's not in my implementation below. With a Node lookup you can get the items and cast them to either a Label or a LegendItem which expose the graphic and text. I chose Label to avoid using the internal API
See more here: It is a bad practice to use Sun's proprietary Java classes?
With String comparisons between the legend text and series names you can associate the two assuming each series has a name and that it is unique. If it isn't unique you could compare the stroke fills as well as the names
Important: These assumptions limit this approach and so if possible I'd avoid it
SSCCE:
public class DarkerSeriesOnHoverExample extends Application {
#SuppressWarnings("unchecked")
#Override
public void start(Stage primaryStage) throws Exception {
//LogarithmicNumberAxis source: https://stackoverflow.com/a/22424519/5556314
LineChart lineChart = new LineChart(new LogarithmicNumberAxis(1, 1000000), new NumberAxis(0, 2.25, 0.25));
lineChart.setCreateSymbols(false);
//Values guessed from the screen shot
ObservableList<XYChart.Data> seriesData = FXCollections.observableArrayList(new XYChart.Data(1, 2),
new XYChart.Data(10, 2), new XYChart.Data(100, 2), new XYChart.Data(1000, 1.85),
new XYChart.Data(10000, 1.50), new XYChart.Data(100000, 1.20), new XYChart.Data(1000000, 0.9));
ObservableList<XYChart.Data> series2Data = FXCollections.observableArrayList(new XYChart.Data(1, 2),
new XYChart.Data(10, 2), new XYChart.Data(100, 2), new XYChart.Data(1000, 1.60),
new XYChart.Data(10000, 1.25), new XYChart.Data(100000, 0.95), new XYChart.Data(1000000, 0.65));
ObservableList<XYChart.Data> series3Data = FXCollections.observableArrayList(new XYChart.Data(1, 2),
new XYChart.Data(10, 1.85), new XYChart.Data(100, 1.55), new XYChart.Data(1000, 1),
new XYChart.Data(10000, 0.65), new XYChart.Data(100000, 0.5), new XYChart.Data(1000000, 0.45));
ObservableList<XYChart.Series> displayedSeries = FXCollections.observableArrayList(
new XYChart.Series("Series 1", seriesData), new XYChart.Series("Series 2", series2Data),
new XYChart.Series("Series 3", series3Data));
lineChart.getData().addAll(displayedSeries);
Scene scene = new Scene(lineChart, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
darkenSeriesOnHover(displayedSeries); //Setup for hovering on series (cleaner)
darkenSeriesOnLegendHover(lineChart); //Setup both hovering on series and legend (messier)
}
private void darkenSeriesOnHover(List<XYChart.Series> seriesList){
for(XYChart.Series series : seriesList){
Node seriesNode = series.getNode();
//seriesNode will be null if this method is called before the scene CSS has been applied
if(seriesNode != null && seriesNode instanceof Path){
Path seriesPath = (Path) seriesNode;
Color initialStrokeColor = (Color)seriesPath.getStroke();
double initialStrokeWidth = seriesPath.getStrokeWidth();
seriesPath.setOnMouseEntered(event -> {
updatePath(seriesPath, initialStrokeColor.darker(), initialStrokeWidth*2, true);
});
seriesPath.setOnMouseExited(event -> {
//Reset
updatePath(seriesPath, initialStrokeColor, initialStrokeWidth, false);
});
}
}
}
private void darkenSeriesOnLegendHover(LineChart lineChart){
Set<Node> legendItems = lineChart.lookupAll("Label.chart-legend-item");
List<XYChart.Series> seriesList = lineChart.getData();
//Will be empty if this method is called before the scene CSS has been applied
if(legendItems.isEmpty()){ return; }
for(Node legendItem : legendItems){
Label legend = (Label) legendItem;
XYChart.Series matchingSeries = getMatchingSeriesByName(seriesList, legend.getText());
if(matchingSeries == null){ return; }
Node seriesNode = matchingSeries.getNode();
//seriesNode will be null if this method is called before the scene CSS has been applied
if(seriesNode != null && seriesNode instanceof Path){
Path seriesPath = (Path) seriesNode;
Color initialStrokeColor = (Color)seriesPath.getStroke();
double initialStrokeWidth = seriesPath.getStrokeWidth();
legendItem.setOnMouseEntered(event -> {
updatePath(seriesPath, initialStrokeColor.darker(), initialStrokeWidth*2, true);
});
legendItem.setOnMouseExited(event -> {
//Reset
updatePath(seriesPath, initialStrokeColor, initialStrokeWidth, false);
});
seriesPath.setOnMouseEntered(event -> {
updatePath(seriesPath, initialStrokeColor.darker(), initialStrokeWidth*2, true);
});
seriesPath.setOnMouseExited(event -> {
//Reset
updatePath(seriesPath, initialStrokeColor, initialStrokeWidth, false);
});
}
}
}
private void updatePath(Path seriesPath, Paint strokeColor, double strokeWidth, boolean toFront){
seriesPath.setStroke(strokeColor);
seriesPath.setStrokeWidth(strokeWidth);
if(!toFront){ return; }
seriesPath.toFront();
}
private XYChart.Series getMatchingSeriesByName(List<XYChart.Series> seriesList, String searchParam){
for (XYChart.Series series : seriesList){
if(series.getName().equals(searchParam)){
return series;
}
}
return null;
}
}
Output:
Before vs Hover

java awt color chooser does't show in proper way in JavaFx

#FXML
Private void handleItemBackAction(ActionEvent eve)
{
java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
hex="#"+hex;
Text.setText(hex);
ShortcutButton.setStyle("-fx-background-color: " + hex + ";");
}
When I run this window and click on button at first time color chooser goes behind my actual pane.
When I click on button second time while running it shows at top of all other pane which is correct and so on it works properly.
Then why color chooser not shows in front first time on button click?
The first argument to JColorChooser.showDialog is the parent component of the dialog. You told that method to show the dialog with no parent, so it doesn't know about your other windows.
Instead of using JColorChooser.showDialog, you'll need to embed a JColorChooser instance inside a JavaFX dialog or window:
JColorChooser colorChooser = new JColorChooser(java.awt.Color.CYAN);
SwingNode colorChooserNode = new SwingNode();
colorChooserNode.setContent(colorChooser);
Alert dialog = new Alert(Alert.AlertType.NONE);
// Guarantees dialog will be above (and will block input to) mainStage.
dialog.initOwner(mainStage);
dialog.setTitle("Select a color");
dialog.getDialogPane().setContent(colorChooserNode);
dialog.getDialogPane().getButtonTypes().setAll(
ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> response = dialog.showAndWait();
if (response.filter(r -> r == ButtonType.OK).isPresent()) {
int rgb = colorChooser.getColor().getRGB();
String hex = String.format("#%06x", rgb & 0xffffff);
Text.setText(hex);
ShortcutButton.setBackground(new Background(
new BackgroundFill(Color.valueOf(hex), null, null)));
} else {
System.out.println("User canceled");
}
Of course, you're probably better off using ColorPicker in your main window, so you don't have to create an explicit dialog at all:
final ColorPicker colorPicker = new ColorPicker(Color.CYAN);
colorPicker.setOnAction(e -> {
Color color = colorPicker.getValue();
String hex = String.format("#%02x02x02x",
(int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255));
Text.setText(hex);
ShortcutButton.setBackground(
new Background(new BackgroundFill(color, null, null)));
});
myLayoutPane.getChildren().add(colorPicker);
As an aside, Java variable names should always start with a lowercase letter, to make them easy to distinguish from class names. Consider changing Text to text, and ShortcutButton to shortcutButton.

Libgdx scrollpane issue for a chatwindow class

I am having an issue with using a scrollpane in libgdx. It is going to be used for a chatwindow class. When you press enter the message will be added to the window and you will scroll to the latest posted message..However it doesn't. It misses one message and scrolls to the one before the latest message. Below I've posted the chatwindow class and the method that adds input to it. The textAreaholder is a table that holds everything. The chatField is where you input what you want to post to the chat. The chatarea is the textfield that then becomes added to the table. But as stated..it doesn't scroll properly, the error properly lies somewhere in the keyTyped method.
public ChatWindow(final Pipe<String> chatPipe) {
this.chatPipe = chatPipe;
messageFieldCounter = 0;
white = new BitmapFont(Gdx.files.internal("fonts/ChatWindowText.fnt"), false);
fontSize = white.getLineHeight();
white.scale(TEXT_SCALE);
final TextFilter filter = new TextFilter();
/* Making a textfield style */
textFieldStyle = new TextFieldStyle();
textFieldStyle.fontColor = Color.WHITE;
textFieldStyle.font = white;
textFieldStyle.focusedFontColor = Color.CYAN;
/*Area where all chat appears*/
textAreaHolder = new Table();
textAreaHolder.debug();
/*Applies the scrollpane to the chat area*/
scrollPane = new ScrollPane(textAreaHolder);
scrollPane.setForceScroll(false, true);
scrollPane.setFlickScroll(true);
scrollPane.setOverscroll(false, false);
/*Input chat*/
chatField = new TextField("", textFieldStyle);
chatField.setTextFieldFilter(filter);
/*Tries to make the textField react on enter?*/
chatField.setTextFieldListener(new TextFieldListener() {
#Override
public void keyTyped(final TextField textField, final char key) {
if (key == '\n' || key == '\r') {
if (messageFieldCounter <= 50) {
textAreaHolder.row();
StringBuilder message = new StringBuilder(); //Creates the message
message.append(chatField.getText()); //Appends the chatfield entry
TextArea chatArea = new TextArea(message.toString(), textFieldStyle); //Creates a chatArea with the message
chatArea.setHeight(fontSize + 1);
chatArea.setDisabled(true);
chatArea.setTextFieldFilter(filter);
textAreaHolder.add(chatArea).height(CHAT_INPUT_HEIGHT).width(CHAT_WIDTH);
scrollPane.scrollToCenter(0, 0, 0, 0);
//Scrolls to latest input
chatField.setText("");
//InputDecider.inputDecision(message.toString(), chatPipe); //TODO: Change the filter
//chatPipe.put(message.toString()); //TODO: testing
}
}
}
});
Problems could occur, because you're using scrollPane.scrollToCenter(float x, float y, float width, float height) with zero parameters:
scrollPane.scrollToCenter(0, 0, 0, 0);
scrollToCenter method requires that parameters to be correctly supplied. So, try to supply message bounds.
The second reason could be because you call scrollToCenter before table do layout itself. So, try overwrite table's layout method and call scrollToCenter after:
#Override
public void layout()
{
super.layout();
if (new_messages_added)
{
scrollPane.scrollToCenter(...)
}
}

Categories