Skip to main content

JXTableHeader with linewrap

No replies
Tycosson
Offline
Joined: 2011-05-12
Points: 0

Hi everyone,

I'm trying to create a JXTable with a custom header featuring linewrap. It should look something like expected.jpg (attachment 1).

What is the best way to achieve this ?

I don't get any line wrap (see attachment 2 : 'actual result.jpg') if I use

<span class="Apple-tab-span" style="white-space:pre"> </span>final JXTableHeader header = (JXTableHeader)table.getTableHeader();

<span class="Apple-tab-span" style="white-space:pre"> </span>if (header != null) {

<span class="Apple-tab-span" style="white-space:pre"> </span>header.setPreferredSize(new Dimension(50,50));

<span class="Apple-tab-span" style="white-space:pre"> </span>header.setBackground(Color.RED);

<span class="Apple-tab-span" style="white-space:pre"> </span>header.setForeground(Color.WHITE);

<span class="Apple-tab-span" style="white-space:pre"> </span>header.setAlignmentY(SwingConstants.CENTER);

<span class="Apple-tab-span" style="white-space:pre"> </span>}

Line wrap doesn't work (I get empty red header cells) and header sorting icons disapear if I use

<span class="Apple-tab-span" style="white-space:pre"> </span>header.setDefaultRenderer(new DefaultTableCellHeaderRenderer() {

<span class="Apple-tab-span" style="white-space:pre"> </span>@Override

<span class="Apple-tab-span" style="white-space:pre"> </span>public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

<span class="Apple-tab-span" style="white-space:pre"> </span>boolean hasFocus, int row, int column) {


<span class="Apple-tab-span" style="white-space:pre"> </span>JXLabel jxLabel = new JXLabel();

<span class="Apple-tab-span" style="white-space:pre"> </span>if (column &gt;= 0) {

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setText((String)value);

<span class="Apple-tab-span" style="white-space:pre"> </span>}

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setOpaque(true);

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setBackground(Color.RED);

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setForeground(Color.white);

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setLineWrap(true);

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setVerticalAlignment(SwingConstants.CENTER);

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setPreferredSize(new Dimension(50, 50));

<span class="Apple-tab-span" style="white-space:pre"> </span>jxLabel.setBorder(UIManager.getBorder(&quot;TableHeader.cellBorder&quot;));

<span class="Apple-tab-span" style="white-space:pre"> </span>return jxLabel;

<span class="Apple-tab-span" style="white-space:pre"> </span>}

<span class="Apple-tab-span" style="white-space:pre"> </span>});

Any suggestion ?

Regards, Laurent.

AttachmentSize
expected.jpg16.19 KB
actual_result.jpg20.17 KB