views:

43

answers:

2

I want to do a very simple thing with java swing and I'm not being able to.

I added a jLabel as it is the only object using java swing that can hold an image (using the icon property)...at least I wasn't able to set an image using other objects.

Now I want to add another jlabel in top of the first one with another image so it can be "clicked" independently from the "background" label. But whenever I try to add it using the graphical editor or by doing jLabel1.add(jLabel2) it doesn't work...it simply sets next to the label1 but not on top of it.

This is in order to do a java application like the Tic Tac Toe game...so I can have the background which are squares (first label) and the others the "X" and "O".

board

This might be the board and I want to put labels on each square so they can be the pieces.

A: 

Well you would probably want to set the label to use a GridLayout. Then you can add other JLabel components to each of the nine cells. When somebody clicks on the label you can then set the Icon to be whatever you want.

camickr
I'm having a hard time setting the jlabel to a gridlayout...is it possible? I know that with jframe is...
out_sider
A JLabel is a Container, the same as a JPanel is a Container. You can set the layout of a JLabel exactly the same way as you set the layout manager for a JPanel. And you then add components to the label exactly the same way you add components to a panel. Read the Swing tutorial on Using Layout Managers: http://java.sun.com/docs/books/tutorial/uiswing/TOC.html, if you need more help.
camickr
I've managed to add the jlabels to the one I wanted..Thx alot for the help...Is there any way to choose what "sub label" of the Grid label will receive the new label? Like index selection? Cause it's 4x4 grid.One more question I was thinking to control the actions when pressing the one of the label would be action listener the jlabel...do you think it would the best idea?I was thinking in the lines of...the user clicks on the label with an X or a Circule and then the destination will trade images with the source...do you think that might be ease and the best choice to do it?Thx for the help
out_sider
When an event is generated you just use the event.getSource() method to determine which label was clicked. Then you can use the setIcon() method to add your "X" or "O". Labels don't support ActionListener, so you need to use a MouseListener. Or if you really want to use an ActionListener you can use JButtons instead of a JLabel. Use the setBorderPainted(false) method and the button will look like a label.
camickr
A: 

You can easily subclass JLabel and override the whole public void paint(Graphics g) with something like:

public void paint(Graphics g)
{
  Graphics2D g2 = (Graphics2D)g;

  // antialiasing, just because it's nicer
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

  g2.drawRect(..); // or instead
  g2.drawImage(..); // if you really want a border as an image

  g2.drawImage(..); // to draw the symbol in the middle


}

Otherwise you can set a border to the JLabel with the BorderFactory (mind about spacings between grid cells) and set just the image in the middle.

To layout elements use a GridLayout, they'll arrange them in a grid, forcing same sizes.

Jack
you need to override paintComponent() when subclassing Swing components
objects