0

Hi i want to show new line in DefaultTableModel but i dont know why table doesn't show enters. How to enable enters? If i have a string "stss\nsdd" it shows "stsssdd" but i want new line.

public class Main extends JFrame {

    DefaultTableModel model = new DefaultTableModel(
            new Object[][]{{"some", "text"}, {"any", "text"},
                {"even", "more"}, {"text", "str\nings"},
                {"and", "other"}, {"text", "values"}}, new Object[]{
                "Column 1", "Column 2"});

    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        JTable table = new JTable(model);
        table.setRowHeight(40);
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        pack();
    }

    public static void main(String arg[]) {
        new Main().setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Luffy
  • 677
  • 1
  • 6
  • 19

2 Answers2

1

That's because the DefaultTableCellRenderer uses JLabel as the component to paint the cells. And labels can't have new lines. You have to use your own TableCellRenderer that uses a TextComponent that accepts new lines, for example JTextArea.

final JTextArea textArea = new JTextArea(); // or static field
//...
table.setDefaultRenderer(Object.class, new TableCellRenderer()
{
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        textArea.setText(value.toString());
        return textArea;
    }

});

Try copying and pasting a text of a couple of lines, to see it work. Now you might want to cahnge the cellEditor and prevent the Enter key from finalizing the edition so it can be used to write new lines in the cells.

DSquare
  • 2,458
  • 17
  • 19
  • 1
    +1 for a custom renderer, -1 for creating a new component on each call – kleopatra Aug 05 '13 at 11:40
  • @kleopatra true, that's why DefaultTableCellRenderer **is** also a JLabel and in its implementation sets its texts and simply returns `this`. – DSquare Aug 05 '13 at 11:43
  • actuallly .. no: DTCR being-a JLabel is a design accident that shouldn't be repeated (as you correctly don't :) Not extending is no reason to not re-use the same instance of a rendering component. – kleopatra Aug 05 '13 at 11:46
  • ehh .. how about just doing it (vs. adding a comment)? Simply move the line out off the method and you are fine :-) – kleopatra Aug 05 '13 at 11:54
  • There is a lot things to change to make it works corectly in all cases. I will search for diffrent table. – Luffy Aug 05 '13 at 11:57
  • out off the method doesn't mean out off the anonymous class :-) – kleopatra Aug 05 '13 at 12:15
  • @Luffy there's no way around learning to use a framework, be it Swing or something else :-) Good luck, anyway – kleopatra Aug 05 '13 at 12:16
0

I did this in easy way

JLabel l = new JLabel("<html>Hello World!<br>blahblahblah</html>", SwingConstants.CENTER)
Luffy
  • 677
  • 1
  • 6
  • 19