Appending items to javafx GridPane on event - java

I have an application that uses the javafx GridPane layout manager, with one button affixed to it. Currently when I click on the button, I have it call a function and write to command line the result.
What I'm wondering is instead of writing to command line, how could I instead send the result of my function call to the grid itself, displaying it in say a text field or what have you? Currently this is the code that creates the UI
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 25, 0, 25));
Button btn = new Button();
btn.setText("Get Button");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String result = staticClass.getData(1, "A");
System.out.println(result );
}
});
grid.add(btn, 0, 0;

Found that to perform what I was after, you need to use the setText method, which I believe is what Uluk Biy was alluding too in his comments. For those that run into the same issue as I did down the road, here's a code sample to help illustrate.
final Label textDisplayLabel = new Label();
#Override
public void handle(ActionEvent event) {
textDisplayLabel.setText(calcTime.getTimestampDiff(new DateTime(1976, 9, 11, 10, 59, 0, 0), new DateTime(2012, 11, 11, 10, 59, 0, 0)));
}
});
then just place textDisplayLabel to where you want it on the UI and viola.

Related

JavaFX change label after button click

im trying to show one line of matrix each time. But when button is pressed i want to show next line of that matrix. My idea was that i show line with index "index" and create action on button press that add 1 to variable "index". It doesnt semms to be good idea, because its not working. Its showing only the first line, and never changes.
public class GUI extends Application {
int index = 0;
public static int save[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}};
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Label label = new Label(Arrays.toString(save[index]));
Button next = new Button();
next.setText("Next");
next.setOnAction(e -> {
dalsi();
});
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(8);
grid.setHgap(10);
GridPane.setConstraints(label, 5, 6);
GridPane.setConstraints(next, 6, 13);
grid.getChildren().addAll(label, next);
Scene scene = new Scene(grid, 250, 180);
primaryStage.setScene(scene);
primaryStage.setTitle("QuickSort");
primaryStage.show();
}
public void dalsi() {
if (index < Quicksort.delka - 1) {
index++;
}
}
}
To make label text change, you need to call setText on the label when you want the text to change.
Make your label a member variable for the class, then write:
label.setText(
Arrays.toString(save[index])
);
after you call index++ in your dalsi() method.

The setText() method is not working (Java)

I've come across a problem where when I want to use the setText() method on a label, it won't change the label's text. I've searched for a long time, but couldn't find any solution.
Here is a sample of the code:
class OptionsListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if (optionsI1.isSelected()){
lesu1.setText("8:30");
}
}
}
I display all the components I use into another class, everything is displayed correctly.
I've tried simplifying the code by doing something like this:
class OptionsListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if (optionsI1.isSelected()){
System.out.println("bla");
}
}
Which seems to work fine and display the message "bla".
Anything I'm missing here?
The declaration of my elements (only showing the labels and menu's in the order I coded it):
// labels //
lesu1 = new JLabel("1");
lesu1.setBounds(8, 39, 20, 22);
lesu2 = new JLabel("2");
lesu2.setBounds(8, 69, 20, 22);
lesu3 = new JLabel("3");
lesu3.setBounds(8, 99, 20, 22);
lesu4 = new JLabel("4");
lesu4.setBounds(8, 129, 20, 22);
lesu5 = new JLabel("5");
lesu5.setBounds(8, 159, 20, 22);
lesu6 = new JLabel("6");
lesu6.setBounds(8, 189, 20, 22);
lesu7 = new JLabel("7");
lesu7.setBounds(8, 219, 20, 22);
lesu8 = new JLabel("8");
lesu8.setBounds(8, 249, 20, 22);
dag = new JLabel("07/08");
dag.setBounds(5, 15, 36, 13);
// menubar //
menu = new JMenuBar();
options = new JMenu("Opties");
optionsI1 = new JCheckBoxMenuItem("Weergeef de lesuren in uren");
optionsI1.addItemListener(new OptionsListener());
menu.add(options);
options.add(optionsI1);
This is the order I placed those components. I also added them to the panel in this order.
Your Label lesu1 is not placed on any component like panel,button etc.
Add lable to some component.
For ex.
menu.add(lesu1);
I found out what the problem was.
I was confused and used a constructor for everything instead of methods. That solved it.
As I said: I'm a beginner in Java.

Absolute layout- centering of buttons

I'm working on a project, and I need to make two buttons absolutly centered on the scene.
I haven't found any built in pane option to do the work, so I had to use alot of binding.
The code for that is really long and I'm quite sure there is an easier way to do the job.
Here is a demo: http://screencast.com/t/pvi5WLko
So what I want to know is, if there's an easy way to do the same thing and preferably with built in panes or something.
I want the buttons to be centered, no matter how the window is resized. Example:
The probably easiest way would be to use a VBox:
public void start(final Stage stage) throws Exception {
final Button button0 = new Button("Start learning");
final Button button1 = new Button("Customize");
final VBox box = new VBox();
box.setFillWidth(true);
box.getChildren().setAll(button0, button1);
box.setAlignment(Pos.CENTER);
stage.setScene(new Scene(box));
stage.setWidth(200);
stage.setHeight(100);
stage.show();
}
Another possible way of doing this is with a GridPane:
public void start(final Stage stage) throws Exception {
final Button button0 = new Button("Start learning");
final Button button1 = new Button("Customize");
final GridPane cPane = new GridPane();
cPane.getChildren().addAll(button0, button1);
GridPane.setConstraints(button0, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER);
GridPane.setConstraints(button1, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER);
final ColumnConstraints columnn0 = new ColumnConstraints();
columnn0.setPercentWidth(100);
cPane.getColumnConstraints().addAll(columnn0);
final RowConstraints row0 = new RowConstraints(1);
row0.setPercentHeight(50);
final RowConstraints row1 = new RowConstraints(1);
row1.setPercentHeight(50);
cPane.getRowConstraints().addAll(row0, row1);
stage.setScene(new Scene(cPane));
stage.setWidth(200);
stage.setHeight(100);
stage.show();
}
The idea here would be to configure the rows and columns in the grid to fill your scene using the according constraints objects. The above defines one column and two rows. You can then align your components within the cells of the Grid, using GridPane.setConstraints(...).
You might want to alter the code slightly to have the top button align to VPos.BOTTOM and the lower one to VPos.TOP, depending on whether you want the buttons to stick together (you'll then have to define a margin for both, of course).

Improper Alignment Error with Java AWT. Cannot get my label to move

I have put a label into my frame, but it refuses to move. SetBounds() is not working, and I get an improper alignment error if I put any argument past "Result" below that isn't 0, 1, or 2, none of which put it in the correct place. Here's where I declare the Label:
Label result = new Label("Result.", 3);
Here's the SetBounds statement:
result.setBounds(0, 1500, 100, 20);
This program I am writing, I simply just want to have the user input 2 numbers, add them, and print the result using GUI components. The result is the label which refuses to change. The code of the entire program is below, and the program is still not done yet, but if you compile it, result is always stuck to the left and I want it to be at the same level as the TextFields. This problem is actually happening with the other labels, Help1, and Help2. Please don't tell me I have to use swing! I dislike swing.
I have yet to change the event to where it adds the user inputs. I copied the event from a previous program.
The code: (Sorry for no comments, but it's not a huge program)
import java.awt.*;
import java.awt.event.*;
public class MouseClick {
TextField number1;
TextField number2;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}
public MouseClick() {
Frame f = new Frame("Addition Time!");
Button button = new Button("Click Here To Add The Two Numbers.");
button.setBounds(175, 250, 230, 30);
button.addMouseListener(new MyMouseListener());
f.add(button);
Label help1 = new Label("Enter the first number below.");
Label help2 = new Label("Enter the second number below.");
Label exprsn1 = new Label("+", 0);
Label exprsn2 = new Label("=", 0);
Label result = new Label("Result.", 3);
number1 = new TextField("TextField1", 100);
number2 = new TextField("TextField2", 100);
help1.setBounds(50, 80, 150, 20);
help2.setBounds(250, 80, 150, 20);
exprsn1.setBounds(00, 80, 30, 30);
exprsn2.setBounds(00, 80, 30, 30);
number1.setBounds(50, 100, 100, 20);
number2.setBounds(250, 100, 100, 20);
result.setBounds(0, 1500, 100, 20);
f.add(number1);
f.add(number2);
f.add(help1);
f.add(help2);
f.add(result);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.setSize(600, 300);
f.setVisible(true);
}
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
String S = number1.getText();
number2.setText(S);
}
}
}
The error is because 3 is not an allowed value for parameter "alignment" in the constructor
Label(String text, int alignment)
Are you getting confused with TextField() which has a similar constructor?
TextField(String text, int columns)
The reason the label is not appearing in the correct location is because you've not specified your using null layout explicitly. You need this line:
public MouseClick() {
Frame f = new Frame("Addition Time!");
f.setLayout(null); // add this line

Java: Popup not always visible

I use a Popup to display an update progress. I put a semi-transparent panel above the main window for effect. In far the most cases the Popup is visible but on some computers it's not. It seems to be related to specific computers. Does anyone know a solution or have a better way to implement this?
//Disable main components
tabs.setEnabledAt(0, false);
tabs.setEnabledAt(1, false);
comPorts_CB.setEnabled(false);
getinfo_B.setEnabled(false);
//Add effect panel
pop_effect_panel = new JPanel();
pop_effect_panel.setBackground(new Color(255, 255, 255, 192));
pop_effect_panel.setBounds(0, 0, 1000, 1000);
pop_effect_panel.setLayout(null);
pop_effect_panel.setOpaque(true);
getContentPane().add(pop_effect_panel);
getContentPane().setComponentZOrder(pop_effect_panel, 0);
getContentPane().setEnabled(false);
pop_effect_panel.invalidate();
//Create pop-up panel
pop_panel = new JPanel();
pop_panel.setBackground(BACKGROUND_COLOR);
pop_panel.setSize(300, 300);
pop_panel.setLayout(null);
pop_panel.setOpaque(true);
pop_panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
pop_progress_TA = new NonSelectableTextArea();
pop_progress_TA.setBounds(2, 2, 296, 268);
pop_progress_TA.setBackground(BACKGROUND_COLOR);
pop_panel.add(pop_progress_TA);
pop_progress_bar = new JProgressBar();
pop_progress_bar.setBounds(1, 270, 240, 28);
pop_progress_bar.setValue(0);
pop_progress_bar.setStringPainted(true);
pop_progress_bar.setString("");
pop_panel.add(pop_progress_bar);
pop_ok_B = new JButton("OK");
pop_ok_B.setBounds(241, 270, 57, 28);
pop_ok_B.setEnabled(false);
pop_panel.add(pop_ok_B);
final Popup popup = PopupFactory.getSharedInstance().getPopup(getContentPane(), pop_panel, 100, 250);
popup.show();
pop_ok_B.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
popup.hide();
//Remove effect panel
getContentPane().remove(pop_effect_panel);
getContentPane().validate();
//Enable main components
tabs.setEnabledAt(0, true);
tabs.setEnabledAt(1, true);
comPorts_CB.setEnabled(true);
getinfo_B.setEnabled(true);
}
});
pop_progress_TA.requestFocusInWindow();
Ensure that:
The progress dialog is created and updated on the EDT.
The long running task is completed off the EDT.
consider that would be better look for/using un-decorated Modal JDialog or JWindow (by defalut un-decorated) instead of JPopup

Categories