Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java beginners problem

  • 17-05-2013 5:05pm
    #1
    Closed Accounts Posts: 30


    Hi, so If started learning java and I was messing around with two classes, trying to create a dialog box in one class and call it up in the other. Can anyone tell me whats wrong with the below?? Your help would be really appreciated.

    below is the first class

    import javax.swing.JOptionPane;
    import java.util.Scanner;
    public class dominos {
    public static void message(){
    System.out.println("Enter your name:");
    Scanner imput = new Scanner(System.in);
    String name = imput.nextLine();
    JOptionPane.showMessageDialog(null, "welcome", name, JOptionPane.INFORMATION_MESSAGE);
    }
    }

    below is the second class

    public class earl {
    public static void main(String agrs[]){
    dominos domObject = new dominos();
    domObject.message();

    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    what exactly is wrong?

    As it looks like it should be giving no compile errors.


  • Closed Accounts Posts: 30 Derekmcauley7


    Hey, its not giving a compiler error message. The dialog box (JOptionPane.showMessageDialog(null, "welcome", name, JOptionPane.INFORMATION_MESSAGE); ) is not appearing.


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    The Dominos class needs to extend JFrame in order to use JOptionPane, it cannot be used on it's own

    If you want to just print out the entered name to th console use System.out.println("Welcome " + name);


  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    it appears you need a component to display the error message on, no idea why but this code works:
    import javax.swing.JOptionPane;
    
    import java.awt.Frame;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		System.out.println("Enter your name:");
    		Scanner imput = new Scanner(System.in);
    		String name = imput.nextLine();
    		System.out.println(name);
    		Frame f = new Frame();
    		f.show();
    		JOptionPane.showMessageDialog(f, "welcome", name,
    				JOptionPane.INFORMATION_MESSAGE);
    	}
    }
    


  • Closed Accounts Posts: 30 Derekmcauley7


    Hi, I was trying to do this but get the user to imput the name. I also wanted to try call it from one class to another.

    import java.awt.Frame;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class dominos {
    public static void main(String[] args) {

    JOptionPane.showMessageDialog(null, "welcome", "Derek",
    JOptionPane.INFORMATION_MESSAGE);
    }
    }


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 403 ✭✭counterpointaud


    Are you sure the JOptionPane is not there ? Maybe it is just hidden ?

    Have you tried running it, entering name in console, pressing enter, and then hitting alt + tab to check this ? I think if you want JOPtionPane to always be on top of all other windows, then you need to use a JFrame or other parent component reference as the first parameter, not null, as others have mentioned.

    Generally you don't mix console and JOptionPane i/o in the way you are doing, maybe consider having the user input on a JDialogBox or something, rather than using the console.


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    public class domino extends JFrame {

    That's all that's needed for it to work.


  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    Zero1986 wrote: »
    public class domino extends JFrame {

    That's all that's needed for it to work.

    I tried your solution it didn't work for me in the latest java 6 I believe.


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    mad turnip wrote: »
    I tried your solution it didn't work for me in the latest java 6 I believe.
    This is the exact code I tried and it worked for me no problem, I haven't updated my Java in a while though.
    [B]First class[/B]
    
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    import java.util.Scanner;
    
    public class dominos extends JFrame {
         public void message(){
              System.out.println("Enter your name:");
              Scanner imput = new Scanner(System.in);
              String name = imput.nextLine();
              JOptionPane.showMessageDialog(null, "welcome" + name, null, JOptionPane.INFORMATION_MESSAGE);
         }
    }
    
    
    [B]Second class[/B]
    
    public class earl {
         public static void main(String agrs[]){
              dominos domObject = new dominos();
              domObject.message();
         }
    }
    


  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    Zero1986 wrote: »
    This is the exact code I tried and it worked for me no problem, I haven't updated my Java in a while though.
    [B]First class[/B]
    
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    import java.util.Scanner;
    
    public class dominos extends JFrame {
         public void message(){
              System.out.println("Enter your name:");
              Scanner imput = new Scanner(System.in);
              String name = imput.nextLine();
              JOptionPane.showMessageDialog(null, "welcome" + name, null, JOptionPane.INFORMATION_MESSAGE);
         }
    }
    
    
    [B]Second class[/B]
    
    public class earl {
         public static void main(String agrs[]){
              dominos domObject = new dominos();
              domObject.message();
         }
    }
    

    Did you run this through eclipse by any chance?


  • Advertisement
  • Closed Accounts Posts: 30 Derekmcauley7


    mad turnip wrote: »
    Did you run this through eclipse by any chance?

    Yeah that worked. Thanks :-D


Advertisement