问题描述
我程序的输出在JScrollPane
内的JTextArea
中.看起来很讨厌:
The output from my program is in a JTextArea
inside a JScrollPane
. It looks nasty:
Created BinaryTree.java in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\BinaryTree.java
Created DisplayStuff.java in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\DisplayStuff.java
Created LinkedList.java in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\LinkedList.java
Created Node.java in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\Node.java
Created notes in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\notes
Created TryBinaryTree.java in I:\Netbeans OLDIES but KEEPIES\BinaryTree\src\binarytree\TryBinaryTree.java
因此,我考虑将输出放入JTable
中,假设在执行结束时调整列大小并获得看起来更像电子表格并因此更具可读性的结果很简单:
So I thought about putting the output into a JTable
, assuming it would be a simple matter to adjust column size at the end of execution and get results that look more like a spreadsheet and thus be more readable:
我以前从未使用过JTable
,所以我尝试了一个简单的程序来查看我是否完全可以制作JTable
.我做到了在这里:
I'd never used JTable
before, so I tried a simple program to see if I could make a JTable
at all. I did. Here it is:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class TestJTable
{
static void init()
{
fraFrame = new JFrame();
fraFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dftTableModel = new DefaultTableModel(0 , 4);
tblOutput = new JTable(dftTableModel);
scrOutput = new JScrollPane(tblOutput);
colMdl = tblOutput.getColumnModel();
fraFrame.add(scrOutput); // NOTE: add the SCROLL PANE, not table
fraFrame.setVisible(true);
fraFrame.pack();
}
public static void main(String[] args)
{
init();
tblOutput.doLayout();
dftTableModel.addRow(new Object[]{"Action", "Filename", "", "Path"});
for (int i = 0; i < 100; i++)
{
String c0 = Utilities.Utilities.repeat("x", (int) (Math.random()*50));
dftTableModel.addRow(new Object[]{((Math.sin(i)) > 0 ? "Updated" : "Created"),
c0, "in", c0});
k = Math.max(k, c0.length());
}
}
static int k;
static JFrame fraFrame ;
static JTable tblOutput;
static DefaultTableModel dftTableModel;
static JScrollPane scrOutput;
static TableColumnModel colMdl;
}
这是输出:
我希望能够完全根据需要设置列的大小.
I want to be able to set the columns' sizes exactly as I want.
我花了一个小时的时间来旋转尺寸,首选尺寸,工作台尺寸,自动调整大小(而不是自动调整大小)的车轮,这使我在程序的不同部分尝试了不同组合的以下操作,尽管取出pack
,但都没有令人满意的结果有所帮助:
An hour of spinning my wheels with sizes, preferred sizes, table sizes, autoresizing (and not) led me to try the following in varying combinations at varying parts of the program, none producing satisfactory results, although taking out pack
helped somewhat:
fraFrame.setSize(1400, 600);
tblOutput.setSize(1500,400);
colMdl.getColumn(0).setPreferredWidth(15);
colMdl.getColumn(1).setPreferredWidth(10*k);
colMdl.getColumn(2).setPreferredWidth(5);
colMdl.getColumn(3).setPreferredWidth(10*k);
所以我不得不问:
(a)具体来说,鉴于我知道k
是Filename
中的字符数,并且'Path columns, how can I make the contents of those columns be exactly fit their columns? (I assume
font'大小将输入图片.)
(a) Specifically, given that I know that k
is the number of characters in the Filename
and 'Pathcolumns, how can I make the contents of those columns be exactly fit their columns? (I assume
font` size will enter the picture.)
(b)通常,如何更改JTable
中的列大小?例如,一列将始终显示"in".如何使该列不超过该宽度?
(b) Generally, how do I change the column size in a JTable
? E.g., one column will always say "in". How to make the column be no wider than that?
编辑
感谢@camickr,在上面的测试程序和实际的下面的输出中轻松获得所需的输出是很容易的:
Thanks to @camickr, it was EASY to get the desired output, both in the test program above and in the real thing, output below:
推荐答案
查看表格列调整器.
您可以基于以下条件设置列宽:
You can set the column width based on:
- 列标题
- 该列每一行中的数据
- 标题或数据中较大的一个
该类还支持其他功能,以允许在更改数据时动态调整大小.
The class also supports other features to allow for dynamic resizing if the data is changed.
或者您可以使用提供的基本代码,该代码仅使用JTable的prepareRenderer(...)
方法来确定最大宽度.
Or you can use the basic code provided which just uses the prepareRenderer(...)
method of the JTable to determine the maximum width.
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 );
}
FontMetrics fm = table.getFontMetrics( table.getFont() );
int charWidth = fm.stringWidth("a");
System.out.println( charWidth );
这篇关于如何使JTable列大小完全(或紧密)适合内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!