Results 1 to 7 of 7
  1. #1
    dn baas is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Question JButtons and JTables

    Hi,

    I decided to add JButtons in a JTable (it's actually a DefaultTableModel). but instead of a normal button I see "javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,bor der=javax.swing.plaf.BorderUIResource$CompoundBord erUIResource@371d1794,flags=296,maximumSize=,minim umSize=,preferredSize=,defaultIcon=,disabledIcon=, disabledSelectedIcon=,margin=javax.swing.plaf.Inse tsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=verwijder,defaultCapable=tr ue]". Does someone know a solution? I use this code:
    Java Code:
    package View;
    
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.RowSorter;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    public class PanelView extends JPanel{
    	private static final long serialVersionUID = 1L;
    	protected JTable table;
    	protected JScrollPane pane;
    	DefaultTableModel model;
    	
    	public PanelView(){
    	}
    	
    	public void makeTabel()
    	{
    		System.out.println("tabel maken");
    		try
    		{
    			for(Object[] o: file.getList())
    			{
    				addRowIntoTable(o);
    			}
    		}
    		catch(NullPointerException e)
    		{
    			
    		}
    	}
    //don't mind this, it only makes a table and that works
    
    	/*public void createTable(String columns[], Object rows[][]){
    			model =
    	               new DefaultTableModel(rows, columns) {
    				public Class getColumnClass(int column) {
    	               Class returnValue;
    	               if ((column >= 0) && (column < getColumnCount())) {
    	                 returnValue = getValueAt(0, column).getClass();
    	               } else {
    	                 returnValue = Object.class;
    	               }
    	               return returnValue;
    	             }
    	           };
    	           JTable table = new JTable(model);
    	           RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    	           table.setRowSorter(sorter);
    	           pane = new JScrollPane(table);
    	}*/
    	
    	public void addRowIntoTable(Object row[])
    	{
    		model.insertRow(model.getRowCount(), row);
    	}
    }
    Java Code:
    package View;
    
    import javax.swing.JButton;
    
    
    public class PanelBand extends PanelView
    {
    	private static final long serialVersionUID = 1L;
    	
    	public PanelBand(String map){
    		String columns[] = {"test","test","test","test"};
    		JButton button = new JButton("verwijder");
    		Object row[] = {"test","test","test",button};
    		Object rows[][] = {};
    		createTable(columns, rows);
                    makeTabel();
    		add(pane);
    	}
    	
    }
    This isn't all of the code: some date is loaded from a file and put into the table.


    Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: JButtons and JTables

    You can't just add a JButton to a JTable. What you're seeing is what happens when the JButton's toString() method is called, because the JTable is trying to render the JButton as a JTable.

    Instead, you'll want to use a custom renderer that draws a JButton. Keep in mind that these JButtons won't accept click events.

    You might also want to rethink our logic here. Do you really want a JTable of JButtons? Or do you want a grid of JButtons (if so use a layout manager)? Or do you want a JTable that supports mouse input (if so use a MouseListener)?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    dn baas is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: JButtons and JTables

    Quote Originally Posted by KevinWorkman View Post
    You can't just add a JButton to a JTable. What you're seeing is what happens when the JButton's toString() method is called, because the JTable is trying to render the JButton as a JTable.

    Instead, you'll want to use a custom renderer that draws a JButton. Keep in mind that these JButtons won't accept click events.

    You might also want to rethink our logic here. Do you really want a JTable of JButtons? Or do you want a grid of JButtons (if so use a layout manager)? Or do you want a JTable that supports mouse input (if so use a MouseListener)?
    Well, I want to make a JTable with text (Strings) and JButtons. Those buttons need to remove a row when you click on it. Alternatives are also welcome. :P

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: JButtons and JTables

    Like I said, you can either use a custom renderer or a MouseListener, or even both.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: JButtons and JTables

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: JButtons and JTables

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    dn baas is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: JButtons and JTables

    I think I will figure something out. Thanks!

Similar Threads

  1. JLists and JTables
    By loopsnhoops in forum New To Java
    Replies: 2
    Last Post: 05-27-2011, 01:19 AM
  2. Jtables
    By amiral.smith in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2011, 03:22 PM
  3. JTables and Databases
    By sehudson in forum Advanced Java
    Replies: 8
    Last Post: 03-25-2010, 09:01 AM
  4. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 08:01 PM
  5. Scrolling with JTables
    By hiranya in forum AWT / Swing
    Replies: 5
    Last Post: 10-30-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •