I have a JTable, edit button and save button. when I click the edit button , I want to insert two JTextFields into a particular cell which is selected . So I can write (strings) into these text fields . when I click on save button want to remove those two textfields from the cell and paste that strings (into the same cell of the table).
Asked
Active
Viewed 590 times
-2
-
What is wrong with the default functionality of the JTable. It already supports this requirement. You can tab to the cell and start typing and the data in the cell will be updated. Or you can double click on the cell to start editing. All you do is override the `iseCellEditable(...)` method of the JTable. Read the tutorial. You were given the link in your last question. Your current requirement makes no sense. Why would you force the user the extra step of clicking on the Edit button? What happens if you put them in "Edit" mode and they want to "Cancel"? – camickr Nov 15 '19 at 04:49
-
What happens if the users tabs to another component without clicking "Save". The requirement is not very clear. Maybe you should display a modal JDialog when the user clicks on "Edit". Then when the dialog is close you can update the TableModel. – camickr Nov 15 '19 at 04:51
-
I am thinking in same way, but the requirement is like this only. table save the comma separated person name and city name (in one column). And every row have edit and delete button(with icon). when user click on edit he should me able to edit the person name and city name in two textfields (in the same cell). and the save button will save them again in a comma separated strings. – devilmk Nov 15 '19 at 05:10
-
Maybe the [List Editor](https://tips4java.wordpress.com/2008/10/19/list-editor/) will give you some ideas. It adds a single JTextField to a row of a list when you want to edit the item. In your case you would add a panel containing two JTextFields to the cell of the JTable. – camickr Nov 15 '19 at 15:41
-
Thanks , I tried this solution but Jlist is not editable I was able to add the buttons and string , But button was not clickable . Now for every row i am using panel and add these into big panel. – devilmk Nov 16 '19 at 19:35
-
*but Jlist is not editable* - The suggestion was not to use a JList. The suggestion was to show you how to display a popup with an editable text field so you could apply the concept to your JTable. Instead your popup would contain two JTextFields. *Now for every row i am using panel* - not a great solution for when you have many rows of data. This will waste resources. – camickr Nov 16 '19 at 20:22
1 Answers
1
You don't need to add a JTextField to a JTable in order for a cell to be editable. The isCellEditable(int row, int column) function can be overridden to return a boolean dependent on the edit button. Here's an example:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.EventQueue;
import java.awt.event.*;
import java.awt.Dimension;
public class EditTableExample extends JFrame {
private boolean editable = false;
public EditTableExample() {
//set up jframe
setPreferredSize(new Dimension(500, 500));
setMinimumSize(new Dimension(500, 500));
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//set up content pane
JPanel contentPane = new JPanel();
setContentPane(contentPane);
//table model
Object[][] tableContents = new Object[][]{ //contents of our table
{"Person1", "City1"},
{"Person2", "City2"},
{"Person3", "City3"}
};
Object[] tableHeader = new Object[]{
"Name", "City"
};
DefaultTableModel model = new DefaultTableModel(tableContents, tableHeader) {
@Override
public boolean isCellEditable(int row, int column) {
return editable;
}
};
//table
JTable table = new JTable(model);
//scrollpane to house table
JScrollPane tablePane = new JScrollPane(table);
tablePane.setPreferredSize(new Dimension(450, 450));
//button that will add a row
JButton add = new JButton("Add Row");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
model.addRow(new Object[model.getColumnCount()]); //adds a new, empty row to the table
}
});
//button that will toggle edit mode
JButton edit = new JButton("Toggle Edit");
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
editable = !editable; //switches the value of 'editable' on click
}
});
//button to remove a row
JButton remove = new JButton("Remove Row");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
try {
model.removeRow(table.getSelectedRow()); //remove selected row
}
catch (ArrayIndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(rootPane, "No Row Selected");
}
}
});
//add everything together
contentPane.add(tablePane);
contentPane.add(add);
contentPane.add(edit);
contentPane.add(remove);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
EditTableExample e = new EditTableExample();
e.setVisible(true);
}
});
}
}
As you can see, the isCellEditable function will return the value of 'editable', a boolean whose value is toggled by the 'edit' button. Instead of having one cell per person that contains "Name, City" there are two columns, one for the person's name and one for their city. Let me know if you have any other questions.

danelliott
- 150
- 1
- 7
-
Thanks for the code. I have tried this way , like the way you explained. And I am to do this part. But in my use case I am using panels for rows inside panel. – devilmk Nov 16 '19 at 19:38