• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing and adding JComponents

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i click LOGIN button the app has to search for the username in database if  present i will be warned to enter a new username if username is new it has to welcome me with a label ,,i can remove all the components but can't add the welcome label.

 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some huge problems with your class that you have to address:

  • You user interface consists of a lot of static fields. Don't use static fields unless they are constants. Your application becomes very brittle: What happens when you create a second instance of your class?
  • You are mixing different applications layers together in one class. It acts as both a controller, view and persistence layer. Like using static fields, when your application becomes bigger the code will turn into spaghetti and it will become unmaintainable.
  • Your method names are not verbs. A method name should reflect what it does.
  • Your user interface is not being built on the event dispatch thread. While this rarely leads to problems, subtle issues may arise which are hard to debug.
  • You are not using layout managers. Layout managers are at the core of Swing. Not using them is asking for trouble. I wouldn't be surprised if this is what causes your issue.
  • You are calling the setVisible() method from your class' constructor. This method is able to start new threads and you should never spawn new processes from a constructor.
  • You're using a MouseAdapter to handle what should be handled by a FocusListener and an ActionListener.


  • First, split your class in one that just displays the user interface, and one that handles interactions with the database. Make them final, because there's not a good reason for them to be extensible. Remove static modifiers so that all fields are part of separate instances. Rename your methods so they express what they do. Rewrite the UI to use layout managers, in this particular case you may want to use a CardLayout to switch to a welcome screen after the user has authenticated. Use a FocusListener to change the text of your fields when they're empty, and use a AbstractActions to handle what happens when the user clicks the login and signup buttons.
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    dev spm wrote:(...) i can remove all the components but can't add the welcome label.


    The cause of this is the following code you have:

    After: 'panel.removeveAll()' the variable 'welcomelabel' becomes null, and so the line 'welcomelabel.setText("WELCOME");' results in a NullPpinterException. Therefore, create  new JLabel just before this line and you should be fine (well, apart from what Stephan mentioned...). By the way, revalidate is not necessary.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15484
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:After: 'panel.removeveAll()' the variable 'welcomelabel' becomes null


    No, that's impossible because that would require Java to support aliasing. The problem is that the variable was never initialized in the first place.
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're right.
     
    dev goku
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks guys...
    reply
      Bookmark Topic Watch Topic
    • New Topic