Link array of buttons and textfields in Java - java

I have a list of textfields, and a list of buttons. These are laid out on a form with the buttons next to the textfields. Let's say there are 5 of each, they are 1 through 5. In reality, the number will be created at runtime.
When the user clicks a button, a new form is opened, which guides the user through creating a string. When they finish on that form, a text string is written in to the text field next to the button.
I'd like to create a relationship between each button and text field so that I know which text field to write in based on which button was clicked.
Is there a Java standard here? If not, any suggestions appreciated.
Thanks

Well either wrap or extend JButton to add a JTextfield field. Upon creation of the Button, pass the associated textfield to it and then you can pass along that info.

I'd like to create a relationship between each button and text field
Create an ActionListener class that takes the text field as a parameter. Something like:
JTextField textField = new JTextField();
JButton button = new JButton(...);
button.addActionListener( new FormPopupListener( textField ) );
Then you can save the text field as a variable in your listener class and when the dialog closes you can update the text field.

You have two lists, why not use the index in the list? Button at index 0 relates to textfield at index 0 and so on. Another alternative would be wrap them in an object that contains the button and the textfield and run a single list.
I would use the second route but there is nothing wrong with just using the index of the list.

Basically you can subclass JButton with additional attribute index. You can populate the index when creating the buttons and it will refer to index of text field in array of text fields. If you want you can even have a name given to each text field and add them to a map and then give the name to button to lookup the required text field.

Related

Is it possible to add the same ActionListener for 100 buttons?

I have created an array of buttons (JButton[] jb = new JButton[100]) and I want to add the same action listener for all the buttons in the jb array instead of adding them one by one.
Just imagine every time I click one of the buttons displayed on the screen and it will print out the index of that button in the jb array.
The trivial answer is yes, you can add the same listener to many different buttons, and via the parameter the listener will be able to figure out which Button instance clicked it.
The challenge is that you won't be able to know which index in the array matches the button, so you'll need to either:
Figure out which button it was based on something in the button, like its getLabel value
Scan the array each click to figure out the index for a button instance, which is slow (O(n)), but for a hundred buttons it might not matter that much
Store a mapping from button instance to array index in a different faster data structure like a HashMap (O(1)), which might get ugly if buttons aren't well defined to be hashable
Use the same ActionListener class for all the buttons, but store a field on each instance of the listener that tells it what index it maps to

How do I create a textbox JavaFX

I am writing a tableview based program which stores its values in MySQL.
I need to create textBoxes with x button like in Gmail in textfields class(considering that there would be several boxes in one field) with auto completion on JavaFX. The Boxes supposed to be like this:
Example
The box also will have hidden values, supposed to be stored in MySQL and do some action when clicked on them.
Apologize if question is simple I am kind of beginner in Java
You need to add the button in the TextBox:
CustomTextField textfield = new CustomTextField
textfield.setRight(new Button());
And on button you can add every style what you want

Is there a way in Java to set a JTextField's position to be at the start of the text?

private JTextField textField = new JTextField();
textField.setText("10000000000");
If the JTextField is small, which mine are, it'll show the last so many characters. In this case, that would be something like "00000" if only five characters can be shown given the area of the text field. I want to set the position automatically to the start of the JTextField once I press a separate JButton. So, in this case, I would want "10000" to show.
I have a list of multiple JTextFields and a JButton that does calculations on them and finds things like the total, then prints that total in another JTextField, and you only see the last part of the total which can get confusing if you don't realize it's showing the end of the text instead of showing it from the beginning.
I looked through all the ".set***" methods and didn't see any option to do this.
Take a look at JTextField#setCaretPosition
textField.setCaretPosition(0);
You could also highlight the text using...
textField.setCaretPosition(textField.getText().length());
textField.moveCaretPosition(0);
But JTextField#selectAll would be simpler...
Depending on your needs, you can also "suggest" the preferred number of columns that the field should display, for example...
private JTextField textField = new JTextField(15);
or
textField.setColumns(15);
This will encourage some layout managers to ensure that the field is sized to a more appropriate size based on the fields needs

Adding text to jTextarea in jTabbedPane

I have a desktop program with 3 tabs i'm building in netbeans. There is 1 text input field below the three tabs. each tab has a jtextarea component. When i type in the text input field and hit enter i want it to add that text to the textarea contained by the currently selected tab.
I've been playing around with
jTabbedPane1.getSelectedIndex()
jTabbedPane1.getTabComponentAt()
How can i use the index to reference it back to the textarea associated with that tab?
Big java noob here.
The simplest way would be to use an array of JTextArea components corresponding to the order that they appear on the JTabbedPane.
JTextArea[] textArea = new JTextArea[3];
// assign textAreas...
You could then set the text using the appropriate index:
int index = jTabbedPane1.getSelectedIndex();
textArea[index].setText(myField.getText());

Creating a number of JTextField s that is defined by user input in way to to read text from all of them in a push of a button

I used to make my UI by making declaring new JTextField in a for cycle while attaching an action listener by anon class to each JTextField, which means that you have to press enter fire an event which will read the text of the field and put it into an array here is the code
Getting data from JTextField that is one of several local variables without a few minor changes . Now I have to modify it to so that i press a button like Apply in order to have values written into an array. While I found two ways to do this wonder what is the optimal way to do this .
The horrible way . Create an array that acts as temporary storage ,
replace ActionListeners with DocumentListeners that will place
the values into this temp array . And a button that will on press
iterate through the the temp array placing its values into the
target array.
A better way which I found while searching , create a JTextField
array as public and simply have a button that will on press iterate
through the JTextField array and place its values into the target
array.
Adder is an example that maintains a List<JFormattedTextField> to enforce formatting. It uses a PropertyChangeListener & FocusListener to update on navigation events, such as the default key binding to Tab and Shift-Tab.

Categories

Resources