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

Thread: JButton help

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default JButton help

    hi,
    I've run into a problem with JButtons. When the applet runs the buttons are not visible until there is a mouse-over, if you resize the window the buttons disappear again until you put the mouse pointer over it.
    Has anyone run into this problem before?
    any help would be great thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JButton help

    Please post code

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JButton help

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
     
    public class JButtonTest extends JApplet implements ActionListener
    {
    	JButton btn1, btn2, btn3;
    	Container pane;
    	public void init()
    	{
    		pane = getContentPane();
    		pane.setLayout(new FlowLayout());
    		btn1 = new JButton("button 1");
    		btn2 = new JButton("button 2");
    		btn3 = new JButton("button 3");
    		pane.add(btn1);
    		pane.add(btn2);
    		pane.add(btn3);
    	}
    	public void paint(Graphics g)
    	{
    		g.drawString("hello", 10, 10);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
    }

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JButton help

    Ok, from what I understand about how Swing/Applets paint their sub-components is via the paint method. Since you over-wrote the paint method, the code for telling these components to paint themselves isn't there. I'm not quite sure why the components get painted after you put your mouse over them, though.

    There are two solutions:
    1. Call the super paint method inside your paint method.

    public void paint(Graphics g)
    {
         super.paint(g);
         // rest of your paint code
    }

    Note that if you do this, it will have the default panel background color as opposed to the white one. You can change this by using the setBackground() method.

    this.setBackground(Color.white);

    2. Manually calling the update method for the buttons. You can even create a loop using the getComponentCount() and getComponent() methods to make sure you re-painted all the components inside your panel, even if you decide to add more components later.

    public void paint(Graphics g)
    {
         for (int i = 0; i < this.getComponentCount(); i++)
         {
              this.getComponent(i).repaint();
         }
    }

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    tabutcher (March 9th, 2010)

  6. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JButton help

    thanks you helloworld922

Similar Threads

  1. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM
  2. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 10:29 AM
  3. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM
  4. [SOLVED] Interesing JButton Query
    By ravjot28 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 14th, 2010, 11:19 AM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM