import
java.awt.BorderLayout;
import
java.awt.Component;
import
java.awt.Dimension;
import
java.awt.event.WindowAdapter;
import
java.awt.event.WindowEvent;
import
javax.swing.JFrame;
import
javax.swing.JScrollPane;
import
javax.swing.JTable;
import
javax.swing.WindowConstants;
import
javax.swing.table.DefaultTableCellRenderer;
import
javax.swing.table.TableCellRenderer;
import
javax.swing.table.TableColumn;
public
class
FixTableColumnWidthFrame
extends
JFrame
{ Object[] columnNames_
=
{
"
First Name
"
,
"
MI
"
,
"
Last Name
"
}
;
Object[][] names_
=
{
{
"
Lynn
"
,
"
M.
"
,
"
Seckinger
"
}
,
{
"
Carol
"
,
"
R.
"
,
"
Seckinger
"
}
,
{
"
Roy
"
,
"
D.
"
,
"
Martin
"
}
,
{
"
Bill
"
,
"
O.
"
,
"
Veryveryveryverylonglastname
"
}
,
{
"
Richard
"
,
"
A.
"
,
"
Tattersall
"
}
,
{
"
Philip
"
,
"
B.
"
,
"
Edwards
"
}
,
{
"
Moore
"
,
"
T.
"
,
"
Moore
"
}
,
//
shorten scrollbar grip with these
{
"
Lynn
"
,
"
M.
"
,
"
Seckinger
"
}
,
{
"
Carol
"
,
"
R.
"
,
"
Seckinger
"
}
,
{
"
Roy
"
,
"
D.
"
,
"
Martin
"
}
,
{
"
Bill
"
,
"
O.
"
,
"
Veryveryveryverylonglastname
"
}
,
{
"
Richard
"
,
"
A.
"
,
"
Tattersall
"
}
,
{
"
Philip
"
,
"
B.
"
,
"
Edwards
"
}
,
{
"
Moore
"
,
"
T.
"
,
"
Moore
"
}
, }
; JTable table_
=
new
JTable(names_, columnNames_);
public
FixTableColumnWidthFrame()
{ TableUtilities.fixTableColumnWidth(table_); JScrollPane scroll
=
new
JScrollPane(table_);
//
scroll.setPreferredSize(table_.getPreferredSize());
getContentPane().add(scroll, BorderLayout.CENTER); pack(); }
public
static
void
main(String args[])
{ FixTableColumnWidthFrame frame
=
new
FixTableColumnWidthFrame(); frame.setTitle(
"
Setting Column Widths
"
); frame.pack(); frame.show(); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.addWindowListener(
new
WindowAdapter()
{
public
void
windowClosed(WindowEvent e)
{ System.exit(
0
); }
}
); }
}
class
TableUtilities
{
public
static
Dimension fixTableColumnWidth(JTable table)
{
int
MaxWidth
=
0
;
int
width
=
0
;
for
(
int
i
=
0
; i
<
table.getColumnCount(); i
++
)
{ TableColumn col
=
table.getColumn(table.getColumnName(i)); width
=
getPreferredWidthForColumn(table, col); col.setMinWidth(width); col.setMaxWidth(width); MaxWidth
+=
width; }
//
sizeColumnsToFit() must be called due to a JTable
//
bug
table.sizeColumnsToFit(
0
); Dimension tableSize
=
new
Dimension(MaxWidth, table.getPreferredSize().height); table.setPreferredSize(tableSize);
return
tableSize; }
public
static
int
getPreferredWidthForColumn(JTable table, TableColumn col)
{
int
hw
=
columnHeaderWidth(table, col),
//
hw = header width
cw
=
widestCellInColumn(table, col);
//
cw = column width
return
hw
>
cw
?
hw : cw; }
private
static
int
columnHeaderWidth(JTable table, TableColumn col)
{ TableCellRenderer renderer
=
col.getHeaderRenderer();
if
(renderer
==
null
)
{ renderer
=
new
DefaultTableCellRenderer(); ((DefaultTableCellRenderer)renderer).setText(col.getHeaderValue().toString()); }
Component comp
=
renderer.getTableCellRendererComponent( table, col.getHeaderValue(),
false
,
false
,
0
,
0
);
return
comp.getPreferredSize().width
+
2
; }
private
static
int
widestCellInColumn(JTable table, TableColumn col)
{
int
c
=
col.getModelIndex(), width
=
0
, maxw
=
0
;
for
(
int
r
=
0
; r
<
table.getRowCount();
++
r)
{ TableCellRenderer renderer
=
table.getCellRenderer(r,c);
Component comp
=
renderer.getTableCellRendererComponent( table, table.getValueAt(r,c),
false
,
false
, r, c);
width
=
comp.getPreferredSize().width
+
2
; maxw
=
width
>
maxw
?
width : maxw; }
return
maxw; }
}
|