Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Buttons inside cell of JTable along with data?

Is it possible to add buttons inside the JTable cell along with data? What I am trying to do is to create a table with columns which display data(number) from the database, and two buttons to increase/decrease the number inside the same cell.

|ID | Quantity|
|06| 2 [+][-] |

it would be something like above with [+][-] being buttons. So when I press [+], the number will change to 3 and 1 if pressing [-].

like image 774
Todd Avatar asked Jan 15 '10 05:01

Todd


People also ask

How can you change the appearance of data in cells in JTable?

We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.

How do you get a value from a selected cell in a JTable?

To get the value from a particular grid cell, you can use the wValue property of the JTable object. The property has the Row and Column parameters which specify the row and the column that contain the cell.

How can we add insert a JButton to JTable cell in Java?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.

How add data from Jtextfield to JTable?

To add the data entered in the JTextFields you will need to register an ActionListener to your add button, in this case jButton1 . To add entries to your table model you could use a mutable model such as DefaultTableModel : DefaultTableModel model = new DefaultTableModel(data, columns);


2 Answers

Yes, it is possible, although It won't be easy.

You have to write your own custom cell renderer and your own cell editor.

This is a sample I made in 5 minutes:

sample

It is far from perfect, but shows the concept.

Here's the source code:

import java.awt.Component;
import java.awt.Font;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Dimension;

public class CustomCell {
    public static void main( String [] args ) { 
        Object [] columnNames = new Object[]{ "Id", "Quantity" };
        Object [][] data        = new Object[][]{ {"06", 1}, {"08", 2} };

        JTable table = new JTable( data, columnNames ) { 
            public TableCellRenderer getCellRenderer( int row, int column ) {
                return new PlusMinusCellRenderer();
            }
         };

        table.setRowHeight( 32 );
        showFrame( table );
    }

    private static void showFrame( JTable table ) {
        JFrame f = new JFrame("Custom Cell Renderer sample" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.add( new JScrollPane( table ) );
        f.pack();
        f.setVisible( true );
    }
}

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
        public Component getTableCellRendererComponent(
                            final JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
                this.add( new JTextField( value.toString()  ) );
                this.add( new JButton("+"));
                this.add( new JButton("-"));
                return this;
        }
}

Here's a thread that may be interesting and here.

like image 52
OscarRyz Avatar answered Nov 15 '22 20:11

OscarRyz


As discussed in the tutorial you'll need both a renderer to display your value and an editor to detect events from the cell being edited. In this example, the Component is a JCheckBox. Note that this requires a custom DataModel that extends AbstractTableModel to supply the correct Class for a given column. Joonas' suggestion to use JSpinner is a good one that works well.

like image 34
trashgod Avatar answered Nov 15 '22 20:11

trashgod