-2

My requirement is, I have to add a table cell renderer in a cell with data which is fetching from DB and button.

When I double click in that cell on data, the cell should be editable so that user can change the data. When I double click on button one more dialog opens which accepts data from the user as soon as I click on OK button in that dialog,the data should populate in the previous table cell. Currently, I am able to render the data and image and when I click on the button, it's opening another dialog and accepting data from the user.

My issues:

  1. am not able to and edit the cell
  2. How to capture the edited value
  3. how to display the accepted value in other dialog in this cell.

Currently the below image is not showing text but for other cells, I am able to show.

JTable cell with text and button

jTableTestCases.setModel(new javax.swing.table.DefaultTableModel( new Object [][] {

        },
        new String [] {
            "Select", "Status", "ID", "Title", "Config", "Tester", "TestCase Name"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            true, false, false, false, false, false, true
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }

    }); static class MyTableEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

    JPanel jpanel;
    JLabel label;
    JButton button;



    MyTableEditor() {
        label = new JLabel();
        button = new JButton("Click Me!!!");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null, "Hi" );
            }

        });

        GridLayout gridLayout = new GridLayout(0,2);
        jpanel = new JPanel();
        jpanel.setLayout(gridLayout);
        jpanel.setBorder(new EmptyBorder(0, 10, 0, 10));
        jpanel.add(label);
        jpanel.add(button);
    }

    @Override
    public Object getCellEditorValue() {
        return null;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
       if(null!=value)
        label.setText(value.toString());

        jpanel.setBackground(table.getSelectionBackground());

        return jpanel;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         if(null!=value)
        label.setText(value.toString());
        if(isSelected){
          jpanel.setBackground(table.getSelectionBackground());
        }else{
          jpanel.setBackground(table.getBackground());
        }
        return jpanel;
    }

}jTableTestCases.getColumn("TestCase Name").setCellRenderer(new MyTableEditor());
             jTableTestCases.getColumn("TestCase Name").setCellEditor(new MyTableEditor());
suma2020
  • 17
  • 5
  • 2
    1) You've described a requirement or specification, but asked no question. What is your question? 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 15 '16 at 12:08
  • 1
    can you post your code please – Youcef LAIDANI May 15 '16 at 12:09
  • @Andrew Thompson, 1)I am not able to edit the table cell ,which contains text and button when I click on the teext ,the cell should be editable and the edited value should be stored. 2)when I double click on the button,one more dialog opens up,I need to capture the data which user entered and should populate in the table cell currently ,I am not able to do this 2functionalities with the editor – suma2020 May 15 '16 at 12:14
  • 1
    If you want to notify Andrew Thompson of a comment, place a `@` before his name: @AndrewThompson. I second both of the folks requests above, if you want decent help, improve your question by posting your [mcve] code. – Hovercraft Full Of Eels May 15 '16 at 12:17
  • 1
    Please edit your question, show your code in the question itself and not in comments. Also you're posting small snippets of code which won't help us. Please read the [mcve] and [sscce](http://sscce.org) links to see what you should be creating and posting. – Hovercraft Full Of Eels May 15 '16 at 12:25
  • @Hovercraft:Apologies.I was not known about it,I have added my complete code.Can you help me.I am not able to edit the cell after adding the editor but am able to when I just add the renderer but I don't know how too capture this value.I have to save the edited value to dataabase – suma2020 May 15 '16 at 15:06
  • If you are trying to use a button as an editor, then you can check out [Table Button Column](https://tips4java.wordpress.com/2009/07/12/table-button-column/) for a complete renderer/editor implementation. If you are attempting to have text and button the same columns then I suggest you use two columns. A JTable is not designed to hold multiple components in the same cell. – camickr May 15 '16 at 15:20
  • @Camickr:Thanks for your reply.If both the functionalities text editing and clicking button will not happen in the same cell ,surely I will perform my functionality with the button.I am opening a one more dialog when the button clicked ,user enters a value in the text box ,as soon as click on ok button ,the entered value should display in the table cell beside to the button and I should able to read the value..is it possible.Thanks in advance.. – suma2020 May 15 '16 at 15:48

1 Answers1

1

,user enters a value in the text box ,as soon as click on ok button ,the entered value should display in the table cell beside to the button and I should able to read the value

Why are you creating a popup of this?

A JTable is designed so that you can enter text directly into the cell. The is you can start typing and the text will be updated. Or you can double click on the cell and the editor will be displayed.

But if you really want to do display a popup then you can check out the Table Popup Editor.

Whenever you click on the cell, use the F2 key or type a character a JDialog will be displayed and you can enter the text.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks Camickr.Its very simple and easily understandable. Actually user selects a value which is automatically populated from the local system for that purpose ,I am showing a dialog with list box and whatever user selects ,the value will be set to text box.I am still looking is there any possibility to render button also. – suma2020 May 15 '16 at 17:56
  • @suma2020, Instead of a JList you should just use a JComboBox for the editor. See the section from the Swing tutorial on [Using a Combo Box as an Editor](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox). – camickr May 15 '16 at 18:53
  • ,How to control getCellEditorValue(),In your example ,its calling when I click on "OK" button but in my example,its calling immediately when the dialog loaded with few values – suma2020 May 16 '16 at 10:49
  • will it be called after calling fireEditingStopped()? – suma2020 May 16 '16 at 11:01
  • @suma2020, `How to control getCellEditorValue(),` - I already showed you how to do this with working code. Start with the working code and modify it to meet your requirements. – camickr May 16 '16 at 14:52