Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

how to update the edited data in textfield to table in java

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Report post to moderator
i had created the table with the 3 rows and also i had given an option to add the no of rows and it is getting done .if am selecting the row and clicking the option edit button it wil move to the other window and displays the rows data in textfield and allows to edit and it is not getting updated in this table so please help me i am a beginner of java...
Thank you in advance!!!

DisplayTableAction.java
*****************************

1. //*****DisplayTableAction.java****//
2.
3. package formlayout;
4. import javax.swing.*;
5.
6. import java.awt.*;
7. import java.awt.event.*;
8. import java.sql.*;
9. import java.util.*;
10. import javax.swing.table.*;
11. import javax.swing.event.*;
12.
13. import com.jgoodies.forms.layout.CellConstraints;
14. import com.jgoodies.forms.layout.FormLayout;
15.
16. public class DisplayTableAction extends JFrame implements ActionListener/*,FocusListener*/{
17. int counter1=0,counter2=0,counter3=0,counter4=0;
18.
19. JButton btnedit,btnok;
20.
21. JTable table;
22. JTextField txtfield;
23. JLabel lbltxt,lb4;
24. String data[][];
25. String columnNames [];
26. TablePanel paneltable;
27. Object obj1=null,obj2=null,obj3=null,obj4=null;
28. DefaultTableModel model;
29. int rowcount;
30. JPanel panel;
31. FormLayout form;
32. String rowdata[];
33.
34. public DisplayTableAction(){
35. super("Table");
36. form=new FormLayout("1dlu,pref,4dlu,pref,4dlu,pref","1dlu,pref,4dlu,pref,4dlu,pref,4dlu,pref");
37. panel=new JPanel(form);
38. CellConstraints cc=new CellConstraints();
39. data = new String[][]{{"05","MSc","Mangalore","2009"},{"06","MBA","Mangalore","2009"},{"05","MSc","Mangalore","2010"}};
40.
41. columnNames =new String[] {"S.NO","Qualification","University/College",
42. "Year Of Passing"};
43.
44.
45.
46. btnedit=new JButton("Edit");
47. panel.add(btnedit,cc.xy(4, 2));
48. btnok=new JButton("OK");
49. panel.add(btnok,cc.xy(6,4 ));
50. lbltxt=new JLabel("No: of Rows to be added to the table");
51. panel.add(lbltxt,cc.xy(2, 4));
52. txtfield=new JTextField(10);
53. panel.add(txtfield,cc.xy(4, 4));
54.
55. lb4=new JLabel("Enter only Numbers");
56. panel.add(lb4,cc.xy(4, 6));
57. model = new DefaultTableModel(data, columnNames);
58.
59. table = new JTable(model){
60.
61. @Override
62. public boolean isCellEditable(int row, int coloumn) {
63.
64. return false;
65.
66. }
67. };
68. table.setSelectionBackground(Color.orange);
69. table.setShowGrid(true);
70. table.setShowVerticalLines(true);
71. table.setGridColor(Color.black);
72.
73. table.getTableHeader();
74. JScrollPane scrollPane = new JScrollPane(table);
75. panel.add(scrollPane,cc.xy(2, 2));
76.
77. this.add(panel);
78.
79. this.setVisible(true);
80. this.setSize(800,500);
81. // model.fireTableDataChanged();
82.
83. btnedit.addActionListener(this);
84. btnok.addActionListener(this);
85. lb4.setVisible(false);
86.
126.
127. }
128.
129.
130.
131. public void actionPerformed(ActionEvent e){
132.
133. if(e.getSource().equals(btnedit)){
134. if(table.getSelectedRow()==-1){
135. JOptionPane.showMessageDialog(DisplayTableAction .this,"Please Select a Row From The Table", "", JOptionPane.ERROR_MESSAGE);
136. }
137. else{
138.
139. getTableData();
140.
141. }
142.
143.
144.
145. }
146. else if(e.getSource().equals(btnok)){
147. getRowData();
148.
149. }
150. }
151. public int getRowData(){
152. String s1=txtfield.getText();
153.
154. if(s1.equals("")){
155. JOptionPane.showMessageDialog(DisplayTableAction .this,"Fields cannot be blank.", "", JOptionPane.ERROR_MESSAGE);
156. txtfield.requestFocus();
157. }
158. else{
159. int s=Integer.parseInt(s1);
160.
161. int row;
162. rowdata=new String[]{"08","MTech","Mysore","2012"};
163.
164. model.setRowCount(3);
165.
166. for(row=0;row<s;row++){
167.
168. model.addRow(rowdata);
169.
170. //model.fireTableDataChanged();
171. }
172.
173. rowcount=model.getRowCount();
174.
175. }
176. return rowcount;
177. }
178.
179. public void getTableData(){
180.
181. int i = table.getSelectedRow();
182.
183. obj1 = GetData(table, i, 0);
184. obj2 = GetData(table, i, 1);
185. obj3 = GetData(table, i, 2);
186. obj4 = GetData(table, i, 3);
187. paneltable = new TablePanel();
188. paneltable.rowdata(i);
189. paneltable.text1.setText(obj1.toString());
190. paneltable.text2.setText(obj2.toString());
191. paneltable.text3.setText(obj3.toString());
192. paneltable.text4.setText(obj4.toString());
193.
194. //paneltable.setVisible(true);
195. paneltable.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
196. this.dispose();
197. //new TablePanel();
198.
199. }
200.
201.
202.
203. public void setTextData(String s1,String s2,String s3,String s4,int row_number){
204.
205. model.setValueAt(s1, row_number, 0);
206. model.setValueAt(s2, row_number, 1);
207. model.setValueAt(s3, row_number, 2);
208. model.setValueAt(s4, row_number, 3);
209.
210. }
211. public Object GetData(JTable table, int row_index, int col_index){
212.
213. return table.getModel().getValueAt(row_index, col_index);
214. }
215.
216. public static void main(String args[]){
217. new DisplayTableAction();
218.
219.
220. }
221.
222. }
223.
224.
225. //****TablePanel.java****//
226.
227.
228. package formlayout;
229.
230. import java.awt.Color;
231. import java.awt.event.ActionEvent;
232. import java.awt.event.ActionListener;
233. import java.awt.event.KeyAdapter;
234. import java.awt.event.KeyEvent;
235. import java.awt.event.KeyListener;
236. import java.util.Vector;
237.
238. import javax.swing.BoxLayout;
239. import javax.swing.JButton;
240. import javax.swing.JFrame;
241. import javax.swing.JLabel;
242. import javax.swing.JPanel;
243. import javax.swing.JTextField;
244. import javax.swing.table.DefaultTableModel;
245.
246. import com.jgoodies.forms.layout.CellConstraints;
247. import com.jgoodies.forms.layout.FormLayout;
248. import com.sun.media.sound.Toolkit;
249.
250. public class TablePanel extends JFrame implements ActionListener {
251. JFrame frame;
252. JLabel lab1,lab2,lab3,lab4,lb3,lb2,lb1,lb4;
253. JTextField text1,text2,text3,text4;
254. JPanel panel;
255. FormLayout form;
256. JButton btnok;
257. DisplayTableAction display;
258. int row_number;
259. public TablePanel(){
260.
261. super("EDUCATIONAL QUALIFICATION");
262.
263. form=new FormLayout("1dlu,pref,4dlu,pref,4dlu,pref","1dlu,pref,4dlu,pref,4dlu,pref,4dlu,pref,4dlu,pref");
264. CellConstraints cc= new CellConstraints();
265. panel=new JPanel(form);
266.
267. lab1=new JLabel("SNO");
268. panel.add(lab1,cc.xy(2, 2));
269. text1=new JTextField(20);
270. panel.add(text1,cc.xy(4, 2));
271.
272. lb1 = new JLabel("Enter Only Numbers");
273. panel.add(lb1,cc.xy(6, 2));
274.
275. lab2=new JLabel("Qualification");
276. panel.add(lab2,cc.xy(2, 4));
277. text2=new JTextField(20);
278. panel.add(text2,cc.xy(4, 4));
279. lb2 = new JLabel("Enter Only Letters");
280. panel.add(lb2,cc.xy(6, 4));
281. lab3=new JLabel("University/College");
282. panel.add(lab3,cc.xy(2, 6));
283. text3=new JTextField(20);
284. panel.add(text3,cc.xy(4, 6));
285. lb3 = new JLabel("Enter Only Letters");
286. panel.add(lb3,cc.xy(6, 6));
287. lab4=new JLabel("Year Of Passing: yyyy");
288. panel.add(lab4,cc.xy(2, Cool);
289. text4=new JTextField(20);
290. panel.add(text4,cc.xy(4, Cool);
291. lb4 = new JLabel("Enter Only Numbers");
292. panel.add(lb4,cc.xy(6, Cool);
293. btnok=new JButton("OK");
294. panel.add(btnok,cc.xy(2, 10));
295. btnok.addActionListener(this);
296. this.add(panel);
297. this.setVisible(true);
298. this.pack();
299. lb3.setVisible(false);
300. lb2.setVisible(false);
301. lb1.setVisible(false);
302. lb4.setVisible(false);
431. }
432.
433. public void actionPerformed(ActionEvent e){
434. display=new DisplayTableAction();
435. if(e.getSource().equals(btnok)){
436. String s1=text1.getText();
437. String s2=text2.getText();
438. String s3=text3.getText();
439. String s4=text4.getText();
440.
441. display.setTextData(s1,s2,s3,s4,row_number);
442. this.dispose();
443.
444. }
445. }
446. public void rowdata(int rowindex)
447. {
448. this.row_number = rowindex;
449.
450. }
451.
452.
453. public static void main(String args[]){
454.
455. new TablePanel();
456. }
457.
458.
459. }


I am getting the error as
*******************************
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 3
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:648)
at formlayout.DisplayTableAction.setTextData(DisplayTableAction.java:203)
at formlayout.TablePanel.actionPerformed(TablePanel.java:214)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Report post to moderator
please do the needful
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Report post to moderator
you already have your answer.

why waste everyone's time reading
duplicate posts?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
prajna pa,
This is a duplicate of your other post.

Please create only one post for a problem. I think you have been given all needed help in the other post. Follow them, try it and if you still have a problem, reply back to that post.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!