Results 1 to 10 of 10
  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    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 ?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  5. #5
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    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.

    Thanks
    Last edited by Levian; 07-07-2011 at 05:06 AM.

  6. #6
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    OK, the error shown SQL problem.

    Here's my code :
    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;
    	}
    
    }
    Error on console :
    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)

  7. #7
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    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 07:43 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    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;
    	}
    That first line is adding the ActionListener returned by the upRecS() method.
    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.

  9. #9
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    The way you have done it is a complete misunderstanding of how it wires up.
    So read the tutorials.

Similar Threads

  1. Listener question
    By rclausing in forum New To Java
    Replies: 8
    Last Post: 02-09-2010, 05:33 AM
  2. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 10:40 PM
  3. question on listener interface
    By Minu in forum Java Servlet
    Replies: 1
    Last Post: 01-16-2009, 11:33 AM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-21-2008, 12:07 AM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 03:00 AM

Posting Permissions

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