我有一个简单的JTable,它显示来自另一个JTable的行的详细信息(以列格式)。这很好用。但是,有时连续的文本很长,因此用户最终不得不滚动而不是整齐。
如何将文本换行并允许行高改变以显示其中的所有文本。
以下是代码:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
DefaultTableModel newModel = new DefaultTableModel();
String rowName = "Row: " + selectedRow;
newModel.setColumnIdentifiers(new Object[]{rowName});
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
newModel.addRow(new Object[]{table.getModel().getValueAt(selectedRow, i)});
}
JTable newTable = new JTable(newModel) {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(140, 240);
}
};
newTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
newTable.setRowHeight(14, 30);
TableColumnAdjuster tcanewTable = new TableColumnAdjuster(newTable);
tcanewTable.setColumnHeaderIncluded(true);
tcanewTable.setColumnDataIncluded(true);
tcanewTable.setOnlyAdjustLarger( true );
tcanewTable.setDynamicAdjustment( true );
tcanewTable.adjustColumns();
// Apply any custom renderers and editors
JOptionPane.showMessageDialog(frame, new JScrollPane(newTable),
rowName, JOptionPane.PLAIN_MESSAGE);
}
}
});
答案 0 :(得分:6)
您可以使用JTextArea
作为表格中该列的TableCellRenderer
来完成此操作。例如:
static class WordWrapCellRenderer extends JTextArea implements TableCellRenderer {
WordWrapCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText(value.toString());
setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
return this;
}
}
在表格中使用WordWrapCellRenderer
:
table.getColumnModel().getColumn(columnIndex).setCellRenderer(new WordWrapCellRenderer());
答案 1 :(得分:0)
这是接受答案的更新。
通过限制 Bernoulli || Bernoulli
Bernoulli || Poisson
Beta || Beta
Beta || ContinuousBernoulli
Beta || Exponential
Beta || Gamma
Beta || Normal
Beta || Pareto
Beta || Uniform
Binomial || Binomial
Categorical || Categorical
Cauchy || Cauchy
ContinuousBernoulli || ContinuousBernoulli
ContinuousBernoulli || Exponential
ContinuousBernoulli || Normal
ContinuousBernoulli || Pareto
ContinuousBernoulli || Uniform
Dirichlet || Dirichlet
Exponential || Beta
Exponential || ContinuousBernoulli
Exponential || Exponential
Exponential || Gamma
Exponential || Gumbel
Exponential || Normal
Exponential || Pareto
Exponential || Uniform
ExponentialFamily || ExponentialFamily
Gamma || Beta
Gamma || ContinuousBernoulli
Gamma || Exponential
Gamma || Gamma
Gamma || Gumbel
Gamma || Normal
Gamma || Pareto
Gamma || Uniform
Geometric || Geometric
Gumbel || Beta
Gumbel || ContinuousBernoulli
Gumbel || Exponential
Gumbel || Gamma
Gumbel || Gumbel
Gumbel || Normal
Gumbel || Pareto
Gumbel || Uniform
HalfNormal || HalfNormal
Independent || Independent
Laplace || Beta
Laplace || ContinuousBernoulli
Laplace || Exponential
Laplace || Gamma
Laplace || Laplace
Laplace || Normal
Laplace || Pareto
Laplace || Uniform
LowRankMultivariateNormal || LowRankMultivariateNormal
LowRankMultivariateNormal || MultivariateNormal
MultivariateNormal || LowRankMultivariateNormal
MultivariateNormal || MultivariateNormal
Normal || Beta
Normal || ContinuousBernoulli
Normal || Exponential
Normal || Gamma
Normal || Gumbel
Normal || Normal
Normal || Pareto
Normal || Uniform
OneHotCategorical || OneHotCategorical
Pareto || Beta
Pareto || ContinuousBernoulli
Pareto || Exponential
Pareto || Gamma
Pareto || Normal
Pareto || Pareto
Pareto || Uniform
Poisson || Bernoulli
Poisson || Binomial
Poisson || Poisson
TransformedDistribution || TransformedDistribution
Uniform || Beta
Uniform || ContinuousBernoulli
Uniform || Exponential
Uniform || Gamma
Uniform || Gumbel
Uniform || Normal
Uniform || Pareto
Uniform || Uniform
调用的次数,使渲染器更加高效。
getPreferredSize()