Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JTables with JButtons, I'm overlooking something

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default JTables with JButtons, I'm overlooking something

    Ok, I have two columns of my JTable rendered to hold JButtons. When those JButtons are pressed, it will fire a method that opens the appropriate JFrame. My problem is that I can't get the ActionListener for each JButton to respond. Here is the snippets of code that are relevant for the issue at hand.

    public class UserControl extends JFrame
    {
    ...
    	Object[][] data;
    	String[] columnNames;
    	JTable table;
    	DefaultTableModel model;
    ...
        	public UserControl(boolean isSOC)
        	{
    	...
    		table.getColumnModel().getColumn(4).setCellRenderer(new TableCellRenderer()
    	        	{
    	            	public Component getTableCellRendererComponent(
    						JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    						{
    							final String val = value+"";
    							JButton rendererComponent = new JButton(val);
    							rendererComponent.addActionListener(new ActionListener(){
    								public void actionPerformed(ActionEvent e)
    								{
    									System.out.println("Action performed");
    									UserControl.this.openContact(val);
    								}
    							});
    							return rendererComponent;
    						}
    			});
    		table.getColumnModel().getColumn(5).setCellRenderer(new TableCellRenderer()
    	        	{
    	            	public Component getTableCellRendererComponent(
    						JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    						{
    							final String val = value+"";
    							JButton rendererComponent = new JButton(val);
    							rendererComponent.addActionListener(new ActionListener(){
    								public void actionPerformed(ActionEvent e)
    								{
    									System.out.println("Action performed");
    									UserControl.this.openContact(val);
    								}
    							});
    							return rendererComponent;
    						}
    			});
    	...
    	}
     
    	public void openContact(String s)
    	{
    		for(int i=0;i<contacts.size();i++)
    		{
    			if(contacts.get(i).name.equalsIgnoreCase(s))
    			{
    				new ContactInfo(s, contacts.get(i).toString());
    			}
    		}
    	}
    	...
    }

    Any thoughts?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: JTables with JButtons, I'm overlooking something

    Ok, scratch that issue. Figured it out. I used the SelectionModel, although it is not ideal for what I'm wanting, since it registers the cell as being selected and not the JButton as being pressed. But for now it is fine. If there are any better ways of doing it, I would appreciate learning them.

    Now I have a much more infuriating issue with my JTable. Something that I have never managed to do correctly. How do I make it so only a ONE cell can be selected at a time. My issue is that when a Button is selected, the entire row is selected. Which means the other JButton in that row cannot be pressed since the row is already selected. I know it is possible to fix this, but I have never figured out how and I cannot find anything on Google.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JTables with JButtons, I'm overlooking something

    table.setCellSelectionEnabled(true);

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: JTables with JButtons, I'm overlooking something

    Check out Rob Camick's blog post:
    Table Button Column Java Tips Weblog

    db

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: JTables with JButtons, I'm overlooking something

    Quote Originally Posted by Darryl.Burke View Post
    Check out Rob Camick's blog post:
    Table Button Column Java Tips Weblog

    db
    Ok, so I followed his instructions and wrote this par with his article:
    Action open = new AbstractAction(){
    				public void actionPerformed(ActionEvent e)
    				{
    					System.out.println("ActionPerformed");
    					String val = ((JButton)(e.getSource())).getText();
    					System.out.println("val: "+val);
    					UserControl.this.openContact(val);
    				}
    			};
    			ButtonColumn buttonColumn = new ButtonColumn(table,open,4);
    			ButtonColumn buttonCol = new ButtonColumn(table,open,5);

    But the event doesnt register when a button is pressed. Any thoughts?


    EDIT: Nvm, I think I may have gotten it. We'll see when we do an extensive test.
    Last edited by aussiemcgr; October 8th, 2010 at 12:55 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. live feeds and jtables
    By petem86 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 19th, 2010, 08:44 PM
  2. Replies: 2
    Last Post: March 6th, 2009, 02:00 PM