Results 1 to 10 of 10
Thread: Question about listener
- 07-07-2011, 02:13 AM #1
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
Question about listener
I think how listener works is that...a component is bound to a listener and then create a handler for that listener (a process(es) that will be done when someone do the action like pressing button, moving mouse, etc).
I wonder if I'm right on that.
But then about tablemodellistener, it supposedly detect a change in the data to trigger the handler, however the change on the data maybe done by something like saving data by clicking button, so in this case do I have to use actionlistener, mouselistener, or tablemodellistener ?
I want to have some fields to add or delete the table that is already shown there, and upon
pressing a button (save or delete), it'll affect directly to the table.
Should I bind the button to an actionlistener that process the queries then bind the table to a tablemodellistener ?
Thanks in advance.
- 07-07-2011, 02:33 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The components manages it's listeners and when you add an action listener, the component adds that listener to it's list of listeners. When something is used it sends an event to all it's listener and the listeners handle it.
If you want to change the items in a table with a button click you have a listener listening to the button(ActionListener), when the button is clicked the ActionEvent is sent to the listener and the appropriate action is taken.
This is my understanding of swing, while I'm no expert, I feel confident with what I said and hopefully people will correct me if I'm wrong.
You may want to look at the observer pattern because as far as I know this is what swing uses.
- 07-07-2011, 03:07 AM #3
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
Thanks for the reply,
So I don't need tablemodellistener unless I make the changes straight from the table data ?
What's the observer pattern that you talked about ? can you give me a link as to where to read it ?
- 07-07-2011, 03:27 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The best way to see is to try it. I have never done anything with tables, but if it allows you to get a selected item and remove items/add items you can do it with an action listener on a button
Observer pattern
- 07-07-2011, 04:03 AM #5
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
Yes, the best way is to try...I just ask while waiting for plugin installation cause it'll narrow down what I need to try and wondering if what I currently think of listener is correct, a wrong understanding will get me a wrong result.
ThanksLast edited by Levian; 07-07-2011 at 04:06 AM.
- 07-07-2011, 05:31 AM #6
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
OK, the error shown SQL problem.
Here's my code :
Error on console :Java Code:public class LOTest extends JFrame { private static final long serialVersionUID = -4640296748500842876L; private JPanel contentPane; private JTextField txMatSID; private JTextField txMatSName; QueryTableModel qstm = new QueryTableModel(); JTable MatSTable = new JTable(qstm); public LOTest() throws SQLException { setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 385, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 22, 357, 149); contentPane.add(scrollPane); scrollPane.setViewportView(MatSTable); MatSTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); qstm.setQuery("SELECT matsubid, matsubname from matsub"); MatSTable.setFillsViewportHeight(true); txMatSID = new JTextField(); txMatSID.setBounds(170, 178, 197, 20); contentPane.add(txMatSID); txMatSID.setColumns(10); txMatSName = new JTextField(); txMatSName.setBounds(170, 210, 198, 20); contentPane.add(txMatSName); txMatSName.setColumns(10); JButton btnUpdateS = new JButton("Update"); btnUpdateS.setBounds(144, 237, 89, 23); contentPane.add(btnUpdateS); btnUpdateS.addActionListener(upRecS()); } private ActionListener upRecS() throws SQLException { new JTable(qstm); qstm.qDB.executeUpdate("INSERT INTO matsub( matsubid, matsubname ) VALUES (" + "'" + txMatSID.getText() + "'" + "," + "'" + txMatSName.getText() + "'" +")"); return null; } }
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Duplicate entry '' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:19 90)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2619)
at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1698)
at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1617)
at LOTest.upRecS(LOTest.java:73)
at LOTest.<init>(LOTest.java:67)
at LOTest$1.run(LOTest.java:28)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 07-07-2011, 05:36 AM #7
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
On the first run, a button click adds a blank fields into the tables....on the next run, error as stated due adding blank fields which cause a duplicated entry (it's not even run and generated the above mentioned error on console).
Last edited by Levian; 07-07-2011 at 06:43 AM.
- 07-07-2011, 10:39 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
That first line is adding the ActionListener returned by the upRecS() method.Java Code:btnUpdateS.addActionListener(upRecS()); } private ActionListener upRecS() throws SQLException { new JTable(qstm); qstm.qDB.executeUpdate("INSERT INTO matsub( matsubid, matsubname ) VALUES (" + "'" + txMatSID.getText() + "'" + "," + "'" + txMatSName.getText() + "'" +")"); return null; }
Which is null everytime since that method only returns null.
Of course before it returns that null it will execute the code in the method, which executes that SQL.
You need to read the tutorials on how listeners work.
- 07-07-2011, 11:01 AM #9
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
Thanks, I'll give it a read then.
Figured out the problem is in the return null value, but I get error mark in line everytime I change/remove it.
Anyway, is that how it's done ? Get the button to add listener and define the action event (my problem is in the action event apparently which I need to read more) or it's a wrong way to do that.
- 07-07-2011, 11:12 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Listener question
By rclausing in forum New To JavaReplies: 8Last Post: 02-09-2010, 04:33 AM -
Tab Listener
By teckno101 in forum AWT / SwingReplies: 2Last Post: 09-29-2009, 09:40 PM -
question on listener interface
By Minu in forum Java ServletReplies: 1Last Post: 01-16-2009, 10:33 AM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-20-2008, 11:07 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-10-2008, 02:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks