Results 1 to 4 of 4
  1. #1
    leglesslizard is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Unhappy Creating and editing tables

    What is the best way to create a table that can be edited by the user? For example I want the user to be able to add and delete rows but am struggling to implement this. I am trying to use a JButton just to simply remove a selected row for now but am having trouble, I'll focus on the adding data once I get this figured out!

    Any help would be greatly appreciated!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Java Code:
    DefaultTableModel model = new DefaultTableModel(...);
    JTable table = new JTable( model );
    Now you can dynamically add/remove rows from the default table model. Read the API for the appropriate method to use.

  3. #3
    leglesslizard is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Wow thanks for the quick reply, really appreciate it. I have read through the JTable page on the sun website but I was quite confused by it. I have created a table and can insert and remove rows by using:

    tableModel.insertRow() etc.

    This works fine and I have my table. But why would this same command not work when used in a JButton? And how can you get around this? There must be an easy way I just can't figure it out. My action performed code is as follows and works for buttons such as exit:

    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();

    if (source == jBExit)
    {
    System.exit(1);
    }

    if(source == jBAdd)
    {
    (What type of code would go here to add a row??)
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by leglesslizard View Post
    This works fine and I have my table. But why would this same command not work when used in a JButton?
    Without seeing a small compilable version of your code, it's hard to say what mistakes you are making.


    Java Code:
    public void actionPerformed(ActionEvent event)
    	{
     		Object source = event.getSource();
     		 		
     		if (source == jBExit)
     		{
      			System.exit(1);
    		}
    		
    		if(source == jBAdd)
    		{
                            (What type of code would go here to add a row??)
    		}
    	}
    Again, this is very hard to say without seeing a small compilable version of your code. I can get this working with my code easily (and camickr can do the same even more easily), but without seeing some program structure, no one has a crystal ball to predict what code will work in that slot for you.

    Oh, please read the link in my signature below on using code tags as it will make it easier for others to help you here. Also, please read the link on how to create an SSCCE as this will help guide you towards creating a small compilable program that demonstrates your problem and that you can post here.

    Much luck!

Similar Threads

  1. Photo Editing
    By stekun in forum Advanced Java
    Replies: 3
    Last Post: 02-23-2010, 05:11 AM
  2. Problem editing existing UI
    By sargehendricks in forum AWT / Swing
    Replies: 3
    Last Post: 04-23-2009, 06:29 PM
  3. Text editing
    By right2001 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 07:03 AM
  4. Replies: 0
    Last Post: 12-23-2007, 07:50 AM

Tags for this Thread

Posting Permissions

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