-1

As a beginner,i am creating a jtable with some functionalities like adding and removing contents. I would like to know how to make a rename functionality to my application that on selecting this menu should highlight all the contents of the cell as in an editing mode. Thanks in advance

Syamili V
  • 53
  • 1
  • 10
  • 2
    Well, you can only have one cell in edit mode at a time... – MadProgrammer Jul 29 '14 at 03:09
  • Think out loud, you might be able to "highlight" all the matches, put the first match into "edit mode" and when accepted, rename all other occurrences within the model...you would need some way to flag the model that it should do a "search and replace" when `setValueAt` is called though... – MadProgrammer Jul 29 '14 at 03:15
  • You might look at @camickr's article _Table Select All Editor_, cited [here](http://stackoverflow.com/a/10067560/230513). – trashgod Jul 29 '14 at 03:20

1 Answers1

1

Continuing from your previous post.... Did you want something like below, when when you hit edit in the context menu, you can edit in some popup window?

enter image description hereenter image description here

You pretty much already have to tools for this functionality (in your code). For the new Action, you simply need to show a JOptionPane input dialog with the value of the selected cell. The return input of the JOptionPane will be the value you set back to the table. Something like. Keep in mind though, depending on the type of data, you may want to do some logical parsing or conversion. Below I just take the value as a String.

class EditCellAction extends AbstractAction {
    private JTable table;

    public EditCellAction(JTable table) {
        putValue(NAME, "Edit");
        this.table = table;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int row = table.getSelectedRow();
        int col = table.getSelectedColumn();
        String newValue = JOptionPane.showInputDialog(table,
                "Enter a new value:", table.getValueAt(row, col));
        ((DefaultTableModel) table.getModel()).setValueAt(
                                               newValue, row, col);
    }
}

If you don't want the popup, and just want to programmatically start the cell editing, you can simple use table.editCellAt(row, col) to start the editing, and use the underlying text field of the cell editor to select the field contents. Something like below (tested and works)

@Override
public void actionPerformed(ActionEvent e) {
    int row = table.getSelectedRow();
    int col = table.getSelectedColumn();
    table.editCellAt(row, col);
    JTextField field = (JTextField) ((DefaultCellEditor) table
            .getCellEditor()).getComponent();
    field.requestFocus();
    field.setSelectionStart(0);
    int endSelection = 
              (!field.getText().isEmpty()) ? field.getText().length() -1 : 0;
    field.setSelectionEnd(endSelection);
}

Keep in mind though, if the cell is editable, the user can just double click the cell to edit it. I guess this adds some more functionality

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • @MadProgrammer that's what the second part of my answer does ;-) I wasn't too sure about the OP requirement so I posted both options – Paul Samsotha Jul 29 '14 at 04:44
  • Yep missed that part :P – MadProgrammer Jul 29 '14 at 04:45
  • 1
    @user3884321 the best place to look at the [Official Swing Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/). They provide most of the details you need for working with swing. It's a large tutorial so you need to search around for what you're looking for. – Paul Samsotha Jul 29 '14 at 05:05