jTable单元格中的新行

时间:2015-04-30 09:39:51

标签: java swing jtable

这就是我在jTable中添加行的方法,你可以看到我尝试在zainteresowania中使用\ n但它被忽略了。如何在列表中的每条记录之后简单地添加新行?

for (Osoba a : osoby){
        List zainteresowania1 = a.getZainteresowania();  

        for (Iterator iterator2 = zainteresowania1.iterator(); iterator2.hasNext();){
                Zainteresowania nazwa = (Zainteresowania) iterator2.next();
                zainteresowania = zainteresowania + (nazwa.getZainteresowanie() + ".  " + "\n"); 
            }   
        model.addRow(new Object[]{a.getImie(), a.getNazwisko(), a.getEmail(), a.getTelefon(), zainteresowania});     
        zainteresowania = "";
        }

1 个答案:

答案 0 :(得分:3)

参考示例

   import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;

    public class TestJTableMultiline extends JFrame {
      public TestJTableMultiline() {
        super("Multi-Line Cell Example");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        DefaultTableModel dm = new DefaultTableModel() {
          public Class<String> getColumnClass(int columnIndex) {
            return String.class;
          }
          public boolean isCellEditable(int row, int column) {
            return false;
          }
        };
        dm.setDataVector(
            new Object[][]{
                {"A0, Line1\nA0, Line2\nA0, Line3", 
                 "B0, Line1\nB0, Line2", 
                 "C0, Line1"}, 
                {"A1, Line1", 
                 "B1, Line1\nB1, Line2", 
                 "C1, Line1"},
                {"A2, Line1", 
                 "B2, Line1", 
                 "C2, Line1"}
                }, 
                new Object[] {"A", "B", "C"});

        JTable table = new JTable(dm);
        table.setDefaultRenderer(String.class, new MultiLineTableCellRenderer());
        TableRowSorter<? extends TableModel> sort = new TableRowSorter<DefaultTableModel>(dm);
        table.setRowSorter(sort);
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll);
        setLocationByPlatform(true);
        setSize(400, 430);
        setVisible(true);
      }

      public static void main(String[] args) {
        TestJTableMultiline frame = new TestJTableMultiline();
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
    }

public class MultiLineTableCellRenderer extends JTextArea 
  implements TableCellRenderer {
  private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();

  public MultiLineTableCellRenderer() {
    setLineWrap(true);
    setWrapStyleWord(true);
    setOpaque(true);
  }

  public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected, boolean hasFocus,
      int row, int column) {
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      setBackground(table.getSelectionBackground());
    } else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }
    setFont(table.getFont());
    if (hasFocus) {
      setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
      if (table.isCellEditable(row, column)) {
        setForeground(UIManager.getColor("Table.focusCellForeground"));
        setBackground(UIManager.getColor("Table.focusCellBackground"));
      }
    } else {
      setBorder(new EmptyBorder(1, 2, 1, 2));
    }
    if (value != null) {
      setText(value.toString());
    } else {
      setText("");
    }
    adjustRowHeight(table, row, column);
    return this;
  }

  /**
   * Calculate the new preferred height for a given row, and sets the height on the table.
   */
  private void adjustRowHeight(JTable table, int row, int column) {
    //The trick to get this to work properly is to set the width of the column to the 
    //textarea. The reason for this is that getPreferredSize(), without a width tries 
    //to place all the text in one line. By setting the size with the with of the column, 
    //getPreferredSize() returnes the proper height which the row should have in
    //order to make room for the text.
    int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
    setSize(new Dimension(cWidth, 1000));
    int prefH = getPreferredSize().height;
    while (rowColHeight.size() <= row) {
      rowColHeight.add(new ArrayList<Integer>(column));
    }
    List<Integer> colHeights = rowColHeight.get(row);
    while (colHeights.size() <= column) {
      colHeights.add(0);
    }
    colHeights.set(column, prefH);
    int maxH = prefH;
    for (Integer colHeight : colHeights) {
      if (colHeight > maxH) {
        maxH = colHeight;
      }
    }
    if (table.getRowHeight(row) != maxH) {
      table.setRowHeight(row, maxH);
    }
  }
}