Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to creat a jButton

  1. #1
    Junior Member benglish's Avatar
    Join Date
    Feb 2011
    Location
    LUV country
    Posts
    21
    My Mood
    Busy
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to creat a jButton

    Hi dears!

    I'm a new Java programmer. I want to write a program in which I ask the user to enter a number and then with the number (for example 4), I want to create 4 jButtons. How can I implement such code(s)?
    And how can I use them in a "for" loop. For example i want the user to enter 4 numbers (54, 73, 23, 75) and the code must enter these numbers into the jButtons I've just created respectively.

    TNX


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to creat a jButton

    What have you tried? Break the problem down...how would you do it for 1 button, then work off of that with a loop up to the number you want.

  3. #3
    Junior Member benglish's Avatar
    Join Date
    Feb 2011
    Location
    LUV country
    Posts
    21
    My Mood
    Busy
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to creat a jButton

    it's easy do to it for a single jButton, but how may i initiate the index of the jButton. for example, i want to write "45" as a text for jButton1 and "94" for jButton2 and so on. how may i address the index of jButtons in a "for" loop to place these numbers as their texts?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to creat a jButton

    Like I said, break the problem down. First, how would you store those values (array)? Second, how would you retrieve the values from the user? Next, how would you loop through that array and create the JButtons? Try to answer these questions, and post what you have so far.

  5. #5
    Junior Member benglish's Avatar
    Join Date
    Feb 2011
    Location
    LUV country
    Posts
    21
    My Mood
    Busy
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to creat a jButton

    See, i want to ask the user to enter a number; eg. 5. and then i have to create 5 jButtons in the GUI form of Java. How may i create a jButton in the GUI form and get the numbers one by one from the user and place them as jButton's text. I work with NetBeans IDE 6.8 in "Java Desktop Application" part. The following is the code i expect to write

         int i, k;
          i = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of jButtons you need"));
         for ( int j = 0; j < 5; j++ ) {
              //Now, i want to create 5 jButtons
              k = Integer.parseInt(JOptionPane.showInputDialog("Enter the numbers you want to set as the jButton"+ i + "'s   text"));
              //Now, numbers the user enters must be set as the ith jButton's text!
         }
    Falling in LUV is awfully simple, but falling out of LUV is simply awful

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to creat a jButton

    You are pretty close. All you need is to create the JButton based upon user input, then add that button to the user interface.
      i = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of jButtons you need"));
         for ( int j = 0; j < 5; j++ ) {
              //Now, i want to create 5 jButtons
              String k = JOptionPane.showInputDialog("Enter the numbers you want to set as the jButton"+ i + "'s   text");//no direct need to parse the int
               JButton button = new JButton(k);
              //add the button to the GUI
              //Now, numbers the user enters must be set as the ith jButton's text!
         }
    If you want the button to do something, you will also need to add a Listener to the button. If you are unfamiliar with adding components to a GUI and/or using swing component Listeners, I recommend reading the online tutorials at Sun (such as How to use Buttons). GUI builders are convenient, but IMO one should know the fundamentals of arranging a GUI and actions associated with that GUI.

Similar Threads

  1. please tell me how to creat an array of an inner cclass;
    By amr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 23rd, 2010, 04:24 PM
  2. how to creat one source multiple receiver
    By bhatti in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2010, 04:48 PM
  3. Creat a tree from list of strings
    By ayri in forum Loops & Control Statements
    Replies: 0
    Last Post: May 10th, 2010, 10:50 AM
  4. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 07:04 PM
  5. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM