13 May 2011

Resize JTable Columns


Resize JTable Columns


Now Let's doing another nice and useful trick  with Java Swing,

Here we will create one JTable. And when we will run it, it will adjust its column size as per its contents..!

Want to know how can it be done....then stick with me friends.

Output will be:


1. Normal Default JTable   

                                                                    

2. Resized JTable.



Copy the Below code And paste in your Java Editor and simply run it...


1. Create class ColumnResizer .java  this contains the logic of resize the columns of   
    any JTable.
---------------------------------------------------------------------------------------------------------------------


import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.*;

public class ColumnResizer {
public static void adjustColumnPreferredWidths(JTable table) {
// strategy - get max width for cells in column and
// make that the preferred width
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {

int maxwidth = 0;
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer rend = table.getCellRenderer(row, col);
Object value = table.getValueAt(row, col);
Component comp = rend.getTableCellRendererComponent(table,
value, false, false, row, col);
maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);

TableColumn column = columnModel.getColumn(col);
column.setPreferredWidth(maxwidth);
}
}

------------------------------------------------------------------------------------------------------------------------------

2. Create Second class TestColumnResizer.java  this contains the JTable which we 
    want to resize.



import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestColumnResizer {
final static Object[][] TABLE_DATA = {
{ new Integer(1), "Nirav Raval", "http://www.niravjavadeveloper.blogspot.com/" },
{ new Integer(2), "Java Web", "http://www.java2s.com/" },
{ new Integer(3), "Java 2d Effects","http://www.daniweb.com/" },
{ new Integer(4), "Java Swing", "http://www.onjava.com/"},
{ new Integer(5), "QTJ book", "http://www.oreilly.com/catalog/" } } ;

final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

public static void main(String[] args) {

DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
JTable table = new JTable(mod);

JScrollPane pane = new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JFrame frame = new JFrame("JTable Column Widths");
frame.setSize(500, 400);
frame.getContentPane().add(pane);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Waiting For 3 Seconds and then adjust the width of Columns....
// You can delete this try...catch block if you wish to.
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}

// setting of columns widths done here
final JTable fTable = table;
SwingUtilities.invokeLater(new Runnable() {

public void run() {
ColumnResizer.adjustColumnPreferredWidths(fTable);
fTable.revalidate();
}
});
}
}

-----------------------------------------------------------------------------------------------------------

5 comments:

  1. Thanks, this helped me a lot !

    ReplyDelete
  2. Thanks Anonymous, I am always here to help people in Java Swing.

    ReplyDelete
  3. Are you releasing this with an open source license?

    ReplyDelete
  4. The beauty of this nice class is the compactness. It's working like a charm and simple to understand...

    ReplyDelete
  5. Thanks, that worked well for me..

    ReplyDelete

Leave Comment If You Like This...Thank You..!