Unable to add two buttons in a single cell in a JTable

后端 未结 3 2070
南旧
南旧 2021-01-14 00:52

I am trying to make a dynamic table that fetches data from a database and adds those data in separate rows..I want to add an extra cell with each row(dynamically) th

3条回答
  •  梦毁少年i
    2021-01-14 00:56

    Use a CellRenderer to render the two buttons on the column.

    The overriden method could look something like this :

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus, int row, int column)
    {
        component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
    
         JPanel buttonPanel = new JPanel();
    
        //create buttons and add to JPanel. 
    
        //add an action listener to each of the buttons
    
        //Return this JPanel at the end of the method.
    
        return buttonPanel;
    

    }

    In the actionPerformed() method for the delete button, find the row that you want to delete. You have the table object. From the table object you can then get the model object. In your model object you should have a method like, getRowForRowNumber(..), where you can pass in the row paramters.

    now once you have a reference to your row after clicking the button you just need to go to the backend, update your delete action, remove it from the model, and perform a fireTableDataChanged().

提交回复
热议问题