Link to home
Start Free TrialLog in
Avatar of allelopath
allelopath

asked on

Set width in JTable with 1 column

In the code below, how do I get table1 to be only as wide as the text in it?

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * ThreeTablePanel
 */
public class ThreeTablePanel extends JPanel {
    
	private final static String TABLE1 =  "Table1";
    private final static String TABLE2 =  "Table2";
    private final static String TABLE3 =  "Table3";

    private JPanel centerPanel;
    
    private JTable table1;
    private JTable table2;
    private JTable  table3;

    
	private String[] table1ColumnArray = {"Fruit"};

	private Object[][]  table1DataArray = { 
			{"Apple"}, 
			{"Orange"}, 
			{"Lemon"}, 
			{"Lime"}, 
	};

	private String[] table2ColumnArray = {"Longer"};

	private Object[][]  table2DataArray = { 
			{"AppleAppleAppleAppleAppleAppleAppleApple"}, 
			{"OrangeOrangeOrangeOrangeOrangeOrangeOrangeOrange"}, 
			{"LemonLemonLemonLemonLemonLemonLemonLemonLemon"}, 
			{"LimeLimeLimeLimeLimeLimeLimeLimeLimeLime"}, 
	};
			
    /**
     * constructor
     */
    public ThreeTablePanel() {
        
    	super();
        
        setLayout();
    }

    public void setLayout() {

		DefaultTableModel defaultTableModel1 = new DefaultTableModel(table1DataArray, table1ColumnArray);
		DefaultTableModel defaultTableModel2 = new DefaultTableModel(table2DataArray, table2ColumnArray);
		
		// layout
		SpringLayout springLayout = new SpringLayout();
        centerPanel = new JPanel (springLayout);
        // table 1
        table1 = new JTable(defaultTableModel1);
        JScrollPane table1ScrollPane = new JScrollPane(table1);
        table1.setFillsViewportHeight(true);
        JPanel table1Panel = new JPanel(new BorderLayout());
        
        table1Panel.add(new JLabel(TABLE1), BorderLayout.PAGE_START);
        table1Panel.add(table1ScrollPane, BorderLayout.CENTER);

        centerPanel.add(table1Panel);

		// adjust constraints (ie position component)
		springLayout.putConstraint(SpringLayout.WEST, table1Panel, 5, SpringLayout.WEST, centerPanel);
		springLayout.putConstraint(SpringLayout.NORTH, table1Panel, 10, SpringLayout.NORTH, centerPanel);

        // table 2
        table2 = new JTable(defaultTableModel2);
        JScrollPane tables2ScrollPane = new JScrollPane(table2);
        table2.setFillsViewportHeight(true);
        JPanel table2Panel = new JPanel(new BorderLayout());
        table2Panel.add(new JLabel(TABLE2), BorderLayout.PAGE_START);
        table2Panel.add(tables2ScrollPane, BorderLayout.CENTER);

        centerPanel.add(table2Panel);
        
		// adjust constraints (ie position component)
        springLayout.putConstraint(SpringLayout.WEST, table2Panel, 5, SpringLayout.EAST, table1Panel);
		springLayout.putConstraint(SpringLayout.NORTH, table2Panel, 0, SpringLayout.NORTH, table1Panel);
		
        // table 3
        table3 = new JTable();
        JScrollPane table3ScrollPane = new JScrollPane(table3);
        table3.setFillsViewportHeight(true);
        JPanel table3Panel = new JPanel(new BorderLayout());
        table3Panel.add(new JLabel(TABLE3), BorderLayout.PAGE_START);
        table3Panel.add(table3ScrollPane, BorderLayout.CENTER);

        centerPanel.add(table3Panel);
        
		// adjust constraints (ie position component)
        springLayout.putConstraint(SpringLayout.WEST, table3Panel, 5, SpringLayout.EAST, table2Panel);
		springLayout.putConstraint(SpringLayout.NORTH, table3Panel, 0, SpringLayout.NORTH, table2Panel);

		// set constraints for center panel
		springLayout.putConstraint(SpringLayout.EAST, centerPanel, 5, SpringLayout.EAST, table3Panel);
		springLayout.putConstraint(SpringLayout.SOUTH, centerPanel, 10, SpringLayout.SOUTH, table3Panel);
		
        add(centerPanel, BorderLayout.CENTER);

    }

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

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

		final JFrame frame = new JFrame();
		final ThreeTablePanel threeTablePanel = new ThreeTablePanel();
		frame.add(threeTablePanel, BorderLayout.CENTER);
		frame.setTitle("ThreeTablePanel");
//		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);

	}

}

Open in new window

Avatar of ksivananth
ksivananth
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of allelopath
allelopath

ASKER

Thanks!
:-)