Results 1 to 3 of 3
- 11-23-2011, 12:28 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
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(); } }
- 11-23-2011, 12:34 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 11-24-2011, 09:50 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
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.
Edit is my class member variable.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; } }
Subsequently I invoked these function in my actionPerformed class like that:
But if you have a better solution or you didn't think about this way, you can share your opinion quietlyJava 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); } }
thanks again
Similar Threads
-
Change button size on click
By dvreed77 in forum AWT / SwingReplies: 0Last Post: 03-04-2011, 02:40 AM -
Editable multiple line ToolTip on mouse right click
By smitharavi in forum AWT / SwingReplies: 1Last Post: 12-18-2010, 10:34 AM -
fill table with cells
By BigBear in forum AWT / SwingReplies: 3Last Post: 01-26-2010, 09:22 PM -
Non-Editable Table Column
By ld_pvl in forum AWT / SwingReplies: 6Last Post: 08-03-2009, 06:35 PM -
Color cells and rows in SWT table
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks