Thread: Jtable

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    Jtable


    I'm trying to make a program where basically the whole table is made up of different buttons, and i need each button to perform different actions. i got the table layout how i want it but i'm not sure on how to turn my sample string inputs in to jbuttons. I know how to do actionlistener so i'm not worried about that right now i just need to know how to put different buttons inside different cells.

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import javax.swing.DefaultCellEditor;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    public class TableExample
    {
    	public TableExample()
    	{
    		JFrame frame = new JFrame("JButtonTable Example");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		DefaultTableModel dm = new DefaultTableModel();
    		dm.setDataVector(new Object[][]{{"Category 1", "Button 1","Button 4"},{"Category 2", "Button 2","Button 5"},{"Category 3", "Button 3","Button 6"}}, new Object[]{"Category", "Item 1", "Item 2"});
    
    		JTable table = new JTable(dm);
    	//	table.getColumn("Show 1").setCellRenderer(new ButtonRenderer());
    
    		JScrollPane scroll = new JScrollPane(table);
    		frame.add(scroll);
    		frame.pack();
    		frame.setVisible(true);
    
    	}
    
    	public static void main(String[] args)
    	{
    		TableExample te = new TableExample();
    	}
    
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I think you might be better to use GridLayout instead of JTable . I'm not sure taht JTable can contain buttons.
    Using this : http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
    you can create table of buttons as big as u need.

IMN logo majestic logo threadwatch logo seochat tools logo