Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » chessboard hw(do I need to create a panel for each square?)
chessboard hw [message #1027858] Wed, 27 March 2013 14:07 Go to next message
uta f is currently offline uta fFriend
Messages: 1
Registered: March 2013
Junior Member
For now I created a frame with gridlayout.
The problem is I can;t seem to figure out how to color the spaces between the squares, and I still need to add a tooltip to each square which should tell the mul of the specific square's row and col (rowxcol).

I don;t think I can do that for the frame so I am thinking about a panel
but the labels I add (as squares) are not shown...

this is my code:

import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Scanner;

public class Program {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ShowGridLayout frame = new ShowGridLayout();			
		frame.setTitle("ShowGridLayout");
		frame.setSize(300, 400);
		frame.setLocationRelativeTo(null); // Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setAlwaysOnTop(true);		    
		frame.setVisible(true); 
	}

}

class ShowGridLayout extends JFrame    
{
	public ShowGridLayout()       
	{ 	

		Scanner scan=new Scanner(System.in);

		System.out.println("Please enter number of rows");		
		int rows=scan.nextInt();

		System.out.println("Please enter number of columns");		
		int cols=scan.nextInt();

		//Set GridLayout,rows,columns,gaps between components:horizontally,vertically

		//setLayout(new GridLayout(rows, cols, 5, 5));	   	   

		JPanel p1=new JPanel(new GridLayout(rows, cols));
		JLabel label;
		for(int i=0;i<rows;i++)
			for(int j=0;j<cols;j++)
			{					
				 label=new JLabel(i+","+j);
				label.setOpaque(true);	

				if((i+j)%2==0)
					label.setBackground(Color.yellow);	    	    
				else
					label.setBackground(Color.blue);	
				
				p1.add(label);
				
			}

				//				JLabel label=new JLabel(i+","+j);
				//				label.setOpaque(true);	
				//
				//				if((i+j)%2==0)
				//					label.setBackground(Color.yellow);	    	    
				//				else
				//					label.setBackground(Color.blue);	
				//
				//				add(label);
			//}


	}
}



in green is what I had previously (which worked) before I added the panel.
But I couldn;t add lines in between.
and yet again I am worried about the tooltip.

But adding the panel does not work and I don;t know why...
Re: chessboard hw [message #1028955 is a reply to message #1027858] Fri, 29 March 2013 00:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This doesn't sound related to Eclipse at all. You're probably better to
try stackoverflow.

On 28/03/2013 11:52 AM, uta f wrote:
> For now I created a frame with gridlayout.
> The problem is I can;t seem to figure out how to color the spaces
> between the squares, and I still need to add a tooltip to each square
> which should tell the mul of the specific square's row and col (rowxcol).
>
> I don;t think I can do that for the frame so I am thinking about a panel
> but the labels I add (as squares) are not shown...
>
> this is my code:
>
> import javax.swing.JLabel;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.border.LineBorder;
>
> import java.awt.Color;
> import java.awt.GridLayout;
> import java.util.Scanner;
>
> public class Program {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> ShowGridLayout frame = new ShowGridLayout();
> frame.setTitle("ShowGridLayout");
> frame.setSize(300, 400);
> frame.setLocationRelativeTo(null); // Center the frame
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setAlwaysOnTop(true); frame.setVisible(true); }
>
> }
>
> class ShowGridLayout extends JFrame {
> public ShowGridLayout() {
>
> Scanner scan=new Scanner(System.in);
>
> System.out.println("Please enter number of rows");
> int rows=scan.nextInt();
>
> System.out.println("Please enter number of columns");
> int cols=scan.nextInt();
>
> //Set GridLayout,rows,columns,gaps between
> components:horizontally,vertically
>
> //setLayout(new GridLayout(rows, cols, 5, 5));
> JPanel p1=new JPanel(new GridLayout(rows, cols));
> JLabel label;
> for(int i=0;i<rows;i++)
> for(int j=0;j<cols;j++)
> {
> label=new JLabel(i+","+j);
> label.setOpaque(true);
>
> if((i+j)%2==0)
> label.setBackground(Color.yellow); else
> label.setBackground(Color.blue);
>
> p1.add(label);
>
> }
>
> // JLabel label=new JLabel(i+","+j);
> // label.setOpaque(true);
> //
> // if((i+j)%2==0)
> // label.setBackground(Color.yellow);
> // else
> // label.setBackground(Color.blue);
> //
> // add(label);
> //}
>
>
> }
> }
>
>
> in green is what I had previously (which worked) before I added the
> panel.
> But I couldn;t add lines in between.
> and yet again I am worried about the tooltip.
>
> But adding the panel does not work and I don;t know why...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: chessboard hw [message #1028967 is a reply to message #1028955] Fri, 29 March 2013 01:15 Go to previous message
Russell Bateman is currently offline Russell BatemanFriend
Messages: 3798
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

There's also a Swing forum at javaranch.com:
http://www.coderanch.com/forums/f-2/GUI

On 03/28/2013 06:49 PM, Ed Merks wrote:
> This doesn't sound related to Eclipse at all. You're probably better to
> try stackoverflow.
>
> On 28/03/2013 11:52 AM, uta f wrote:
>> For now I created a frame with gridlayout.
>> [snip]
Previous Topic:HELP WITH PROGRAM!!!
Next Topic:Popup Menu on a wizard
Goto Forum:
  


Current Time: Thu Apr 25 17:32:32 GMT 2024

Powered by FUDForum. Page generated in 0.06251 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top