MyJButton is not working with Gridlayout

MyJButtons squash up into the top left corner of my GUI

Page 1 of 1

5 Replies - 1476 Views - Last Post: 16 September 2009 - 02:24 PM Rate Topic: -----

#1 kezkankrayon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-October 08

MyJButton is not working with Gridlayout

Post icon  Posted 15 September 2009 - 05:26 PM

Hello,
I'm having trouble with a implementing a gridlayout. For some reason all of my MyJButtons squash up into the top left corner of my GUI. Oddly enough button clicks are still being detected in the lower right and are returning seemingly correct x and y values. I have no idea why this is happenening and any help will be much appreciated.

The reason I've extended the JButton class is so that an x and y value can be returned to assist in locating the button in a 2d array.

This is a screen shot of the afflicted GUI
Posted Image

This is code for the GUI
GuiDriver.java
public class GuiDriver
{
	public static void main(String[] args)
	{
		TestGui gui = new TestGui();
	}

}



MyJButton.java
import javax.swing.JButton;

public class MyJButton extends JButton {
	private int x;
	private int y;

	public MyJButton(int x, int y) {
		super(x+","+y); //set name as co-ordinates
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

}




TestGui.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestGui extends JFrame
{
	JPanel basePanel = new JPanel();
	MyJButton[][] button = new MyJButton[9][9];

	public TestGui()
	{
		basePanel.setLayout(new GridLayout(9, 9));
		for (int x = 0; x < 9; x++) 
		{
			for (int y = 0; y < 9; y++) 
			{
				button[x][y] = new MyJButton(x,y);
				button[x][y].addMouseListener(new MyButtonListener());
				basePanel.add(button[x][y]);
			}
		}
		
		add(basePanel);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	class MyButtonListener implements MouseListener {
		
		public void mouseClicked(MouseEvent e)
		{	
			Object obj = e.getSource();
			if (obj instanceof MyJButton) 
			{
				MyJButton clicked = (MyJButton) obj;
				if (e.getButton() == 1)
				{
					System.out.println(clicked.getX()+","+clicked.getY()+":left");
				}
				else if (e.getButton() == 3) 
				{
					System.out.println(clicked.getX()+","+clicked.getY()+":right");
				}
			}
		}

		public void mouseEntered(MouseEvent e)
		{

		}

		public void mouseExited(MouseEvent e)
		{

		}

		public void mousePressed(MouseEvent e)
		{

		}

		public void mouseReleased(MouseEvent e)
		{

		}
	}
}


This post has been edited by kezkankrayon: 16 September 2009 - 02:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: MyJButton is not working with Gridlayout

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MyJButton is not working with Gridlayout

Posted 15 September 2009 - 06:54 PM

This is a gorgeous, juicy, challenging problem
the way I rally like them :^: :^: :^:
Your Buttons are not stack one over the others they are just not visible... when you pass the mouse in the panel they appears by magic.

Obviously the call to the pack() method is useless and you have a a call setSize() missing
I don't see why you have a MouseListener over JButton, they were made to react to click with their ActionListener

I re-arrange a bit your code and add the main() method that you haven't provided

This is your new code....

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestGui extends JFrame
{
	JPanel basePanel;
	MyJButton[][] button = new MyJButton[9][9];

	public TestGui()
	{
		super("Test GUI");
		setLayout(new BorderLayout());
		basePanel = new JPanel(new GridLayout(9, 9));
		for (int x = 0; x < 9; x++) 
		{
			for (int y = 0; y < 9; y++) 
			{
				button[x][y] = new MyJButton(x,y);
				button[x][y].addMouseListener(new MyButtonListener());
				basePanel.add(button[x][y]);
			}
		}
		
		add(basePanel);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //	   pack();
		setSize(500, 400);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TestGui tg = new TestGui();
				tg.setVisible(true);
			}
		});
	}
	class MyButtonListener implements MouseListener {
		
		public void mouseClicked(MouseEvent e)
		{	
			Object obj = e.getSource();
			if (obj instanceof MyJButton) 
			{
				MyJButton clicked = (MyJButton) obj;
				if (e.getButton() == 1)
				{
					System.out.println(clicked.getX()+","+clicked.getY()+":left");
				}
				else if (e.getButton() == 3) 
				{
					System.out.println(clicked.getX()+","+clicked.getY()+":right");
				}
			}
		}

		public void mouseEntered(MouseEvent e)
		{

		}

		public void mouseExited(MouseEvent e)
		{

		}

		public void mousePressed(MouseEvent e)
		{

		}

		public void mouseReleased(MouseEvent e)
		{

		}
	}
	
	class MyJButton extends JButton {
		private int x;
		private int y;

		public MyJButton(int x, int y) {
			super(x+","+y); //set name as co-ordinates
			this.x = x;
			this.y = y;
		}

		public int getX() {
			return x;
		}

		public int getY() {
			return y;
		}

	}

}



now why the buttons only show when the mouse passes over them ? :crazy:
I don't think I'll be able to go to bed before finding out why

Stay tune !!!
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MyJButton is not working with Gridlayout

Posted 15 September 2009 - 06:59 PM

?????????????????

Now this version with ActionListener has exactly the same behaviour and works

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestGui extends JFrame
{
	JPanel basePanel;
	MyJButton[][] button = new MyJButton[9][9];

	public TestGui()
	{
		super("Test GUI");
		setLayout(new BorderLayout());
		basePanel = new JPanel(new GridLayout(9, 9));
		for (int x = 0; x < 9; x++) 
		{
			for (int y = 0; y < 9; y++) 
			{
				button[x][y] = new MyJButton(x,y);
				basePanel.add(button[x][y]);
			}
		}
		
		add(basePanel);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 400);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TestGui tg = new TestGui();
				tg.setVisible(true);
			}
		});
	}
	
	class MyJButton extends JButton implements ActionListener{
		private int x;
		private int y;

		public MyJButton(int x, int y) {
			super(x+","+y); //set name as co-ordinates
			this.x = x;
			this.y = y;
			addActionListener(this);
		}

		public void actionPerformed(ActionEvent e) {
	System.out.println("Button X=" + x + "  Y=" + y + " was clicked");
			
		 }
	}
}



Now the question ? Why the other version didn't worked ?
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MyJButton is not working with Gridlayout

Posted 15 September 2009 - 07:14 PM

Now... even more curious
Your desired behaviour with the left and right click... with JButton thow

What the heck is wrong with MyButton ?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestGui extends JFrame
{
	JPanel basePanel;
 //   MyJButton[][] button = new MyJButton[9][9];
	JButton[][] button = new JButton[9][9];
	public TestGui()
	{
		super("Test GUI");
		setLayout(new BorderLayout());
		basePanel = new JPanel(new GridLayout(9, 9));
		for (int x = 0; x < 9; x++) 
		{
			for (int y = 0; y < 9; y++) 
			{
 //			   button[x][y] = new MyJButton(x,y);
				  button[x][y] = new JButton(x + "," + y);
				  button[x][y].addMouseListener(new MyButtonListener());
				basePanel.add(button[x][y]);
			}
		}
		
		add(basePanel);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//	   pack();
		setSize(500, 400);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TestGui tg = new TestGui();
				tg.setVisible(true);
			}
		});
	}
	
   
	class MyButtonListener implements MouseListener {
		
		public void mouseClicked(MouseEvent e)
		{	
			Object obj = e.getSource();
			for(int i = 0; i < 9; i++) {
				for(int j = 0; j < 9; j++) {
					if(obj == button[i][j]) {
						if (e.getButton() == 1)
						{
							System.out.println(i + "," + j + ":left");
						}
						else if (e.getButton() == 3) 
						{
							System.out.println(i + "," + j + ":right");
						}
						return;
					}
				}
			}
		}

		public void mouseEntered(MouseEvent e) {
		}
		public void mouseExited(MouseEvent e) {
		}
		public void mousePressed(MouseEvent e) {
		}
		public void mouseReleased(MouseEvent e) {
		}
	}
  }


Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MyJButton is not working with Gridlayout

Posted 15 September 2009 - 07:24 PM

Got it !!! :D

The getX() and getY() methods of your class MyButton overload the getX() and getY() JComponent methods that return the position of the JComponent in pixels in the Panel

So when the GUI was trying to draw your Button it was drawing them at pixels X 0 to 9 and pixel Y 0 to 9

rename your methods getX() and getY() for
getx() and gety() or myButtonGetX() and myButtonGetY() and your original code should be working :D

ouf

Thanks to Eclipse that showed me that getX() and getY() where overloading their father methods

good night

This post has been edited by pbl: 15 September 2009 - 08:02 PM

Was This Post Helpful? 1
  • +
  • -

#6 kezkankrayon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-October 08

Re: MyJButton is not working with Gridlayout

Posted 16 September 2009 - 02:24 PM

View Postpbl, on 15 Sep, 2009 - 06:24 PM, said:

The getX() and getY() methods of your class MyButton overload the getX() and getY() JComponent methods that return the position of the JComponent in pixels in the Panel

So when the GUI was trying to draw your Button it was drawing them at pixels X 0 to 9 and pixel Y 0 to 9

rename your methods getX() and getY() for
getx() and gety() or myButtonGetX() and myButtonGetY() and your original code should be working :D


You're absolutely correct, getX() and getY() in MyButton overload the getX() and getY() JComponent methods. Changing the method names worked. Well done :^:

Posted Image

This post has been edited by kezkankrayon: 16 September 2009 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1