Results 1 to 6 of 6
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Cant dispatch event or listener?

    Hello,
    I have a strange issue. I have a button, which creates a new frame, with simple UI. In that UI there is additional button which is supposed to perform some DB recording but, apparently the actionEvent method is not called ( checked by dbg method )... here is the code:
    Java Code:
    addButton.addActionListener(new ActionListener() {
    								@Override
    								public void actionPerformed(ActionEvent e) {
    										//iniate new thread here
    										System.out.println("Adding...");
    										//disable main ADD button
    										final JFrame adding = new JFrame();
    										final JLabel fnamel = new JLabel("First name:");
    										final JLabel snamel = new JLabel("Second name: ");
    										final JLabel tname = new JLabel("Third name: ");
    										final JLabel jobl = new JLabel("Job: ");
    										final JFormattedTextField fname1 = new JFormattedTextField();
    										final JFormattedTextField sname1 = new JFormattedTextField();
    										final JButton addButton1 = new JButton("Add");
    										
    										addButton1.setEnabled(false);
    										
    										//enable control on closing
    										adding.addWindowListener(new WindowListener() {
    												public void windowDeactivated(WindowEvent e) {}
    												public void windowOpened(WindowEvent e) {
    													addButton.setEnabled(false);
    													}
    												public void windowIconified(WindowEvent e) {}
    												public void windowDeiconified(WindowEvent e) {}
    												public void windowClosing(WindowEvent e) {}
    												public void windowActivated(WindowEvent e) {}
    												@Override
    												public void windowClosed(WindowEvent we) {
    	// remove after test												Tasker.getThreads("addingThread").requestStop();
    													addButton.setEnabled(true);
    												}
    										});
    														
    														
    										addButton1.addActionListener(new ActionListener() {
    											@Override
    											public void actionPerformed(ActionEvent e) {
    												System.out.println("####################WORKING");
    												Tasker.getThreads("DBconn").init(new Runnable() {
    													@Override
    													public void run() {
    														System.err.println("CALLING ADD1 BUTTON");
    													
    														System.out.println("Trying to record to DB");
    															try {
    																Mongo mg = new Mongo();
    																DB db = mg.getDB("testdb");
    																System.err.format("Conection establieshed...\n");
    																DBCollection tab = db.getCollection("workers");
    																BasicDBObject doc = new BasicDBObject();
    																BasicDBObject w = new BasicDBObject("worker",
    																		new BasicDBObject("name", 
    																		(store.getPerson(0)).
    																			getPersonAsParameters()[0]));
    																System.err.println(store.getPerson(0).
    																	getPersonAsParameters()[0]);
    																w.put("payment", 100);
    																doc.put("employees", w);
    																tab.insert(doc);
    														} catch (UnknownHostException ukhe1) {
    															ukhe1.printStackTrace();
    														} catch (Exception mongoex) {
    															mongoex.printStackTrace();
    														} 
    													} 
    													});
    												
    												};
    											}); //close ADD button
    												}
    											});			
    										adding.setLayout(new BorderLayout());
    										adding.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    										adding.setSize(320,240);
    										JPanel inputWorker = new JPanel(new GridLayout(0,1));
    										fname1.setColumns(10);
    										sname1.setColumns(10);
    										/*occs must be created from interface with many 
    										many jobs... 100 jobs must be read from another class */
    										String[] occs = {"worker", "maintenance", "engeneer", "support"};
    										final JComboBox job = new JComboBox(occs);
    										//button event setup
    										fname1.getDocument().addDocumentListener(
    											 new DocumentListener() {
    									 	public void removeUpdate(DocumentEvent e) {
    											updateButton(e);
    										}
    										public void changedUpdate(DocumentEvent e) {
    											updateButton(e);
    										}
    										public void insertUpdate(DocumentEvent e) {
    											 updateButton(e);
    										}
    										
    										public void updateButton(DocumentEvent e) {
    											Document doc = (Document)e.getDocument();
    											int changeLength = e.getLength();
    											synchronized (addButton1) {
    											if ( (fname1.getText().length() > 2)  &&
    												(sname1.getText().length() > 2) )
    											{
    												addButton1.setEnabled(true);
    											} else {
    												addButton1.setEnabled(false);
    											}
    										}
    									}
    									});
    										
    									sname1.getDocument().addDocumentListener(
    									 new DocumentListener() {
    									 	public void removeUpdate(DocumentEvent e) {
    											updateButton(e);
    										}
    										public void changedUpdate(DocumentEvent e) {
    											updateButton(e);
    										}
    										public void insertUpdate(DocumentEvent e) {
    											 updateButton(e);
    										}
    										
    										public void updateButton(DocumentEvent e) {
    											
    											Document doc = (Document)e.getDocument();
    											int changeLength = e.getLength();
    											synchronized (addButton1) {
    											if ( (sname1.getText().length() > 2) &&
    												(fname1.getText().length() > 2) )  
    											{
    												addButton1.setEnabled(true);
    											} else {
    												addButton1.setEnabled(false);
    											}
    											}
    										}
    									});
    											
    									inputWorker.add(fnamel);
    									inputWorker.add(fname1);
    									inputWorker.add(snamel);
    									inputWorker.add(sname1);
    									inputWorker.add(jobl);
    									inputWorker.add(job);
    									inputWorker.add(addButton1);
    									adding.add(inputWorker);
    									adding.setVisible(true);
    									
    									
    											
    										/* check if both fields are input and then unlock button ADD */
    										
    																		}  
    						});	// END ADDING BUTTONS
    Any ideas?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,940
    Rep Power
    4

    Default Re: Cant dispatch event or listener?

    Well, I don't know what your doing but your strange issue is probably brought on by the strange (or at least out of the ordinary) way in which you are configuring your buttons. It looks like you are adding a bunch of actionListeners to buttons within an actionListener. Why would you do this? Second, your anonymous class is much too large. Anonymous classes should be relatively small (I would say no more than 20 lines). Otherwise, they get difficult to read.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: Cant dispatch event or listener?

    Well, clicking on the main addButton creates a new JFrame with components... I was prototyping, but I guess I`d better rewrite and simplify it... However your answer is wise, it does not help me why would the nested button does not respond.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,940
    Rep Power
    4

    Default Re: Cant dispatch event or listener?

    I recommend just writing a new Class and let the button instantiate that class. I believe it will be clearer and make it easier to diagnose the problem.
    One suggestion is to make certain that addButton1 is getting enabled. Have you put print statements there to check this?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: Cant dispatch event or listener?

    Yes, I have a debug console, and the button enables once 2 fields are filled with more than 2 characters. I`ll write a separate composite class, but I just don`t get why it would not work.

  6. #6
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: Cant dispatch event or listener?

    Sorry, you were very helpful, but I`ve had a waaay stupid JAVAC error... I`ve pointed a classpath to include com/ilian, thus in my com/ilian additional com/ilian was created and all were messed... fixed it and now everything works fine... DOOOH!

Similar Threads

  1. Dispatch event Mousemoved more humanlike
    By coolking in forum AWT / Swing
    Replies: 3
    Last Post: 02-03-2012, 10:36 PM
  2. Implementing an event listener?
    By jammas615 in forum Java 2D
    Replies: 3
    Last Post: 07-04-2011, 04:15 PM
  3. Replies: 5
    Last Post: 04-14-2010, 05:26 AM
  4. Error: Exception during event dispatch!
    By Javid in forum AWT / Swing
    Replies: 1
    Last Post: 08-02-2008, 01:20 PM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 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
  •