Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > setLocation problem with swing!!!

Reply
Thread Tools

setLocation problem with swing!!!

 
 
asd
Guest
Posts: n/a
 
      02-07-2007
Hi all,

I am doing some kind of home work using swing. Unfortunately I am having
problems with setLocation() function. I know it is a bad idea to use
setPosition and I know I should use Layout manager instead of doing that.
Still I would like to do it with setLocation.

Here is my problem: I am trying to position a button on a given pixel on the
screen. To do that I am using setLocation function of the JButton.

Here is what I did:
Created a JFrame,
Created a JPanel, and added it to frame.
Created a JButton, and added it to panel.
Called setLocation function of the button. It is not working. The button is
just created in the center of the window, where layout manager places it.

What should I do to get it work?

I would really appreaciate it if you could help me with my problem.
Sincerely yours.



 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-07-2007
asd wrote:
> Hi all,
>
> I am doing some kind of home work using swing. Unfortunately I am having
> problems with setLocation() function. I know it is a bad idea to use
> setPosition and I know I should use Layout manager instead of doing that.
> Still I would like to do it with setLocation.
>
> Here is my problem: I am trying to position a button on a given pixel on the
> screen. To do that I am using setLocation function of the JButton.
>
> Here is what I did:
> Created a JFrame,
> Created a JPanel, and added it to frame.
> Created a JButton, and added it to panel.
> Called setLocation function of the button. It is not working. The button is
> just created in the center of the window, where layout manager places it.
>
> What should I do to get it work?
>
> I would really appreaciate it if you could help me with my problem.
> Sincerely yours.
>
>


From the docs:

JPanel

public JPanel()

Creates a new JPanel with a double buffer and a flow layout.

So it puts the button where it wants to. Just create the JPanel with a
null layout manager. Then you can put it anywhere you want. Of course
if that's all you do you won't be able to see the JButton because it
doesn't have a size. So call setSize(button.getPreferredSize()) to make
it appear correctly, more or less.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
asd
Guest
Posts: n/a
 
      02-07-2007

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:Fvbyh.251073$(E-Mail Removed)...
> asd wrote:
>> Hi all,
>>
>> I am doing some kind of home work using swing. Unfortunately I am having
>> problems with setLocation() function. I know it is a bad idea to use
>> setPosition and I know I should use Layout manager instead of doing that.
>> Still I would like to do it with setLocation.
>>
>> Here is my problem: I am trying to position a button on a given pixel on
>> the screen. To do that I am using setLocation function of the JButton.
>>
>> Here is what I did:
>> Created a JFrame,
>> Created a JPanel, and added it to frame.
>> Created a JButton, and added it to panel.
>> Called setLocation function of the button. It is not working. The button
>> is just created in the center of the window, where layout manager places
>> it.
>>
>> What should I do to get it work?
>>
>> I would really appreaciate it if you could help me with my problem.
>> Sincerely yours.
>>
>>

>
> From the docs:
>
> JPanel
>
> public JPanel()
>
> Creates a new JPanel with a double buffer and a flow layout.
>
> So it puts the button where it wants to. Just create the JPanel with a
> null layout manager. Then you can put it anywhere you want. Of course if
> that's all you do you won't be able to see the JButton because it doesn't
> have a size. So call setSize(button.getPreferredSize()) to make it appear
> correctly, more or less.
>
> --
>
> Knute Johnson
> email s/nospam/knute/



Hello Knute,

I appreciate your help. It worked.

I tried setting layout manager to null before posting here. I was unable to
locate the button on the frame.
Thanx for the detailed information about the size of the button.
Sincerely.


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-07-2007
asd wrote:
> "Knute Johnson" <(E-Mail Removed)> wrote in message
> news:Fvbyh.251073$(E-Mail Removed)...
>> asd wrote:
>>> Hi all,
>>>
>>> I am doing some kind of home work using swing. Unfortunately I am having
>>> problems with setLocation() function. I know it is a bad idea to use
>>> setPosition and I know I should use Layout manager instead of doing that.
>>> Still I would like to do it with setLocation.
>>>
>>> Here is my problem: I am trying to position a button on a given pixel on
>>> the screen. To do that I am using setLocation function of the JButton.
>>>
>>> Here is what I did:
>>> Created a JFrame,
>>> Created a JPanel, and added it to frame.
>>> Created a JButton, and added it to panel.
>>> Called setLocation function of the button. It is not working. The button
>>> is just created in the center of the window, where layout manager places
>>> it.
>>>
>>> What should I do to get it work?
>>>
>>> I would really appreaciate it if you could help me with my problem.
>>> Sincerely yours.
>>>
>>>

>> From the docs:
>>
>> JPanel
>>
>> public JPanel()
>>
>> Creates a new JPanel with a double buffer and a flow layout.
>>
>> So it puts the button where it wants to. Just create the JPanel with a
>> null layout manager. Then you can put it anywhere you want. Of course if
>> that's all you do you won't be able to see the JButton because it doesn't
>> have a size. So call setSize(button.getPreferredSize()) to make it appear
>> correctly, more or less.
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/

>
>
> Hello Knute,
>
> I appreciate your help. It worked.
>
> I tried setting layout manager to null before posting here. I was unable to
> locate the button on the frame.
> Thanx for the detailed information about the size of the button.
> Sincerely.
>
>


Those suckers are hard to find when their size is 0x0 .

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving objects on GlassPane ignores setLocation() Icarus Java 5 08-03-2008 03:51 PM
setlocation of a JButton on a drawed JFrame middaycrisis Java 0 11-11-2007 07:46 PM
setLocation (AWT) Jack Java 2 11-05-2005 07:03 PM
setLocation does not work Jack Java 9 11-05-2005 04:05 AM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments