我想获得一个表格宽度。我试着用这段代码来做:
private static class ListenerForOk implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
handle();
if (!dataIsNotInput()) {
createDataTab();
System.out.println(table.getWidth());
}
}
方法createDataTab()
完成添加JTable的所有工作。所以,在createDataTab();
我的桌子放在框架上后我就能看到它。但是此代码table.getWidth()
返回零。另一种尝试使用此代码获取表格宽度的方法:
double sum = 0.0;
for (int i = 0; i < table.getColumnCount(); i++) {
sum += table.getColumnModel().getColumn(i).getWidth();
}
System.out.println("sum: " + sum);
方法table.getColumnModel().getColumn(i).getWidth()
为所有列返回75。它是正确的值,我有一个正确的表宽度。但在我的班级表中,我重写了一个方法。这是我的代码:
class MyTable extends JTable {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row,
int column) {
Component component = super.prepareRenderer(renderer, row, column);
int rendererWidth = component.getPreferredSize().width;
TableColumn tableColumn = getColumnModel().getColumn(column);
tableColumn
.setPreferredWidth(Math.max(rendererWidth
+ getIntercellSpacing().width,
tableColumn.getPreferredWidth()));
return component;
}
因此,如果我在任何列中输入一个长字符串,则此列将相应地展开,其中包含文本。我做到了,专栏扩大了。但之后,方法table.getColumnModel().getColumn(i).getWidth();
再次为所有列返回75。那么,我怎样才能获得正确的表格宽度。
答案 0 :(得分:2)
调整列宽的基本代码是:
JTable table = new JTable( ... );
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
for (int column = 0; column < table.getColumnCount(); column++)
{
TableColumn tableColumn = table.getColumnModel().getColumn(column);
int preferredWidth = tableColumn.getMinWidth();
int maxWidth = tableColumn.getMaxWidth();
for (int row = 0; row < table.getRowCount(); row++)
{
TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(cellRenderer, row, column);
int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
preferredWidth = Math.max(preferredWidth, width);
// We've exceeded the maximum width, no need to check other rows
if (preferredWidth >= maxWidth)
{
preferredWidth = maxWidth;
break;
}
}
tableColumn.setPreferredWidth( preferredWidth );
}
您还可以查看Table Column Adjuster以了解包含上述代码的类,并在数据更改时添加自动列调整等额外功能。
答案 1 :(得分:0)
public int countTableWidth(JTable aTable) {
int tableWidth = 0;
Dimension cellSpacing = aTable.getIntercellSpacing();
int columnCount = aTable.getColumnCount();
int columnWidth[] = new int[columnCount];
for (int i = 0; i < columnCount; i++) {
columnWidth[i] = getMaxColumnWidth(aTable, i, true);
tableWidth += columnWidth[i];
}
// account for cell spacing too
tableWidth += ((columnCount - 1) * cellSpacing.width);
return tableWidth;
}
/*
* @param JTable aTable, the JTable to autoresize the columns on
* @param int columnNo, the column number, starting at zero, to calculate the maximum width on
* @param boolean includeColumnHeaderWidth, use the Column Header width as a minimum width
* @returns The table width, just in case the caller wants it...
*/
public static int getMaxColumnWidth(JTable aTable, int columnNo,
boolean includeColumnHeaderWidth) {
TableColumn column = aTable.getColumnModel().getColumn(columnNo);
Component comp = null;
int maxWidth = 0;
if (includeColumnHeaderWidth) {
TableCellRenderer headerRenderer = column.getHeaderRenderer();
if (headerRenderer != null) {
comp = headerRenderer.getTableCellRendererComponent(aTable, column.getHeaderValue(), false, false, 0, columnNo);
if (comp instanceof JTextComponent) {
JTextComponent jtextComp = (JTextComponent) comp;
String text = jtextComp.getText();
Font font = jtextComp.getFont();
FontMetrics fontMetrics = jtextComp.getFontMetrics(font);
maxWidth = SwingUtilities.computeStringWidth(fontMetrics, text);
} else {
maxWidth = comp.getPreferredSize().width;
}
} else {
try {
String headerText = (String) column.getHeaderValue();
JLabel defaultLabel = new JLabel(headerText);
//Igor
//ako je u table modelu kao ime kolone stvalje html code
//treba izracunati max duzinu text na sljedeci nacin
View view = (View) defaultLabel.getClientProperty("html");
if (view != null) {
Document d = view.getDocument();
if (d instanceof StyledDocument) {
StyledDocument doc = (StyledDocument) d;
int length = doc.getLength();
headerText = StringUtils.leftPad("", length + DEFAULT_COLUMN_PADDING);
}
}
//END Igor
Font font = defaultLabel.getFont();
FontMetrics fontMetrics = defaultLabel.getFontMetrics(font);
maxWidth = SwingUtilities.computeStringWidth(fontMetrics, headerText);
} catch (ClassCastException ce) {
// Can't work out the header column width..
maxWidth = 0;
}
}
}
TableCellRenderer tableCellRenderer;
// Component comp;
int cellWidth = 0;
for (int i = 0; i < aTable.getRowCount(); i++) {
tableCellRenderer = aTable.getCellRenderer(i, columnNo);
comp = tableCellRenderer.getTableCellRendererComponent(aTable, aTable.getValueAt(i, columnNo), false, false, i, columnNo);
//textarea na prvo mjesto jer je takodjer descendant od JTextComponent
if (comp instanceof JTextArea) {
JTextComponent jtextComp = (JTextComponent) comp;
String text = getMaximuWrapedString(jtextComp.getText());
Font font = jtextComp.getFont();
FontMetrics fontMetrics = jtextComp.getFontMetrics(font);
int textWidth = SwingUtilities.computeStringWidth(fontMetrics, text);
maxWidth = Math.max(maxWidth, textWidth);
} else if (comp instanceof JTextComponent) {
JTextComponent jtextComp = (JTextComponent) comp;
String text = jtextComp.getText();
Font font = jtextComp.getFont();
FontMetrics fontMetrics = jtextComp.getFontMetrics(font);
int textWidth = SwingUtilities.computeStringWidth(fontMetrics, text);
maxWidth = Math.max(maxWidth, textWidth);
} else {
cellWidth = comp.getPreferredSize().width;
// maxWidth = Math.max ( headerWidth, cellWidth );
maxWidth = Math.max(maxWidth, cellWidth);
}
}
return maxWidth;
}