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 9 of 9

Thread: how to make a simple JButton on a JFrame window?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default how to make a simple JButton on a JFrame window?

    can anyone show me a demo how to make a JButton in a JFrame window...
    please.. dont have any idea how to do it...


    ill make it an input statement for some of my codes here.... i want to try that GUI instead of using JOptionPane's input dialog.


    is there any link in java tutorials?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    Do you know how to create a JFrame?

    Chris

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    yes of course...

    do i need to add the jbutton in a content pane?

    i know how to instantiate a Jbutton object. i just dont know how to add it in the frame and how to use it as an input routine
    Last edited by chronoz13; November 19th, 2009 at 10:05 AM.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    frame.add(new JButton());

    And you canuse setActionListener() and setActionCommand() to set it up to detect it being pressed.

    Chris

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

    Default Re: how to make a simple JButton on a JFrame window?

    If you are using a JFrame, you should make the call to (assuming your JFrame is named frame)

    frame.getContentPane().add(new JButton());

    In most circumstances you would add the JButton to a JPanel, along with any other things necessary (in your case perhaps a JLabel with a message?), then add that JPanel to the content pane, and as Freaky Chris describes add an action listener to the button to lister for events.

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    And you can use setActionListener() and setActionCommand() to set it up to detect it being pressed.
    ahh so its not that easy to make the JButton as a simple input routine for my program...

    any tnx for the information guys,,, ill post any further questions if necessary , coz i dont know how to use such methods...

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    and i think i didnt post any particular cases in which i have to use a jbutton... im gonna check my console files first.. then ill post any codes here to convert it's input routines into a JButton interface

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    this one..

    public class SwitchSample4_EnumType {
     
        private static enum Day {
     
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
        }
     
        public static void main(String[] args) {
     
            Day day = Day.values()[new Random().nextInt(Day.values().length)];
     
            switch (day) {
     
                case MONDAY: 
     
                    System.out.println("Its Monday");
                    break;
     
                case TUESDAY:
     
                    System.out.println("Its Tuesday");
                    break;
     
                case WEDNESDAY:
     
                    System.out.println("Its Wednesday");
                    break;
     
                case THURSDAY:
     
                    System.out.println("Its Thursday");
                    break;
     
     
                case FRIDAY:
     
                    System.out.println("Its Friday");
                    break;
     
                case SATURDAY:
     
                    System.out.println("Its Saturday");
                    break;
     
                case SUNDAY:
     
                    System.out.println("Its Sunday");
                    break;
     
                default:
     
                    System.out.println("what day is it?");
                    break;          
            }
        }
    }


    how can i make a simple GUI in Jframe for this code... make the switch cases the value for the JBUtton....
    get it?

    i mean... it will be the value for every button

  9. #9
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to make a simple JButton on a JFrame window?

    Here is, in my opinion, the most interesting part:
    JAVA Code:

    //If first time key in date run statement
    if(first == 0){
    ++first;//Track if is the first record key in
    items[i] = new Item(itemNumber, itemDesc, sellPrice, quantity);
    }
    //Else continue key in record
    else{
    entries = items[0].getEntries();//Get number of entries entered
    for(j=0; j<entries; ++j){

    duplicate = items[j].equals(itemNumber, itemDesc, sellPrice, quantity);//Check for duplication of entries

    if(duplicate == true) {
    Print.duplicateError();
    --i;
    --looping;
    break;
    }
    }
    if(duplicate == false)//else save record
    items[entries] = new Item(itemNumber, itemDesc, sellPrice, quantity);
    }

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. need help with JButton and switch statements
    By jjoubert in forum AWT / Java Swing
    Replies: 5
    Last Post: October 28th, 2009, 09:13 AM
  3. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM
  4. Replies: 3
    Last Post: February 26th, 2009, 05:21 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM