Results 1 to 3 of 3
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default Table cells change to editable by button click action

    Hi Experts,

    I have a table with 2 buttons on the frame. And now I would like to change the table cells' status by clicking on buttons. When the user clicks on the "Edit table" button, cells would be editable. The result of the clicking on the "Save table" would be a non-editable table.

    How can I achieve this? May I use listeners?
    Could someone lead me the rigth direction?

    Any help would be greatly appreciated!


    here is my simplified code. The correct expressions replaced with comments:
    Java Code:
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class JTableEditDemo extends JFrame implements ActionListener{
        String[] header = {"dog name", "dog age" };
    	DefaultTableModel model = new DefaultTableModel(header,3);
    	
    	JTable table = new JTable(model);
    	JButton button  = new JButton("Edit table");
    	JButton button2 = new JButton("Save table");
    	public JTableEditDemo()
    		{
    			JPanel panel = new JPanel();
    			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    			panel.add(button);
    			button.addActionListener(this);
    			panel.add(button2);
    			button2.addActionListener(this);
    			panel.add(new JScrollPane(table));
    			getContentPane().add(panel);
    			setTitle("dog");
    			setSize(200,200);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			setVisible(true);
    		}
    	public void actionPerformed(ActionEvent e)
    		{
    			if (e.getActionCommand().equals("Edit table"))
    				{
    					System.out.println("edit table");
    					// cells of the table will be editable - the code here
    				}
    			if (e.getActionCommand().equals("Save table"))
    				{
    					System.out.println("save table");
    					// cells of the table will be inactive (non-editable) - the code here
    				}
    			
    		}
    
    public static void main(String[] args)
    	{
    		new JTableEditDemo();
    	}
    }

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

    Default Re: Table cells change to editable by button click action

    You need to do two things.

    1) Extend your JTable and create a method like setTableEditable(...). You will then need to save the parameter value as a class variable in the extended class. Then when you click the "Edit Table" button you invoke this method and pass "true" as the boolean value. For the "Save Table" button you pass false.

    2) Editing of cells is controlled by the isCellEditable(...) method. So you need to override this method to return the value of the saved variable from the setTableEditable(...) method.

  3. #3
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default Re: Table cells change to editable by button click action

    Thanks camickr,
    your solution is quite useful, and gives me a kind of approach to learn the basics of the OOP. At first I created a MyTable class which extends the JTable.

    Java Code:
    class MyTable extends JTable{
    	
    			public MyTable(DefaultTableModel model)
    				{
    					super(model);
    				}
    			
    			public void setTableEditable(boolean isEdit)
    				{
    					Edit = isEdit;
    				}
    			public boolean isCellEditable(int row , int col)
    				{
    					return Edit;
    				}
    	
    	
    	}
    Edit is my class member variable.


    Subsequently I invoked these function in my actionPerformed class like that:

    Java Code:
    public void actionPerformed(ActionEvent e)
    		{
    			if (e.getActionCommand().equals("Edit table"))
    				{
    					System.out.println("edit table");
    					
    					
    					table.setTableEditable(true);
    					table.isCellEditable(3, header.length);
    				}
    			if (e.getActionCommand().equals("Save table"))
    				{
    					System.out.println("save table");
    					table.setTableEditable(false);
    					table.isCellEditable(3, header.length);
    					
    				}
    			
    		}
    But if you have a better solution or you didn't think about this way, you can share your opinion quietly

    thanks again

Similar Threads

  1. Change button size on click
    By dvreed77 in forum AWT / Swing
    Replies: 0
    Last Post: 03-04-2011, 02:40 AM
  2. Editable multiple line ToolTip on mouse right click
    By smitharavi in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2010, 10:34 AM
  3. fill table with cells
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2010, 09:22 PM
  4. Non-Editable Table Column
    By ld_pvl in forum AWT / Swing
    Replies: 6
    Last Post: 08-03-2009, 06:35 PM
  5. Color cells and rows in SWT table
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:31 PM

Posting Permissions

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