-1

I have a login form and a mainform. In the mainform I have a disabled JButton. I want to enable that JButton only if the username that I get from the login form is "admin". I have a singleton controller instantiated in both forms. I do something like

if(controller.admin=="admin"){jbutton.setEnabled(true)};

but I’m new to swing and I don't know where to use this code. I tried using it in the mainform's constructor but it didn't work.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • What about `MainForm_Load` – Adam Jun 10 '15 at 14:18
  • the load is called in the contructor. i did it. :D all i had to do was to put my if statement after the initComponents() function. private MainApp() { initComponents(); if(controller.admin.equalsIgnoreCase("admin")){ this.jButton5.setEnabled(true); } } – marius raiu Jun 10 '15 at 14:21
  • 1
    Did you try a windows listener to check when the form is actually loading up – Adam Jun 10 '15 at 14:22
  • *"I have a login form and a mainform"* 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) This sounds very familiar.. – Andrew Thompson Jun 10 '15 at 14:34
  • 3
    Don't use "==" for String comparisons. Instead you should be using the equals(...) method. For example: `"admin".equals(controller.admin)`. It is always a good idea to use the constant value first so you don't have to worry about null values in the controller.admin String. – camickr Jun 10 '15 at 14:37
  • As a complement for @camickr's comment (I didn't knew this) here's a link on why he said it: [Interview Java Equals](http://stackoverflow.com/questions/5712100/interview-java-equals) – Frakcool Jun 10 '15 at 17:23

1 Answers1

2
   if((controller.admin).equals("admin"))
  {
 jbutton.setEnabled(true);
  }

this is the correct code .please describe the questions with code

Sriram S
  • 533
  • 3
  • 26