gpt4 book ai didi

java - 将文本格式化到 JTable 单元格中

转载 作者:行者123 更新时间:2023-12-01 14:07:32 25 4
gpt4 key购买 nike

我想以特定方式处理插入到 JTable 单元格中的文本。事实上,现在如果我写入一个单元格,文本将全部在同一行,而我希望在两行中看到它。

也许用图形描述我会更清楚。看,我想要第二个:

enter image description here

我的表代码在这里:

public class TablePanel extends JPanel
{
private JTable table;
public Tabella()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane jps = new JScrollPane(table);
add(jps);
add(new JScrollPane(table));
table.setCellSelectionEnabled(true);
table.setRowHeight(30);
TableColumn tcol;
for (int i=0; i<table.getColumnCount(); i++)
{
tcol = table.getColumnModel().getColumn(i);
tcol.setCellRenderer(new CustomTableCellRenderer());
}

table.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
int col = table.columnAtPoint(new Point(e.getX(), e.getY()));

if (col>0) {
if (e.getClickCount() > 1) {
if (row == 5 | row == 6)
{
JOptionPane.showMessageDialog(null, "Impossible to set lesson.");

return;
}
else {
table.getColumnName(col);
String day = table.getColumnName(col);
String hour = (String) table.getValueAt(row, 0);
InsertLesson cell = new InsertLesson(day, hour);
cel.setVisible(true);

}
}
}
}
}
);
}
private class MyTableModel extends AbstractTableModel {
private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private String[][] data = {{"8:30 - 9:30","","","","","",""},
{"9:30 - 10:30","","","","","",""},
{"10:30 - 11:30","","","","","",""},
{"11:30 - 12:30","","","","","",""},
{"12:30 - 13:30","","","","","",""},
{"13:30 - 14:30","","","","","",""},
{"14:30 - 15:30","","","","","",""},
{"15:30 - 16:30","","","","","",""},
{"16:30 - 17:30","","","","","",""}};

public int getColumnCount() {
return columns.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columns[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.lightGray);
}
else {
if (row % 2 == 0) {
cell.setBackground(new Color (245,245,250));
}
else {
cell.setBackground(new Color(250,250,240));
}
}

return cell;
}
}
}

感谢用户 Kiheru,找到了解决方案:我们必须:

1) 将要插入单元格的字符串格式居中;

2)将单元格本身的“空间”居中;

我们可以通过以下方式执行这些操作:

1)在需要插入单元格的字符串中插入html居中文本代码(html代码会自动识别);

2) 使用命令 setHorizo​​ntalAlignment(JLabel.CENTER); 将单元格本身居中。

这个问题可能对遇到此问题的每个人都有用。

最佳答案

可以使用 html 作为标签文本来拥有多行标签(DefaultTableCellRenderer 扩展 JLabel):

"<html>Math,<br>Class1</html>"

请注意,您可能需要按照here所述调整行高。 .

更新:除了换行之外,还需要将标签文本居中。

这需要在单元格渲染器上调用 setHorizo​​ntalAlignment(JLabel.CENTER) 以使文本 block 居中。另外,文本行需要相对于彼此居中,因此 html 代码最终会变成这样(实际代码具有更多格式):

<html><div style="text-align:center">Math,<br>Class1</div></html>

关于java - 将文本格式化到 JTable 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com