How to save the value in JTextArea into JTable? - java

I have a JTable with four columns, in the fourth column I placed the JTextarea.. After entering the value in text area, I will reload the table , at that time value inside text area is not get saved and text area is empty.
How to solve my problem, suggestions please?

Try to get access through getter. Like
public JTextArea getTextArea()
{
return jTextAreaField;
}
and then
getTextArea().append("ur text");

Related

JTable Cell with window focus in JAVA Swing

I have created JTable with TableModel in which I make column Product Id as editable. This jtable is shown when i click one cashMemoBtn and if i type product id in first row and hit enter key then it is adding new empty row to jtable. But my problem is when new Empty row is added then Product Id Cell should have blicking cursor like we see in any editor OR in textfield.
I have attached my expected output screenshot here
In image you can see vertical bar which is indicating current focus but i want it should be automatic for all new rows when i add new rows.
I tried with
jTable.setEditingColumn(1);
jTable.setEditingRow(0);
But is not working as per my requirement. Please help.
Thanks in advance.
After you add the empty row of data to the table you then need to start editing on the desired cell.
The basic code would be:
if (table.editCellAt(row, 1))
{
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
}

Displaying just one value in JComboBox when one is selected

Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.
You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.
That's the code snippet where the ComboBox gets initialized.
optionsInteger = new JComboBox<String>();
optionsInteger.addItem("Ja");
optionsInteger.addItem("Nein");
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
optionsInteger.setSelectedIndex(1);
optionsInteger.setName("optionsInteger");
The ComboBox gets inserted to a JTable in this method:
public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
JComboBox<String> optionsInteger) {
model.setRowCount(xAmount);
th = table.getTableHeader();
tcm = th.getColumnModel();
tcs = tcm.getColumns();
tcs.nextElement().setHeaderValue("");
tcs.nextElement().setHeaderValue("Lower");
tcs.nextElement().setHeaderValue("Upper");
tc = tcs.nextElement();
tc.setHeaderValue("Integer");
tc.setCellEditor(new DefaultCellEditor(optionsInteger));
for(int i=0;i<xAmount;i++)
{
model.setValueAt("X"+(i+1), i, 0);
}
}
Thank you very much for your help.
In your code, this line
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
sets the default selection to the zeroth element (Ja). This line
optionsInteger.setSelectedIndex(1);
sets the default selection to the first element (Nein).
Either set the selected item or the selected index. There's no need to do both.
A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?
If you really want to do this, you'll have to create your own version of a JComboBox.
. If "Ja" is selected there should just be "Nein" as an option and the other way around.
So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.
Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.
It would also be cool if "Nein" is set per default.
This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.

Creating a dynamic search box/table in Java

What I'm trying to achieve is a JTextField with a JTable on the bottom, every time a change occurs on the JTextField (type or delete a character) the JTable would update showing the results from it's list of strings that match what is written on the JTextField, and showing all results if empty.
What I don't know how to do :
How to set the event on the JTextField that triggers everytime its text changes
Making the JTable update its values in an efficient way, without using too much memory
Add a DocumentListener to your JTextField. Update the TableModel belonging to your JTable with matches. The JTable will update itself in response.

Java: Show HAND cursor and change font of part of text in JTable row

I have a JTable with one column displaying certain text in every row
Like,
Example (www.example.com)
Test (www.Test.com)
Now, when mouse hovers on url i.e. text inside bracket cursor should change to HAND cursor and text inside bracket should be in different color and underlined to show that it looks like its an hyperlink
Tried lot of things but couldn't achieve.
Tried TableCellRenderer, but its changes the whole row content
Tried Map map =
new Hashtable();
map.put(TextAttribute.KERNING,
TextAttribute.KERNING_ON);
font = font.deriveFont(map);
but again it changes the content of whole row
Any solution would be great help
Object rows[][]={ };
Object headers[]={""};
TableModel model=new DefaultTableModel(rows,header);
String name= "Example";
String url="www.example.com";
String display = name + "(" + url +")" ;
(DefaultTableModel)model).addRow(new Object[]{display});
JTable table=Jtable(model);
Implementing a TableCellRenderer is the way to go. getTableCellRendererComponent can return any Swing component. So your question boils down to "how to show a string with multiple formats" and "how to change the cursor".
To show a string with multiple formats, you can use html in a single JLabel, use multiple JLabels in a JPanel or use a JEditorPane.
Setting a Handcursor boils down to a call to setCursor, which is available on all JComponents.
You can try
this.
And for adding multicoloured look for cell value in table use Label.
Have look over this
answer.

Editable cell and growable Jtable Implementation

I have a JTable with Customized CellRenderer and CellEditor, Intially the table is loaded with
a list of values Say with 12 rows and 5 colums, I have a JTextField at the top of the table in which I applied KeyListener and made the Textfield to display like a JComboBox with a list of values as soon as first 3 characters typed in that field, eg. Typing 'met' will display all the medicine names starting with "met", now what I want to do is I have to Implement that Textfield into the Jtable's last row's 2nd column Say 13th row in the situation I mentioned above. and after selecting any 1 medicine from the list of displayed value the JTable could Add a row dynamically and insert a new row with that search textfield, Please Suggest me an Idea and Code for this, also guide me how to apply cellrenderer and celleditor for a particular cell(Cell which contains the dynamic search textfield)...
Thanks a lot in Advance :)
Kindly let us assume jTable2 be your JTable variable name and TextField be your JTextField variable name. Then use the following code with the keylisterner of the text field to get what is wanted:-
javax.swing.table.DefaultTableModel dft= (javax.swing.table.DefaultTableModel)
jTable2.getModel();
jTable2.setModel(dft);
dft.addRow(new Object[1]);
jTable2.setValueAt(TextField.getText(),jTable2.getRowCount()-1,1);

Categories

Resources