Php Pete
Php Pete

Reputation: 762

Java JTable: How to render left column cells for simple row header purposes

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);
    }
    });
  }
}

Upvotes: 1

Views: 2427

Answers (2)

Nick Rippe
Nick Rippe

Reputation: 6465

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"));
        }
    }

} 

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

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

Upvotes: 1

Related Questions