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

Thread: Repressable button in GUI

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Repressable button in GUI

    This class shows a GUI with a JLabel, a JButton and a JTextField. When klicking the button, an array of Strings in another class randomizes a String from the array and adds it to the textfield in this class. This works fine.

    But I want the button to be repressable. I want the button to get a new randomized String from the array when I press it again, without restarting the program. How do I do?


    import com.sun.j3d.utils.behaviors.vp.WandViewBehavior.ResetViewListener;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
     
    public class p12NameGenGUI extends JFrame implements ActionListener
    {
     
        p12NamnLista randomp12Namn = new p12NamnLista();
        JButton knapp;
        JLabel textTK;
        JTextField visaNamn;
     
        public p12NameGenGUI()
        {
            textTK = new JLabel("Tryck på knappen för ett slumpat P12 namn!");
            textTK.setForeground(Color.red);
            knapp = new JButton("TRYCK NU");
     
            visaNamn = new JTextField();
            visaNamn.setColumns(10);
     
            this.add(textTK);
            this.add(knapp);
            knapp.addActionListener(this);
            this.add(visaNamn);
     
     
        }
     
     
        public static void main(String[] args)
        {
     
     
            p12NameGenGUI p = new p12NameGenGUI();
            p.setSize(300, 300);
            p.setLocation(200, 300);
            p.setDefaultCloseOperation(EXIT_ON_CLOSE);
            p.setLayout(new FlowLayout());
            p.setVisible(true);
        }
     
        public void actionPerformed(ActionEvent e)
        {
     
            visaNamn.setText(randomp12Namn.toString());
     
        }
    }//class end
    Last edited by KevinWorkman; February 24th, 2011 at 09:11 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Repressable button in GUI

    How is the button not "repressable"? What happens when you press it again?

    When posting code, make sure you use the code or highlight tags. I did it for you this time, but please be more mindful in the future.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Repressable button in GUI

    It's solved now Ok I should think of that

Similar Threads

  1. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  2. button positioning
    By pds8475 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 29th, 2011, 09:35 AM
  3. How to display a button with an image on it?
    By ice in forum AWT / Java Swing
    Replies: 10
    Last Post: November 13th, 2010, 06:20 AM
  4. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM

Tags for this Thread