Sign in to follow this  
Yarduza

[java] JLabels

Recommended Posts

I want to put 28 labels in a grid layout, 2 horizonal lines (i dont remember if it is a row or a column) 14 in each line in spaces of 50 px one from the other. i put the following code: for(int til=0;til<14;til++){ amuda[til]=new JLabel(""+til,13+50*til); amuda[til].addMouseListener(this); add(amuda[til]); } for(int til=14;til<28;til++){ amuda[til]=new JLabel(""+til,13+50*(til-14)); amuda[til].addMouseListener(this); add(amuda[til]); } but it gives me some fatal error and the console writes the following error: java.lang.IllegalArgumentException: horizontalAlignment at javax.swing.JLabel.checkHorizontalKey(JLabel.java:590) at javax.swing.JLabel.setHorizontalAlignment(JLabel.java:748) at javax.swing.JLabel.<init>(JLabel.java:140) at javax.swing.JLabel.<init>(JLabel.java:160) at Board.<init>(Board.java:49) at Board.main(Board.java:366) Exception in thread "main" i dont know what to do, i dont know where is the error, i never worked with JLabels before and i would like someone to help me with it and give me some tips about how to put the labels. p.s i need the labels for my backgammon game, i will use it as a place indicator- when i press the mouse button i will get the name of the label (0,1,2...27) and then i will know where i pressed the mouse. if anyone has a better idea than putting labels i will be happy to hear about ass well.

Share this post


Link to post
Share on other sites
You misunderstand the constructor. HorizontalAlignment is not a value for fixing the position it is to say whether the label should be placed to the left, right or center of the label. You should not be trying to set the labels' positions anyway. GridLayout will lay them out properly. To get a decent spacing, you just size the panel that is holding them.

ps. To remember the difference between rows and columns, just thing of Roman architecture. They always built things with big columns. So columns are always vertical and rows are always horizontal.

Share this post


Link to post
Share on other sites
thanks, but i have another problem,
i need every JLabel to be in the exact width of 50px, because otherwise when i drag the frame border, the JLabels grow with it, but the picture doesnt (i paint a picture above the JLabels and i use the JLabels as indicator for place)
so if the labels grow, it changes everything becase the picture stays as it was.
if the picture grew with it it was ok, but it doesnt.
if anyone has a suggestion please tell me.
and if anyone has a tutorial about using the graphics in java i will be more than glad to have it.

[Edited by - Yarduza on July 1, 2005 9:20:07 AM]

Share this post


Link to post
Share on other sites

public class GameBoard extends JPanel {
Dimension size = new Dimension(700, 500);
public Dimension getPreferredSize() {
return size;
}
public Dimension getMinimumSize() {
return size;
}
public Dimension getMaximumSize() {
return size;
}
}


If you subclass JPanel, you can override the sizing methods to ensure that your JPanel remains the same size if the JFrame is resized. Providing of course the JFrame does not get smaller than the required size. This will ensure that all your JLabels are 50 pixels wide.

Share this post


Link to post
Share on other sites
thanks :D
//edit
well, i do extend JPanel but after i put in the code it still remained the same, the JPanel is still resizable, i know that its supposed to work, but it doesnt , i would have paste the whole class here for you to see but i dont know how to do that white thing you wrote your code in it and i dont want it to catch all the screen.

[Edited by - Yarduza on July 2, 2005 2:47:29 AM]

Share this post


Link to post
Share on other sites
to make the code block, use the [ source ] [ /source ] tags without the spaces.

If you simply want to disable the resizing , JFrame has a setResizable(boolean resizable) function.

Otherwise if you really can't find a way to get your components to stay the same size, you can ditch the layout entirely.

call jPanel.setLayout(null);
then for each component use the method setBounds (int x, int y, int width, int height);

It's more work but it gets the job done.

Share this post


Link to post
Share on other sites
thanks, i did it, but i have another problem now, i have a mouseListener, and i want to know on what label i pressed, so i put on the mouseClicked function the next code:
"JLabel lab = (JLabel)e.getSource();"
but when i click it gives me this on the console

"java.lang.ClassCastException
at Board.mouseClicked(Board.java:307)
at java.awt.Component.processMouseEvent(Component.java:5103)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3207)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
"


i have another program which i did the same with buttons and it worked (tic tac toe game), what is the problem here?

Share this post


Link to post
Share on other sites
You must have accidentally added the MouseListener incorrectly. You have clicked on something that is not a JLabel, but you are trying to make it a JLabel. Do you have any other components that use the same MouseListener? Try printing out the e.getSource() to see what it really is.

System.out.println(e.getSource());

or

System.out.println(e.getSource().getClass().getName());

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this