- 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 to set magins between cells of a JTable in Java?
To set the margins i.e. row and columns margins between cells of a table, use the setIntercellSpacing() method −
Dimension dim = new Dimension(50,2); table.setIntercellSpacing(new Dimension(dim));
Above, we have used the Dimension class −
The following is an example to set margins between cells of a JTable cell −
Example
package my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { JFrame frame = new JFrame("Demo"); JPanel panel = new JPanel(); String data[][] = { {"Australia","5","1"}, {"US","10","2"}, {"Canada","9","3"}, {"India","7","4"}, {"Poland","2","5"}, {"SriLanka","5","6"} }; String col [] = {"Team","Selected Players","Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data,col); JTable table = new JTable(tableModel); Dimension dim = new Dimension(50,2); table.setIntercellSpacing(new Dimension(dim)); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); frame.add(panel); frame.setSize(600,400); frame.setUndecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG); frame.setVisible(true); } }
Output
- Related Articles
- How to select different cells of a JTable programmatically in Java?
- Java Program to deselect all cells in a JTable
- How to set a value in a particular JTable cell with Java?
- How to set the color to alternate rows of JTable in Java?
- How to set multidimensional array into JTable with Java?
- Java Program to set different height for multiple rows in JTable
- How can we set the background/foreground color for individual column of a JTable in Java?
- How to change each column width of a JTable in Java?
- How to implement the search functionality of a JTable in Java?
- How to add a title to JTable in Java Swing?
- Java Program to set the height of only a single row in a JTable with multiple rows
- How to enable row selection in a JTable with Java
- How to disable auto resizing for a JTable in Java?
- How can we filter a JTable in Java?
- How to make JTable single selectable in Java?

Advertisements