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 on JPanel

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

    Default JButton on JPanel

    Hi,

    Here is my code on how I add a JButton onto a JPanel. However, the JButton completely takes over the whole JPanel, how do I set the size of the JButton so it doesn't take over the whole JPanel?

    import java.awt.BorderLayout;
     
    import javax.swing.*;
     
    public class test extends JFrame {
     
    	public test() {
    		super("Title");
    		JPanel panel = new JPanel();
    		add(panel);
     
    		JButton mybutton = new JButton("TEST BUTTON");
    		getContentPane().add(mybutton);
    	}
     
    }

    Here is what it looks like:



    Any help is appreciated,

    thanks


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JButton on JPanel

    Last edited by Darryl.Burke; March 21st, 2010 at 04:25 AM.

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

    Default Re: JButton on JPanel

    Quote Originally Posted by Darryl.Burke View Post
    Thanks, this is just what I was looking for, something that would explain everything to me.

    Much appreciated

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton on JPanel

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;


    public class Week1MouseEventsQ6 extends JFrame
    {
    DrawPanel canvas;

    public static void main(String[] args)
    {
    Week1MouseEventsQ6 w = new Week1MouseEventsQ6();
    w.setVisible(true);
    }

    public Week1MouseEventsQ6()
    {
    setTitle("Week2MouseEvents: starting code");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,220);
    setLocation(300,300);
    canvas = new DrawPanel(7, 5);
    add(canvas);
    }

    class DrawPanel extends JPanel implements MouseListener
    {
    int numCols;
    int numRows;

    int clickColumnIndex = -1;
    int clickRowIndex = -1;

    int buttonNumber = 0;

    boolean clickControl = false;

    public void mouseReleased(MouseEvent event)
    {
    System.out.println("Mouse Released");
    System.out.println("X: " + event.getX() + "Y: " + event.getY());
    }
    public void mousePressed(MouseEvent event)
    {
    System.out.println("Mouse Pressed");
    System.out.println("X: " + event.getX() + "Y: " + event.getY());
    }
    public void mouseClicked(MouseEvent event)
    {
    buttonNumber = event.getButton();

    clickColumnIndex = (numCols) * (event.getX()) / (getWidth());
    clickRowIndex = (numRows) * (event.getY()) / (getHeight());

    clickControl = true;

    System.out.println("Mouse Clicked");
    System.out.println("X: " + event.getX() + "Y: " + event.getY());
    System.out.println("Row " + clickRowIndex + "Column " + clickColumnIndex);

    repaint();

    }
    public void mouseEntered(MouseEvent event)
    {
    System.out.println("Mouse Entered");
    System.out.println("X: " + event.getX() + "Y: " + event.getY());
    }
    public void mouseExited(MouseEvent event)
    {
    System.out.println("Mouse Exited");
    System.out.println("X: " + event.getX() + "Y: " + event.getY());
    }


    public DrawPanel(int nc, int nr)
    {
    numCols = nc;
    numRows = nr;

    addMouseListener(this);
    }

    Rectangle getRect(int thisCol, int thisRow)
    {
    // if input is out of range, return "null"
    if(thisCol <0 || thisRow < 0)
    return null;
    if(thisCol>=numCols || thisRow>=numRows)
    return null;

    // otherwise, make and return the Rectangle
    int w = getWidth()/numCols;
    int h = getHeight()/numRows;

    int x = thisCol*w;
    int y = thisRow*h;

    Rectangle myRect = new Rectangle(x,y,w,h);
    return myRect;
    }

    public void paint(Graphics g)
    {
    g.setColor(Color.gray);
    g.fillRect(0,0,getWidth(), getHeight());
    g.setColor(Color.black);


    Graphics2D g2 = (Graphics2D)g;
    // we'll use Graphics2D for it's "draw" method -
    // neater than the Graphics "drawRect" suppled
    // (which you could also use)

    for (int i = 0;i<numCols;i++)
    for(int j = 0;j<numRows;j++)
    g2.draw(getRect(i,j));

    if (clickControl == true)
    {
    Rectangle r2 = getRect(clickColumnIndex,clickRowIndex);
    g.setColor(Color.red);
    g.fillOval(r2.x,r2.y,r2.width, r2.height);
    }

    if (buttonNumber == 3)
    {
    Rectangle r3 = getRect(clickColumnIndex,clickRowIndex);
    g.setColor(Color.yellow);
    g.fillOval(r3.x,r3.y,r3.width, r3.height);
    }

    }

    }

    }



    could someone please help i need to understand why it is declarred a boolean and why it is firsty tru and then decl;ared false later on please help me .I need to understand this ......

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

    Default Re: JButton on JPanel

    I think you have used the MouseListener to draw the rectangle and circle on clicks is it? if so the clicks and release should be used.if you cick click control becomes true and the painting takes place else it will not.if it is not used u have used repaint() .It starts painting continuously so used false.

Similar Threads

  1. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 07:04 PM
  2. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  3. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM
  4. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM