0

I have a JTable with 7 column. I want to add at the seven one column a JButton with Icon.

So I have this:

mappa= modelManager.getContoBancarioManager().getContiBancari(null,WFConst.CONTO_BANCARIO_PUBBLICO);
//fontTable = new Font("Century Gothic", Font.PLAIN, 15);
tableModelContiBancari = new MyTableModelContiBancari();
tableContiBancari= new JTable(tableModelContiBancari);

tableModelContiBancari.stampaTabella(mappa);
tableContiBancari.addMouseListener(new MyMouseAdapterTableConti());
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(tableContiBancari);
jScrollPane.setPreferredSize(dTabella);


Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
Double larghezza =screenSize.getWidth()*0.95;
//    System.out.println(larghezza);
int lar = (int) (larghezza /90);
int lar2 = (int)(larghezza /5);
tableContiBancari.getColumnModel().getColumn(0).setPreferredWidth(10);
tableContiBancari.getColumnModel().getColumn(1).setPreferredWidth(lar2);
tableContiBancari.getColumnModel().getColumn(2).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(3).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(4).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(5).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(6).setPreferredWidth(lar);

Action delete = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
    //to do
    System.out.println("pp");
    }
};

ButtonColumn buttonColumn = new ButtonColumn(tableContiBancari, delete, 7);
buttonColumn.setMnemonic(KeyEvent.VK_D);

This is ButtonColumn class:

public void daiProprietaJTableContiBancari(){
    mappa= modelManager.getContoBancarioManager().getContiBancari(null,WFConst.CONTO_BANCARIO_PUBBLICO);
    //fontTable = new Font("Century Gothic", Font.PLAIN, 15);
    tableModelContiBancari = new MyTableModelContiBancari();
    tableContiBancari= new JTable(tableModelContiBancari);
    tableContiBancari.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_F5)
                cambiaTipologiaConti();
        }
    });
    tableModelContiBancari.stampaTabella(mappa);
    //tableContiBancari.setFont(fontTable);
    tableContiBancari.addMouseListener(new MyMouseAdapterTableConti());
    jScrollPane = new JScrollPane();
    jScrollPane.setViewportView(tableContiBancari);
    jScrollPane.setPreferredSize(dTabella);

    tableContiBancari.setRowHeight(25);

    Toolkit t = Toolkit.getDefaultToolkit();
    Dimension screenSize = t.getScreenSize();
    Double larghezza =screenSize.getWidth()*0.95;
    //    System.out.println(larghezza);
    int lar = (int) (larghezza /90);
    int lar2 = (int)(larghezza /5);
    tableContiBancari.getColumnModel().getColumn(0).setPreferredWidth(10);
    tableContiBancari.getColumnModel().getColumn(1).setPreferredWidth(lar2);
    tableContiBancari.getColumnModel().getColumn(2).setPreferredWidth(lar);
    tableContiBancari.getColumnModel().getColumn(3).setPreferredWidth(lar);
    tableContiBancari.getColumnModel().getColumn(4).setPreferredWidth(lar);
    tableContiBancari.getColumnModel().getColumn(5).setPreferredWidth(lar);
    tableContiBancari.getColumnModel().getColumn(6).setPreferredWidth(lar);

    DefaultTableCellRenderer renderer_archivi = new DefaultTableCellRenderer();
    renderer_archivi.setHorizontalAlignment(SwingConstants.RIGHT);
    tableContiBancari.getColumnModel().getColumn(4).setCellRenderer(renderer_archivi);
    tableContiBancari.getColumnModel().getColumn(5).setCellRenderer(renderer_archivi);
    tableContiBancari.getColumnModel().getColumn(6).setCellRenderer(renderer_archivi);

    Action delete = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
        //to do
        System.out.println("pp");
        }
    };

    ButtonColumn buttonColumn = new ButtonColumn(tableContiBancari, delete, 7);
    buttonColumn.setMnemonic(KeyEvent.VK_D);
    //tableContiBancari.getColumnModel().getColumn(7).setCellRenderer(new ButtonRenderer());
    //tableContiBancari.getColumnModel().getColumn(7).setCellEditor(new ButtonEditor(new JCheckBox()));
    //setUpColumnButton(tableContiBancari, tableContiBancari.getColumnModel().getColumn(7));
}

If I try to run the code, I have a JTable with a JButton at the last column but if I try to click on one JButton the action is not execute.

bircastri
  • 2,169
  • 13
  • 50
  • 119

2 Answers2

0

You need to implement your own TableCellRenderer and make its method getTableCellRendererComponent return a JButton for your column. See the Tutorial.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

Check out Table Button Column for one approach.

You add text to the column the same way you do for any other column and then the ButtonColumn class is used as a:

  1. renderer - so that the text is displayed on a button
  2. editor - so you can click on the button to invoke an Action.

You must also provide an Action to the ButtonColumn class. The Action will have access to the row the clicked button. You can easily use the row number to delete the row for example, or use the row to get data from the table and do other processing.

camickr
  • 321,443
  • 19
  • 166
  • 288