1

I have a problem with the line breaks in my JTable.

I tried many solutions but none of them worked, so I ask this question.

Can anyone help me fixing this, without changing my JTable constructor.

Some details about my code:

  • My programm shows JTables in two jTabbedPane and in TAb 0 are 4 more jtabbedPanes which contain the same table.
  • My programm only works in fullscreen.
  • This programm is just a test for the functions of jTable and jTabbedPanes.
  • My question focuses on the linebreaks in the cells of the jTable.

Code:

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.event.*;
import javax.swing.table.*;


public class Versuch01 extends JFrame {
  // Anfang Attribute
  JTabbedPane[] jTabbedPane;
  JPanel[] jPanelH;
  JPanel[] jPanelL;
  JScrollPane[] jScrollPane;
  JTable[] jTable;
  final int wordWrapColumnIndex = 5;
  // Ende Attribute
  public Versuch01() {
    // Frame-Initialisierung
    super();
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int frameWidth = 400;
    int frameHeight = 400;
    setSize(frameWidth, frameHeight);

    int x1 = (d.width - getSize().width) / 2;
    int y1 = (d.height - getSize().height) / 2;
    setLocation(x1, y1);

    int x = (int) d.getWidth();
    int y = (int) d.getHeight();
    setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);

    setLayout(new BorderLayout());
    setTitle("Versuch01");
    setResizable(true);

    String[] columnNames = {
                             "First Name", "Last Name", "Sport", "# of Years",
                             "Vegetarian"
                           };
    Object[][] data = {
                        {
                          "Kathy",
                          "Smithaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa",
                          "Snowboarding", new Integer(5), new Boolean(true)
                        },
                        {
                          "John", "Doe", "Rowing", new Integer(3),
                          new Boolean(true)
                        },
                        {
                          "Sue", "Black", "Knitting", new Integer(2),
                          new Boolean(false)
                        },
                        {
                          "Jane", "White", "Speed reading", new Integer(20),
                          new Boolean(false)
                        },
                        {
                          "Joe", "Brown", "Pool", new Integer(10),
                          new Boolean(false)
                        }
                      };

    // Anfang Komponenten
    jTabbedPane = new JTabbedPane[2];

    for (int i = 0; i < jTabbedPane.length; i++) {
      jTabbedPane[i] = new JTabbedPane(JTabbedPane.TOP,
                                       JTabbedPane.WRAP_TAB_LAYOUT);
    } // end of for

    jPanelH = new JPanel[2];

    for (int i = 0; i < jPanelH.length; i++) {
      jPanelH[i] = new JPanel();
      jTabbedPane[0].addTab("Tab " + i, jPanelH[i]);
    } // end of for

    jPanelL = new JPanel[4];
    jTable = new JTable[4];
    jScrollPane = new JScrollPane[4];

    for (int i = 0; i < jPanelL.length; i++) {
      jPanelL[i] = new JPanel();
      MultilineTableCell wordWrapRenderer = new  MultilineTableCell();
      jTable[i] = new JTable(data, columnNames) {
          public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == wordWrapColumnIndex) {
              return wordWrapRenderer;
            } else {
              return super.getCellRenderer(row, column);
            }
          }
        };
      jTable[i].setDefaultRenderer(String.class, new LineWrapCellRenderer());
      jTable[i].setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      jTable[i].getColumnModel().getColumn(0)
               .setPreferredWidth((int) ((double) x * (0.05))); //Gibt die relative Größe in dezimalen Zahlen zu Tabbele an.
      jTable[i].getColumnModel().getColumn(1)
               .setPreferredWidth((int) ((double) x * (0.65)));
      jTable[i].getColumnModel().getColumn(2)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jTable[i].getColumnModel().getColumn(3)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jTable[i].getColumnModel().getColumn(4)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jScrollPane[i] = new JScrollPane(jTable[i]);
      jScrollPane[i].setPreferredSize(new Dimension(x, y));
      jPanelL[i].add(jScrollPane[i]);
      jTabbedPane[1].addTab("Tab " + i, jPanelL[i]);
      jTabbedPane[1].setTabPlacement(JTabbedPane.TOP);
      jTabbedPane[1].setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
      jTabbedPane[1].setPreferredSize(new Dimension(x, y));
      jPanelH[0].add(jTabbedPane[1]);
    } // end of for

    add(jTabbedPane[0]);

    // Ende Komponenten
    setVisible(true);
  } // end of public Versuch01

  // Anfang Methoden
  public static void main(String[] args) {
    new Versuch01();
  } // end of main

  // Ende Methoden
} // end of class Versuch01
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Gusse
  • 23
  • 5
  • 2
    So, in other words, you want to "split" `"Smithaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa..."` into 2 or more lines? Use HTML tags, for [example](http://stackoverflow.com/questions/24088150/create-new-line-in-row-header-of-my-custom-jtable/24088505#24088505) – Frakcool Jan 03 '17 at 19:57
  • 1
    *"I tried many solutions but none of them worked,"* List the 'top 3' & why they failed for you. – Andrew Thompson Jan 03 '17 at 22:34
  • @Frakcool I want the cell to get bigger and make a line break when the String is too long. – Gusse Jan 04 '17 at 14:46
  • And what is *"too long"* for you? You need HTML tags and some logic to know where to add them in you String – Frakcool Jan 04 '17 at 14:53
  • @Andrew Thompson 1. http://stackoverflow.com/questions/965023/how-to-wrap-lines-in-a-jtable-cell(1. post) , error: the cell did not change 2. http://stackoverflow.com/questions/965023/how-to-wrap-lines-in-a-jtable-cell (5. post), error: nothing happend 3. Can´t find the link but I had to change my constructor. – Gusse Jan 04 '17 at 14:55
  • @Frakcool Too long for me is when the cell ends with "..." because the String does not fit in the cell. – Gusse Jan 04 '17 at 14:58
  • You need some logic to know when that's going to happen, for example after 15 characters, then you need to add a "
    " tag, that's just my idea, you need your own logic, again, when the "..." appear? And set a row height bigger in those cases
    – Frakcool Jan 04 '17 at 15:02
  • @Frakcool I will give it a try. – Gusse Jan 04 '17 at 15:04

0 Answers0