1

My aim is to create a JTable, and render the far left column cells only, with the aim of creating row headers for the table.

All row table examples I have come across online seem convoluted or do not fit my purposes, so I am wondering is there a simple way of creating JTable row headers through rendering the left column cells only?

Below I have code of a simple table with 2 columns and two rows. Is it possible someone could modify this, or explain in simple terms, how I could go about rendering the far left column for row header purposes.

Thank you.

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class GUITable extends JFrame{public GUITable(){


init();
  }

  public final void init(){
    String[] columnNames = {"", "Gross Weight"};

  Object[][] data = {
  {"", new Integer(100)},};

  final JTable table = new JTable(data, columnNames);
  table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  table.setFillsViewportHeight(true);

  JScrollPane scrollPane = new JScrollPane(table);
  add(scrollPane);
}

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

public void run() {
try {
  UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
  e.printStackTrace();
}
  GUITable ex = new GUITable();
  ex.setVisible(true);
    }
    });
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Php Pete
  • 762
  • 3
  • 14
  • 29

2 Answers2

1

your code example could be

import javax.swing.*;
import java.awt.*;

public class GUITable extends JFrame {

    public GUITable() {
        init();
    }

    public final void init() {
        String[] columnNames = {"", "Gross Weight"};
        Object[][] data = {{"", new Integer(100)},};
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Java6
            //UIManager.setLookAndFeel(
               //"javax.swing.plaf.nimbus.NimbusLookAndFeel");//Java7
        } catch (Exception fail) {
        }
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                GUITable ex = new GUITable();
                ex.setVisible(true);
            }
        });
    }
}

not sure from your descriptions, are you meaning Row Number Table by @camickr, or another half_sized attempt

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • mKorbel, thanks for the reply. Your "half sized" attempt is closer to what I was hoping for. Is there a simpler means to achieve row header render, by accessing the column index and rendering it to that of a header? – Php Pete Aug 08 '12 at 07:40
  • `Is there a simpler` not as far as I know, in most cases JTable don't want to works for add / remove rows, nor correctly for RowSorter or RowFilter, best could be to use code by @camickr, sure maybe there are another code – mKorbel Aug 08 '12 at 07:52
1

Yes - by using a custom TableCellRenderer, you can modify the way the first column (and first column only) displays.

Essentially you can use this to set the TableCellRenderer on the first column only:

table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());

And you can extend the DefaultTableCellRenderer to take care of any special rendering you want to do:

//Custom Renderer - does the default rendering except if told the row should be a different color
public static class CustomRenderer extends DefaultTableCellRenderer{

    public CustomRenderer(){
        super();
        //Customize the rendering however you want
        setBackground(UIManager.getColor("TableHeader.background"));
    }
}

To put it all together in your example:

import javax.swing.*; 
import javax.swing.table.DefaultTableCellRenderer;

import java.awt.*; 
public class TestTable extends JFrame{

    public TestTable(){ 
        init(); 
    } 

    public final void init(){ 
        String[] columnNames = {"", "Gross Weight"}; 

        Object[][] data = {{"", new Integer(100)},}; 

        final JTable table = new JTable(data, columnNames); 
        // Add Renderer to first column only
        table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
        table.setFillsViewportHeight(true); 

        JScrollPane scrollPane = new JScrollPane(table); 
        scrollPane.setPreferredSize(new Dimension(300, 200));
        add(scrollPane); 
    } 

    public static void main(String[] args) { 
        SwingUtilities.invokeLater(new Runnable() { 

            public void run() { 
                try { 
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
                TestTable ex = new TestTable();
                ex.pack();
                ex.setVisible(true); 

            } 
        }); 
    } 

    //Custom Renderer - does the default rendering except if told the row should be a different color
    public static class CustomRenderer extends DefaultTableCellRenderer{

        public CustomRenderer(){
            super();
            //Customize the rendering however you want
            setBackground(UIManager.getColor("TableHeader.background"));
        }
    }

} 
Nick Rippe
  • 6,465
  • 14
  • 30