• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Code Freezing at tools.btnAddStructure.addActionListener(new ActionListener()

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is freezing after I press on the action listener above. For the love of me, I can't figure it out. I ran a debug and it freezes near line 451 in class TLineActionListerners





 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to check your application in the following ways:

Make sure your Excel files are placed in a correct path. Sometimes code stuck when DB unable to read the files/path appropriately.  
The event actionlistener is executed in the GUI event processing thread (Swing is single threaded). You can't get any other event while in the callback.
check, look at where the method lags. In this case, it is where the user has to make a selection.
For more complex cases, e.g. if you need the UI to update without user intervention, you need to do your actual work in separate threads and use synchronization to coordinate the UI updates.
 
Anthony Johnson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tehmina Yaseen wrote:You need to check your application in the following ways:

Make sure your Excel files are placed in a correct path. Sometimes code stuck when DB unable to read the files/path appropriately.  
The event actionlistener is executed in the GUI event processing thread (Swing is single threaded). You can't get any other event while in the callback.
check, look at where the method lags. In this case, it is where the user has to make a selection.
For more complex cases, e.g. if you need the UI to update without user intervention, you need to do your actual work in separate threads and use synchronization to coordinate the UI updates.



Sorry, but I'm just beginning to use Java again for my job and I don't know exactly what you are talking about.
 
Marshal
Posts: 73001
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us the lines which you are blaming for the problem. TS TY has suggested a very likely explanation. Read more about it in the Java™ Tutorials.

I can see all sorts of things which I think shou‍ld be changed in that code:-
  • 1: Get rid of the “Hungarian Notation”. That means removed the prefixes saying which type each obejct is. Not cbLineNumber but lineNumberCombo please.
  • 2: The suppress warnings annotation sh‍ould only be used when you are sure the code is correct; using raw types is probably incorrect. If combo boxes are a parametrised type, make sure to fill in the type e.g. JComboBox<String>. The same applies to your List.
  • 3: You have a lot of static fields. Such use of static is probably a mistake. No, I think it is definitely a mistake
  • 4: The main method is for starting the application off. Code to update a databse shou‍ld be written elsewhere. It is probably a mistake to access the database from inside the main method at all.
  •  
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All of that code in that ActionListener should be in it's own class, for handling the loading or whatever it is doing.

    That way you can test that class in isolation, because at the moment how do you even know it works?

    If that code needs data from the GUI then the Listener should be creating an object of a suitable class that encapsulates that data in a form that is usable by the new class, and passing that on.  The processing probably ought to be on its own thread (look up SwingWorkers, or just go through the Concurrency tutorial Campbell linked to).
     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:All of that code in that ActionListener should be in it's own class, for handling the loading or whatever it is doing.

    That way you can test that class in isolation, because at the moment how do you even know it works?

    If that code needs data from the GUI then the Listener should be creating an object of a suitable class that encapsulates that data in a form that is usable by the new class, and passing that on.  The processing probably ought to be on its own thread (look up SwingWorkers, or just go through the Concurrency tutorial Campbell linked to).



    Just so I make sure I understand. Each ActionListener should have its own class or its own method.
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You should NOT use a null layout. Trying to specify the exact position and  size of each component will cause untold headaches in the future. What happens if someone tries to use your application but has a different screen size of resolution than you do? What happens if their default font is different than yours?

    Learn to use layout managers!
     
    Campbell Ritchie
    Marshal
    Posts: 73001
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anthony Johnson wrote:. . . . Each ActionListener should have its own class or its own method.

    No. Its own class or its own λ. Simply providing a method is not sufficient.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anthony Johnson wrote:
    Just so I make sure I understand. Each ActionListener should have its own class or its own method.



    I should have been a bit clearer.  That should have said "all the code in the actionPerformed method should be in its own class".

    So:


    The ExcelHandler would deal with all the Excel stuff (please don't rely on my naming, it's not my forte!)...
    Since it appears that you're working on the same files everywhere it can then hold all of that stuff, eg file names, opening and closing of streams and so on.  You can also then test this away from the GUI code to ensure it actually does what you think it should do, without all the GUI stuff getting in the way and confusing matters.
     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I put all the action listeners in its own class it and now it is freezing after I click on anything.
     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Here is the updated code. No more freezing. Now my issue is that it is not writing to the file.
     
    Anthony Johnson
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anthony Johnson wrote:Here is the updated code. No more freezing. Now my issue is that it is not writing to the file.



    I used debug and found out that its no executing the "workbookWrite.write(excelFileOut);", "excelFileOut.close();", or the "workbookWrite.close()'" lines of code. Can someone please help me figure out what is going on here.
     
    Campbell Ritchie
    Marshal
    Posts: 73001
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please explain how you are supposed to call those methods you mentioned.
    Nobody is going to read the amount of code you posted there. But I can see a design error in the last 20 or so lines, with all that repeated code. There is something very wrong about counting poles like that. If you have two poles, what happens to the reference to the third? What data structure are you using to store poles? I think you shou‍ld write code which will handle any number of poles.
    I suspect there are lots of other design problems hidden behind that long method, too.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I will say this once more, in a slightly different way.

    Get all the code that has nothing to do with actually displaying stuff on the GUI out of there.
    Create something (usually a controller or similar) that manages all work done on the excel sheet.
    Write it in a way that it can be tested without a GUI (eg create an instance of the class and call the methods you are testing).

    If you make this class (or classes) implement a simple interface then you can also test the GUI code without needing to actually do any Excel work by producing a mocked or stubbed version of the class.

    At the moment you have a ton of code that is irrelevant to the problem at hand, whether it's the issue of the GUI locking up or the issue of the file not being written to.
    There is simply too much going on.
     
    Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic