Results 1 to 13 of 13
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: JButton Input

  1. #1
    pirogun is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default JButton Input

    I was wondering how to get input from a JButton when it is clicked and I want it to open a sub-window in which there is text displayed
    Thank You

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JButton Input

    Your question has several parts, and to better help you, we really need to know which specific part has you stumped.

    Have you read the Java Swing tutorials on how to use JButtons? Do you know how to add ActionListeners to JButtons?
    Do you know how to show a JOptionPane? A JDialog?

    Best of all, could you show us what you've tried and what errors if any you're getting from Java?

    Luck and welcome to the forum.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

  4. #4
    pirogun is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: JButton Input

    Ok Well I want when I click the button Chapter 1 it will make a Dialog box with written information on Chapter 1
    Here is my code:
    Java Code:
    package lordoftheflies;
    
    import javax.swing.*;
    import java.awt.*;
    
    
    public class LordoftheFlies extends JFrame {
        
        public LordoftheFlies() {
            super("The Lord of the Flies");
            setSize(425, 250);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
            setLayout(flow);
            
            JPanel row1 = new JPanel();
            JLabel how = new JLabel("To use this application simply click on a chapter for a summary");
            row1.add(how);
            add(row1);
            
            JPanel row2 = new JPanel();
            JButton Ch1 = new JButton("Chapter 1");
            JButton Ch2 = new JButton("Chapter 2");
            JButton Ch3 = new JButton("Chapter 3");
            JButton Ch4 = new JButton("Chapter 4");
            row2.add(Ch1);
            row2.add(Ch2);
            row2.add(Ch3);
            row2.add(Ch4);
            add(row2);
            
            
            JPanel row3 = new JPanel();
            JButton Ch5 = new JButton("Chapter 5");
            JButton Ch6 = new JButton("Chapter 6");
            JButton Ch7 = new JButton("Chapter 7");
            JButton Ch8 = new JButton("Chapter 8");
            row3.add(Ch5);
            row3.add(Ch6);
            row3.add(Ch7);
            row3.add(Ch8);
            add(row3);        
            
            JPanel row4 = new JPanel();
            JButton Ch9 = new JButton("Chapter 9");
            JButton Ch10 = new JButton("Chapter 10");
            JButton Ch11 = new JButton("Chapter 11");
            JButton Ch12 = new JButton("Chapter 12");
            row4.add(Ch9);
            row4.add(Ch10);
            row4.add(Ch11);
            row4.add(Ch12);
            add(row4);     
            
            setVisible(true);
        }
    
        public static void main(String[] args) {
            LordoftheFlies tlotf = new LordoftheFlies();
        }
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: JButton Input

    Restating your objectives doesn't change that we have no idea what your problem is. Do you get error messages when compiling your code? Then post those exact messages here. Does your code not do what you want? Then fully explain what it does do instead. have you followed the previous advice and read the tutorials?

  6. #6
    pirogun is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: JButton Input

    Ok so my code creates a user interface that has 12 chapters with 12 buttons that have no events connected to them because I do not know how to do that so I need to add an actionlistener (I think because I am quite new to Java) that makes a JOptionPane popup so I can put text there summarizing the chapter from lord of the flies

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: JButton Input

    Yes we know what you want. We are not stupid. What we don't know is what your problem is. If you do not know how to add an action listener to your buttons then once again read the tutorial. Sheesh!

  8. #8
    pirogun is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: JButton Input

    Oh well Sorry I didn't mean that I meant is that Do I need multiple classes for the Application or How do I implement the JOptionPane

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: JButton Input

    You need as many classes as you like. You can use 1, 2 or 1000. It is totally upto you. As for JOptionPane you could read the Java API for that class and/or READ A TUTORIAL!

  10. #10
    pirogun is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: JButton Input

    Can I have a link please

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: JButton Input

    :headdesk:

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: JButton Input

    Sure why not: Google

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: JButton Input

    Hi pirogun.

    I realise you might be frustrated by how to go further with your program. But maybe others are feeling frustrated as well.

    Did you read the tutorial section that camickr linked to? It describes a worked example of how to use action listeners. The example there does not display anything in a dialog, it justs prints to System.out. But that would be a good start. If you get stuck understanding what is written there or implementing it in your own program, post your code and describe the problem you are having.

    Once you have the action listeners responding to button clicks you can think about how to display the summaries - using JOptionPane if you decide to try that. Again, the Tutorial is a very good resource and it has a page on How to make dialogs.

    -----

    Generally people who answer questions here don't try and write a mini textbooks. They assume that anyone posting a question can and will go through the standard resources (tutorials and textbooks etc). And, being technical folks, they assume the question will be posed in a specific way: usually involving code and a description of what is going wrong. (compiler messages, runtime exceptions, odd program behaviour). Imprecise questions, or a statement of an end goal without a question, tend to leave everyone a little frustrated.

    Anyway, good luck. And do post back if you have trouble with the action listeners.
    Last edited by pbrockway2; 11-23-2011 at 08:37 AM.
    Fubarable and DarrylBurke like this.

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  2. Replies: 0
    Last Post: 10-14-2011, 12:33 AM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  4. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 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
  •