Hi laura_morgar try it with this , i hope to help it!! luck
Código PHP:
import java.awt.*;
import javax.swing.*;
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++) {
// System.out.println ("--- col " + 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);
//System.out.println ("col " + col +
// " pref width now " +
// maxwidth);
} // for row
/* this version of the width set doesn't consider the
column header's preferred width
*/
TableColumn column = columnModel.getColumn (col);
column.setPreferredWidth (maxwidth);
/* this version of the width set considers the
column header's preferred width too
TableColumn column = columnModel.getColumn (col);
TableCellRenderer headerRenderer = column.getHeaderRenderer();
if (headerRenderer == null)
headerRenderer = table.getTableHeader().getDefaultRenderer();
Object headerValue = column.getHeaderValue();
Component headerComp =
headerRenderer.getTableCellRendererComponent (table,
headerValue,
false,
false,
0,
col);
maxwidth = Math.max (maxwidth,
headerComp.getPreferredSize().width);
column.setPreferredWidth (maxwidth);
*/
} // for col
}
}
Código PHP:
import javax.swing.*;
import javax.swing.table.*;
public class TestColumnResizer {
final static Object[][] TABLE_DATA = {
{new Integer(1), "ONJava", "http://www.onjava.com/"},
{new Integer(2), "Joshy's Site", "http://www.joshy.org/"},
{new Integer(3), "Anime Weekend Atlanta", "http://www.awa-con.com/"},
{new Integer(4), "QTJ book",
"http://www.oreilly.com/catalog/quicktimejvaadn/"},
{new Integer(58989),"catalogswing","erererererere"}
};
final static String[] COLUMN_NAMES = {
"Count", "Name", "URL"
};
public static void main (String[] args) {
// 142 mac l&f has a header bug - force metal for today
try {
UIManager.setLookAndFeel (
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { e.printStackTrace();}
DefaultTableModel mod =
new DefaultTableModel (TABLE_DATA, COLUMN_NAMES);
JTable table = new JTable (mod);
JScrollPane pane =
new JScrollPane (table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame ("JTable Column Widths");
frame.getContentPane().add (pane);
frame.pack();
frame.setVisible (true);
try {
Thread.sleep (5000);
} catch (Exception e) { e.printStackTrace(); }
// now get smart about col widths
final JTable fTable = table;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ColumnResizer.adjustColumnPreferredWidths (fTable);
fTable.revalidate();
}
});
}
}
You can replace the following lines:
TableColumn column = columnModel.getColumn (col);
column.setPreferredWidth (maxwidth);
with these:
TableColumn column = columnModel.getColumn (col);
TableCellRenderer headerRenderer = column.getHeaderRenderer();
if (headerRenderer == null)
headerRenderer = table.getTableHeader().getDefaultRenderer();
Object headerValue = column.getHeaderValue();
Component headerComp =
headerRenderer.getTableCellRendererComponent (table,
headerValue,
false,
false,
0,
col);
maxwidth = Math.max (maxwidth,
headerComp.getPreferredSize().width);
column.setPreferredWidth (maxwidth);