Results 1 to 5 of 5
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default [SOLVED] Open a different class from the same package when button clicked.

    Hello,

    Im new here and fairly new to Java and object orientated programming. I have a pretty simple problem for you guys, but it is driving me insane.

    I have two classes, one called LoginBox and the other called MainInterface. They are both in the same package called VMD. Both of these classes extend JFrame and are two seperate windows.

    There is a button in LoginBox called "confirm". It has an actionListener attached to it which works (I have tested it with a simple println command). What I am trying to achieve, is when the user clicks the button "confirm" it closes the LoginBox and opens MainInterface.

    This is what I have at the moment:

    Java Code:
    public void actionPerformed(ActionEvent e) {
                            LoginBox.setVisible(false);
                            MainInterface.setVisible(true);
                        }
    The problem is, IntelliJ reports that "Non-Static method 'setVisible(boolean)' cannot be referenced from a static context.


    I hope you can help. I really don't know what I am doing wrong.

  2. #2
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

    Default

    you are almost there....

    just try to do with following under your actionPerformed section...

    String action = a.getActionCommand();

    if (action.equals("Comfirm")) {
    .....setVisible(true);
    ................


    hope it will help...
    regards,
    - Neeraj

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Java Code:
    public void actionPerformed(ActionEvent e) {
                            this.setVisible(false);
                            MainInterface mi=new MainInterface();
                            mi.setVisible(true);
                        }
    when you call MainInterface.setVisible(true/false), the compiler will assume setVisible to be a static method defined inside that class, but which is not the case. So you need to call the method with the reference of the class.

  4. #4
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    Hurrah!!! it worked :o

    Thank you so much dswastik, i've spent hours trying to get that work. I thought it had something to do with telling the program MainInterface is not in that class, I just didn't know how to write that in Java Syntax.


    Thank you to neeraj.singh, I now know how to assign multiple buttons to do different things; Atleast, I think that is what your code is referring to.

  5. #5
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

Similar Threads

  1. Replies: 11
    Last Post: 02-13-2009, 01:52 PM
  2. [SOLVED] Compiling Package Class
    By nwboy74 in forum New To Java
    Replies: 3
    Last Post: 11-15-2008, 12:19 AM
  3. Display success message in same page when submit button is clicked
    By am17mu in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:56 AM
  4. Compiling a class in a package from cmd
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 11:27 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
  •