Adding text to jTextarea in jTabbedPane - java

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());

Related

How to display content in a JTextArea or ScrollPane

I input into a text field "C". What should be displayed should be the next 10 letters that come after C. What I could do is convert all these letters into strings and then set the strings text to a JLabels text, but instead I want to be able to show them on a textArea with a scroll pane so that it doesn't run off the page and instead stays within a window I can scroll down from.
Is there any way to accomplish this? I have a TextArea fitted with a scroll pane setup, but I don't know how to output any data here.

JTextField more as one row and start the text at the top of the text field

how can I implement a TextField which has more than one and starts at the top of the TextField?
If I just implement a JTextField the coursor or the text of the field is always in the middle and does not start at the top
JTextField is a lightweight component that allows the editing of a single line of text.
(API docs)
You seem to want a UI component that supports multiple lines of text; that would be JTextArea, not JTextField.

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

Label text does not wrap up in a FieldEditorPreferencePage window

I'm using a FieldEditorPreferencePage for my Preference Window. I'm also using FieldEditor Abstract base class for all my field editors in the Preference window.
I have 3 radio buttons and as the user clicks on the radio button a text will be displayed at the bottom of the window educating the user on the selected options, My problem is that, the text of the label is very long and does not fit into the Preference window. I want the label text to wrap up automatically as the size of the window grows or shrinks, I tried every possible thing to wrap up the text but instead the text gets lost, for example I have tried :
GridData g = new GridData(GridData.FILL_BOTH);
g.widthHint = 200;
Label l = new Label(getFieldEditorParent(), SWT.BEGINNING | SWT.WRAP);
l.setLayoutData(g);
l.setText("Long Msg");
getFieldEditorParent() returns a parent composite for a field editor.
Apart from the above code, I have also tried all the other possible combinations of parameters for GridData and the Label, but the result in the UI part remains the same(text will get lost if the size of the text exceeds the size of the window). I can set the size of the window to fit the text, but that is hard-coding.
How can I possibly wrap up the label text as the window's size grows and shrinks?
Is this an issue with FieldEditorPreferencePage? Is there a conflict using a label in a preference page? Someone please explain in brief as to why this happens?

Link array of buttons and textfields in 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.

Categories

Resources