Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

JTable column header width

Avatar of allelopath
allelopath asked on
JavaEditors IDEs
5 Comments1 Solution1142 ViewsLast Modified:
How do I get column widths to automatically size to the width of the header?
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class MyTable extends JTable {

	private DefaultTableModel defaultTableModel;

	private Object[][] dataArray;
	private Object[] columnNameArray;

	private DefaultTableCellRenderer renderer;

	/**
	 * constructor
	 * 
	 * @param DefaultTableModel
	 */
	public MyTable(DefaultTableModel defaultTableModel) {

		this.setModel(defaultTableModel)	;

		renderer = new DefaultTableCellRenderer();

	}

	/**
	 * constructor
	 * 
	 * @param variableNameArray
	 * @param columnNameArray
	 */
	public MyTable(Object[][] variableNameArray, Object[] columnNameArray) {

		this.dataArray = variableNameArray;
		this.columnNameArray = columnNameArray;

		defaultTableModel = new DefaultTableModel(variableNameArray, columnNameArray);
		this.setModel(defaultTableModel)	;

		renderer = new DefaultTableCellRenderer();

	}

	/**
	 *  alternate shaded/not shaded rows
	 */
	@Override
	public Component prepareRenderer (TableCellRenderer renderer,int Index_row, int Index_col) {

		Component component = super.prepareRenderer(renderer, Index_row, Index_col);
		// space around border of each cell in table
		((JComponent) component).setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0)); // pxl

		//even index, selected or not selected
		if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
			component.setBackground(Color.lightGray);
			component.setForeground(Color.black); // otherwise text disappears
		} 
		else {
			component.setBackground(Color.white);
			component.setForeground(Color.black); // otherwise text disappears
		}

		return component;

	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception e){
			e.printStackTrace();
		}

	    String[] columnNameArray = {"one" , 
	    		"really wide wide wide",
	    		"three",
	    		"4" ,
	    		"5", 
	    		"six six six six",
	    		"7"};
	    
	    Object[][]  dataArray = {  };

		final MyTable panel = new MyTable(dataArray, columnNameArray);

		final JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(panel));
		frame.setTitle("My Table");
		frame.setPreferredSize(new Dimension(500, 500));
		frame.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
			}
		});

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLocation(300, 200);
		frame.pack();
		frame.setVisible(true);

	}

}
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananthFlag of United States of America imageVice President

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers