- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we add/insert a JButton to JTable cell in Java?
A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface. 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.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableButtonTest extends JFrame { private JTable table; private JScrollPane scrollPane; public JTableButtonTest() { setTitle("JTableButton Test"); TableCellRenderer tableRenderer; table = new JTable(new JTableButtonModel()); tableRenderer = table.getDefaultRenderer(JButton.class); table.setDefaultRenderer(JButton.class, new JTableButtonRenderer(tableRenderer)); scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new JTableButtonTest(); } } class JTableButtonRenderer implements TableCellRenderer { private TableCellRenderer defaultRenderer; public JTableButtonRenderer(TableCellRenderer renderer) { defaultRenderer = renderer; } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(value instanceof Component) return (Component)value; return defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } class JTableButtonModel extends AbstractTableModel { private Object[][] rows = {{"Button1", new JButton("Button1")},{"Button2", new JButton("Button2")},{"Button3", new JButton("Button3")}, {"Button4", new JButton("Button4")}}; private String[] columns = {"Count", "Buttons"}; public String getColumnName(int column) { return columns[column]; } public int getRowCount() { return rows.length; } public int getColumnCount() { return columns.length; } public Object getValueAt(int row, int column) { return rows[row][column]; } public boolean isCellEditable(int row, int column) { return false; } public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }
Output
- Related Articles
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- How can we disable the cell editing inside a JTable in Java?
- How can we set the margin to a JButton in Java?
- How can we filter a JTable in Java?
- How can we apply different borders to JButton in Java?
- How can we set the shortcut key to a JButton in Java?
- How to add Icon to JButton in Java?
- How to add empty border to a JButton in Java?
- How to add action listener to JButton in Java
- How can we implement the HTML text of JButton in Java?
- How can we change the JButton text dynamically in Java?\n
- How can we remove a selected row from a JTable in Java?
- How can we sort a JTable on a particular column in Java?
- Java Program to enable cell selection in a JTable

Advertisements