将文本格式化为JTable单元格

时间:2013-09-13 10:31:37

标签: java swing tablecellrenderer

我想以特定方式处理插入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)使用命令setHorizontalAlignment(JLabel.CENTER);对中心单元本身。

这个问题可能对每个人都有用。

2 个答案:

答案 0 :(得分:1)

使用html作为标签文字可以使用多行标签(DefaultTableCellRenderer扩展JLabel):

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

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

<强>更新 除了换行之外,还需要使标签文本居中。

这需要在单元格渲染器上调用setHorizontalAlignment(JLabel.CENTER)以使文本块居中。此外,文本行希望相对于彼此居中,因此html代码最终喜欢(实际代码具有更多格式):

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

答案 1 :(得分:0)

我不知道从哪里得到这个,但我用TextAreaRenderer

完成了这个
public class TextAreaRenderer extends JTextArea implements TableCellRenderer {

    private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
    /** map from table to map of rows to map of column heights */
    private final Map cellSizes = new HashMap();


    public TextAreaRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
    }

    public Component getTableCellRendererComponent(
            JTable table, Object obj, boolean isSelected,
            boolean hasFocus, int row, int column) {

        // set the colours, etc. using the standard for that platform
        adaptee.getTableCellRendererComponent(table, obj,
                isSelected, hasFocus, row, column);
        setForeground(adaptee.getForeground());
        setBackground(adaptee.getBackground());
        setBorder(adaptee.getBorder());
        setFont(adaptee.getFont());
        setText(adaptee.getText());


        // This line was very important to get it working with JDK1.4
        TableColumnModel columnModel = table.getColumnModel();
        setSize(columnModel.getColumn(column).getWidth(), 100000);
        int height_wanted = (int) getPreferredSize().getHeight();
        addSize(table, row, column, height_wanted);
        height_wanted = findTotalMaximumRowSize(table, row);
        if (height_wanted != table.getRowHeight(row)) {
            table.setRowHeight(row, height_wanted);
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    private void addSize(JTable table, int row, int column, int height) {
        Map rows = (Map) cellSizes.get(table);
        if (rows == null) {
            cellSizes.put(table, rows = new HashMap());
        }
        Map rowheights = (Map) rows.get(new Integer(row));
        if (rowheights == null) {
            rows.put(new Integer(row), rowheights = new HashMap());
        }
        rowheights.put(new Integer(column), new Integer(height));
    }

    /**
     * Look through all columns and get the renderer.  If it is
     * also a TextAreaRenderer, we look at the maximum height in
     * its hash table for this row.
     */
    private int findTotalMaximumRowSize(JTable table, int row) {
        int maximum_height = 0;
        Enumeration columns = table.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn tc = (TableColumn) columns.nextElement();
            TableCellRenderer cellRenderer = tc.getCellRenderer();
            if (cellRenderer instanceof TextAreaRenderer) {
                TextAreaRenderer tar = (TextAreaRenderer) cellRenderer;
                maximum_height = Math.max(maximum_height,
                        tar.findMaximumRowSize(table, row));
            }
        }
        return maximum_height;
    }

    private int findMaximumRowSize(JTable table, int row) {
        Map rows = (Map) cellSizes.get(table);
        if (rows == null) {
            return 0;
        }
        Map rowheights = (Map) rows.get(new Integer(row));
        if (rowheights == null) {
            return 0;
        }
        int maximum_height = 0;
        for (Iterator it = rowheights.entrySet().iterator();
                it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            int cellHeight = ((Integer) entry.getValue()).intValue();
            maximum_height = Math.max(maximum_height, cellHeight);
        }
        return maximum_height;
    }
}

然后你就像这样使用它(我的是“Notes”专栏):

table.getColumn("Notes").setCellRenderer(new TextAreaRenderer());

每当出现\n时,它都会包装文本。我也用它来完成对“合并单元”的需求(你只需要有条理地了解如何包装换行符)。它对于合并的单元格要求并不完美,但它可以完成工作。