How do you auto update a string(in JLabel) from textboxes? - java

I am a computer science student doing my first project on the side. I deliver pizza so i figured I would make a program that aids delivery drivers. I am just starting on it today.
Here is a pic:
Right now, you have to press enter after typing in a value in any of the boxes. I want to make it where the value under hourly earnings autoupdates when tips and hours worked boxes have numbers in them. With no need to press enter.

Use DocumentListener for your textfields and update the appropriate JLabel in your Listener implementation.

Depending on when you want the update to occur, you could (as has been suggested) a DocumentListener, which will allow updates in real time (as you type) or if you just want the values to change when you move to the next field (ie tab or click), you could use a FocusListener

Related

Dual-input for 2 TextFields in JavaFX on one machine

I am working on a project in Java using JavaFX. The project is for a Cafe notification system that has a front end for the customers and has a front end for the cooks and I am running a dual display where one display has the user front end and another display for the cooks front end. The problem is that if there are 2 keyboards connected to the raspberry pi, both input will be in the same TextField because you can't select more than one TextField on a single machine.
What I think could work is that the keyboard for cooks is just the number pad and the users have a normal keyboard without a numberpad. So if there was a way to grab input from the numberpad (since they have different key codes then the numbers on the top of a keyboard) and enter text into the cook TextField without the window being in focus, that would solve my issue I just don't know how to go about that. The user input would just be entered normally since it will be focused on the user UI and they can click for different text fields.
Is that method possible? Is there a better method? Or is this whole thing not feasible and I should just switch to 2 separate raspberry pi's? (please see if it is possible before going straight to 2 devices)
For reference the github project is here: https://github.com/thatoneguy43/CafeBuzzerSystem
I'm hoping I didn't leave any important information out, this is my first post here. If you need any more info from me or have any questions ask away.

How to check, after a Button is pressed, if a TextField was filled in

I believe my question is pretty simple but i am not really sure if i have the solution to it. The closest i've found to it is something like:
if (exampleTextField.getText().trim().isEmpty()){ ... }
All in all, i want to know how to filter the objects that are created based on that specific fact.
As an example (i'll just say something random, hope it makes sence):
I have 2 types of student classes, a class named "UGStudent" for UnderGraduate Students and a class named "PGStudent" for Post Graduate Students.
Let's suppose i have a JFrame with some TextFields and one single JButton "Add Student". Let's also suppose that the TextFields i have are 4 : "Last Name", "Full Name", "GPA" and "Graduation Year". When the button is pressed a new object is created and added into a respective ArrayList. So as you'd probably guess, if the field "Graduation Year" is filled we'd like a "PGStudent" to be created, otherwise we'd like a "UGStudent" to be created.
Any suggested solutions?
Not sure of your exact question, but if you're wanting to listen for whether a JTextField's text has been modified, for instance to know whether or not to enable a JButton or its Action, then use a DocumentListener.
Swing, as with most event-driven GUI libraries, has many listener types that will enable you to listen to the state of the components of your GUI.

using DefaultListMethod to make custom buttons via showOptionDialog

I am just learning AWT / Swift / JavaFx recently and I feel like I have learned a lot but have barely scratched the surface. There might be a much easier way to do this BUT, I am trying to make a GUI button in eclipse that calculates the distance between two objects that the user creates. Lets call them Robots for now. So, I have one button that allows the user to create the Robots and it stores them in a DefaultListModel (listModel) and displays them in a Jlist (list) below the buttons. When the user then clicks on a robot, another button becomes actice and allows them to calculate the distance between them (one of the parameters of the robots is their location on a grid). I have all that worked out but my problem is that I am trying to make it to where they have to select two different Robots. At first I thought I could let them select two Robots and then make the computeDistance button becomes active, but I am not really sure how to do that because the only way I can select more than one object in the JList is to cntrl click and I don't want the user to have to know that trick.
My next idea was to allow the user to have one Robot selected and then give them a popup window displaying the other Robots and have them select one. Via showOptionsDialog, I have discovered how to make a custom JOptionPane, so I thought, why not make them buttons (probably will look awful, but I don't know how to make anything other than JOptionPane.showXxx at this point (like I said, only skin deep so far). Have tried consulting the javadocs, but right now that is a LOTTT to take in and have read a decent amount, I thought.
Ok, sorry if this is long, but is there a way, using my DefaultListMethod to make custom buttons? I tried a bunch of ways by creating Object[] options = {list.elements()}; etc but that doesn't work. Any help would be much appreciated!

How to make two documents update each other during real time depending on each other input?

Supposedly there are two abstract documents that correspond to JTextFields (Both JTextFields are different variables).
Suppose, user inputs numeric value in JtextField1(document A), it should update the other JTextField2(document B) during run time accordingly. The same goes if user inputs numeric value in JTextField2(Document B) at the same program run time, it should update JTextField1(Document A).
I tried solving this by using Document Listener, however, it will work only on updating one document(either A updates B or vice versa) during run time rather than both (or each other to be precise) as it provides a deadlock. Should I use DocumentFilter for this problem? I am clueless how I would apply it for this situation, though. I tried avoiding deadlock/write lock problem by using FocusListeners and disabling DocumentListener when the field is not in focus, but it was a very naive approach as it totally does not deal with threads(if that is the problem). Would creating a separate thread when the other field is selected suffice?I have no real knowledge in multi threading, though. I would appreciate a some sort of advice on this problem.
code: http://pastebin.com/qqYPXcAf
I m trying to recreate similar GUI functionality of photoshop that updates width/height size in pixels for keeping AR during run time on user input (picture: http://puu.sh/9L07v/c745e85867.png)
Should I use DocumentFilter for this problem?
An easier solution is to just share the Document:
JTextField textField1 = new JTextField(...);
JTextField textField2 = new JTextField(...);
textField2.setDocument( textField1.getDocument() );
Or if you code is doing some kind of calculation so that the two values are different then you can just remove the DocumentListener to avoid looping. So the basic code for the listener on textField2 would be:
textField1.getDocument().removeDocumentListener(...);
textField1.setText( ... );
textField1.addDocumentListener(...);
So know when you update the text in text field one it will not notify textField2.

Retrieve Values from multiple JFrame to a single JFrame

I have 5 JFrames in my application and I want the values from all 5 JFrames to be sent to a single JFrame. And it is a process where I have to go one frame to another and the value entered previously should not be lost and must be visible at the end of the process.
Easy example is,
I key in my name in the first frame,
then I key in my Address in the second frame,
then my mobile number in the third frame
and so on till the last frame where I want my keyed in details in the previous forms to be in the final frame to display my data in JTextfields. Is this possible? Because if it is a single form, I know how to do it. But when it is multiple forms in this situation I am lost. Please help.
This has nothing to do with Swing or JFrames and all to do with the general issue of getting information from one object into another. Yes it's possible -- give the classes that you wish to extract information from "getter" methods, and then call them when you want the information. If you want to gather this information in an event-dependent fashion, then you will need to have one class listening for state changes brought on by events in the other classes. A PropertyChangeListener can work well for this.
Or if you use modal JDialog windows instead of JFrames, you will always be notified when the dialog has returned and is no longer visible, since the calling code's program flow resumes from right after where it told the dialog to become visible.
Next we can discuss whether having 5 separate JFrames is a good idea or not. I'm guessing you know my opinion on this, else I wouldn't have mentioned the subject.

Categories