JavaFX button not working - java

I started writing a simple music player, but when I tested it, I found that the button wasn't working. I replaced the code in the button's event handler with a simple System.out.println("Got to button");, but it didn't write to the console.
The fxID of the button is playButton.
The #fxml declaration is:
#FXML
private Button playButton;
Initialize:
#FXML
public void initialize() {
this.bindGuiComponentsToViewModel();
this.setEventActions();
}
SetEventActions:
private void setEventActions() {
this.playButton.setOnAction(event -> this.handlePlayAction());
}
handler:
private void handlePlayAction() {
System.out.println("got to play");
}
I haven't been able to find anything through google for the past hour, I've tried making playButton.setDisable(false);, and nothing has worked so far.
Any help is greatly appreciated!

Why not testing the regular way (using an implementation of anonymous class) if it works then the button is bound
yourButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
System.out.println("got to play");
}
});
other solutions :
-Try to use Logger
-Try to Debug the code by adding break point in
private void handlePlayAction() {
x System.out.println("got to play");
}

Related

How To Have Code Run For A Real Time Update For Condition Met Without The Need Of An Event(Button For Example) Java

I am trying to make a password checker, and I am trying to have an icon set as X until the user meets the condition of the password being at least 8 characters long. I am trying to set up a while loop for while the program is running, which will in turn run another while loop which will update the icon. I am doing this because a want it to always be checking to see if the condition is met and not need the user to click a button for example. I have placed the code inside the initComponents of the public form PasswordCheckerUI, but when I hit run on the program, it says running but my GUI doesn't pop up. How would I change the code to make this work(if needed) or where would I have to move it to? I have tried making a public static void with the code and calling it within the initCompnents, but it yielded the same results. Lastly, I tried to call the method within the main method, but the variables aren't static so that did not work either
public class PasswordCheckerUI extends javax.swing.JFrame {
public PasswordCheckerUI() {
initComponents();
while (Thread.currentThread().isAlive()) {
while (txtPassword.getText().length() < 8) {
lblMinCharIcon.setIcon(X);
if (txtPassword.getText().length() >= 8) {
lblMinCharIcon.setIcon(Check);
}
}
}
}
}
Events in Swing don't just happen when the user clicks on a button - they happen all the time (when moving the mouse, when clicking, when editing text, when ...) and they are therefore the best way to solve your problem.
You can for example listen the document change events on the txtPassword and change the icon depending on the new length of the password:
public class PasswordCheckerUI {
private JTextField txtPassword;
private JLabel lblMinCharIcon;
public PasswordCheckerUI() {
txtPassword = new JTextField(40);
txtPassword.getDocument().addDocumentListener(
new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
checkPasswordLen(txtPassword);
}
#Override
public void removeUpdate(DocumentEvent e) {
checkPasswordLen(txtPassword);
}
#Override
public void changedUpdate(DocumentEvent e) {
checkPasswordLen(txtPassword);
}
}
);
}
private void checkPasswordLen(JTextField tf) {
if (tf.getText().length() < 8) {
lblMinCharIcon.setIcon(x);
} else {
lblMinCharIcon.setIcon(check);
}
}
}

Java - Change which method is used through radio buttons

I am trying to make the radio button selection choose which of the encryption methods is used by the program
#FXML
public void atbashChooser(ActionEvent event) {
}
#FXML
public void caesarChooser(ActionEvent evnt) {
}
#FXML
public void encryptShit(ActionEvent event) {
if (atbashButton.isSelected()){
encryptField.setText(new AtbashCipher().encrypt(messageField.getText()));
}
else if (caesarButton.isSelected()){
encryptField.setText(new CaesarCipher().encrypt(messageField.getText()));
}
}
#FXML
public void decryptShit(ActionEvent event) {
if (atbashButton.isSelected()){
decryptField.setText(new AtbashCipher().decrypt(messageField.getText()));
}
else if (caesarButton.isSelected()){
decryptField.setText(new CaesarCipher().decrypt(messageField.getText()));
}
}
This does obviously not work, but is there a way to do this in a somewhat easy way? I can share the FXML code as well if this is necessary.

Thread for controlling components in JFXDrawer

I'm using a JFXDrawer, SalesDrawer containing a VBox and 2 JFXButtons inside it. The contents of the JFXDrawer have a separate controller, SalesDrawerController.java. The controller file, SalesController.java that contains the JFXDrawer consists of 2 anchor panes that I want to set visible on click of the buttons in the JFXDrawer. Till now I was using a set of static boolean variables and making sure on click of a button in the JFXDrawer one of the variables is set to true. Then in the SalesController.java, I used a TimerTask object to check which of these variables were true and set the needed anchor pane to visible. Is there a better way of doing this?
SalesDrawerController.java
public class SalesDrawerController implements Initializable {
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
private void button1Hit(MouseEvent event) {
SalesController.SD[0]=true;
}
#FXML
private void button2Hit(MouseEvent event) {
SalesController.SD[1]=true;
}
}
SalesController.java
public class SalesController implements Initializable {
public static boolean SD[]= {false,false};
static boolean tock=true;
#FXML
private AnchorPane eq_newpane;
#FXML
private AnchorPane eq_delpane;
#FXML
private JFXHamburger SalesHam;
#FXML
private JFXDrawer SalesDraw;
public void initialize(URL url, ResourceBundle rb) {
eq_newpane.setVisible(true);
eq_delpane.setVisible(false);
eq_newpane.setDisable(false);
eq_delpane.setDisable(true);
VBox box = FXMLLoader.load(getClass().getResource("/fxml/SalesDrawer.fxml"));
SalesDraw.setSidePane(box);
HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(SalesHam);
transition.setRate(-1);
SalesHam.addEventHandler(MouseEvent.MOUSE_PRESSED,(e)->{
transition.setRate(transition.getRate()*-1);
transition.play();
if(SalesDraw.isShown()){
SalesDraw.close();
SalesDraw.toBack();
}
else{
SalesDraw.toFront();
SalesDraw.open();
}
});
threadtock();
}
public void threadtock() {
final java.util.Timer timer = new java.util.Timer();
final TimerTask delayedThreadStartTask;
delayedThreadStartTask = new TimerTask() {
public void run() {
try
{
if(tock){
if(SD[0])
{
eq_newpane.setVisible(true);
eq_delpane.setVisible(false);
eq_newpane.setDisable(false);
eq_delpane.setDisable(true);
}
else if(SD[1]){
eq_delpane.setVisible(true);
eq_newpane.setVisible(false);
eq_newpane.setDisable(true);
eq_delpane.setDisable(false);
}
if(tock)
{
threadtock();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
timer.schedule(delayedThreadStartTask, 500);
};
}
Use synchronization mechanisms so you do not inherit deadlocks in your code from 2 or more threads attempting to operate on the same variable at once.
Figuring out how to properly synchronize your threads can be a pain, and it is very hard to explain, let alone debug. So, my best advice is to use google for "Java Concurrency" and "Java Thread Synchronization".
Otherwise, you should start a thread/threads from your main application to preform your intended operations

In GWT is there a way to create a KeyPressEvent for the entire view instead of a single input element?

Right now I have the following code working:
#UiHandler("usernameTextBox")
void onUsernameTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
#UiHandler("passwordTextBox")
void onPasswordTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
void keyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
submit();
}
}
I would like the ability to have just one listener for all elements on the view without duplicating an event for each textbox.
The end goal is that if they press enter, regardless of where they are on the page, it should submit the form.
Thanks!
What works, but still requires you to specify it for each widget, but doesn't require duplicate code:
#UiHandler({"usernameTextBox", "passwordTextBox"})
void onPasswordTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
Yes jackcrews is correct. Also you can try the following. It may be VerticalPanel, DockLayoutPanel etc....
UiBinder.ui.xml
<gwt:VerticalPanel ui:field="mainPanel">
<gwt:Label>Name</gwt:TextBox>
<gwt:TextBox ui:field="textBox">
</gwt:VerticalPanel>
Main.java
#UiField
VerticalPanel mainPanel;
public Main() {
focushandler();
}
void focusHandler() {
mainPanel.addDomHandler(new Handler(), KeyPressEvent.getType());
}
final class Handler implements KeyPressHandler {
#Override
public void onKeyPress(KeyPressEvent event) {
//Code what you expect
}
}
Actually this has more number of lines. But it is good practice.
Regards,
Gnik
I found out that the g:FocusPanel allows me to capture events for everything inside the panel.
#UiHandler("focusPanel")
void onFocusPanelKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
submit();
}
}

Having a problem getting my ActionListeners, Handlers, and GUI to communicate

So I am trying to get my GUI to work. When I run the code below, it does nothing, and I'm sure I'm probably just doing something dumb, but I am completely stuck...
public void actionPerformed(ActionEvent e){
UI.getInstance().sS++;
if((UI.getInstance().sS %2) != 0){
UI.getInstance().startStop.setName("STOP");
UI.getInstance().change.setEnabled(false);
}else if(UI.getInstance().sS%2 == 0){
UI.getInstance().startStop.setName("START");
UI.getInstance().change.setEnabled(true);
}
}
public void setStartListener(StartHandler e){
this.startStop.addActionListener(e);
}
sS is an int that increments every time the button startStop is clicked. change is also a button.
not really an answer, but I think your code would be simpler if you used a boolean instead of an int, something like:
public void actionPerformed(ActionEvent e){
final boolean isEnabled = UI.getInstance().change.isEnabled();
if(isEnabled){
UI.getInstance().startStop.setName("STOP");
}else{
UI.getInstance().startStop.setName("START");
}
UI.getInstance().change.setEnabled(!isEnabled);
}
Here's an example that shows a different approach to managing a Start/Stop button. It uses an instance of javax.swing.Timer to pace updates. Encapsulating the control button and display label may simplify maintenance. This variation illustrates adding a third command to pause updates.
private static final String Start = "Start";
private static final String Stop = "Stop";
…
private static void create() {
…
final JButton button = new JButton(Stop);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (Stop.equals(cmd)) {
jtl.stop();
button.setText(Start);
} else {
jtl.start();
button.setText(Stop);
}
}
});
…
}
More generally, use Action to encapsulate functionality for use elsewhere in your program. This example "exports several actions that make it easy to use them in a control panel."

Categories

Resources