Noobie Java Question

Status
Not open for further replies.

gaev

Wise, Aged Ars Veteran
145
Well, you certainly _can_ position components on an object via coordinates. Unfortunately, to do this you would have to create your own layout class that implements the LayoutManager interface. Unfortunately this is more of a pain than it is often worth becuase many times one of the built-in java layouts works just fine.

Check this out: JAVA Layout Managers

I personally think one of the easiest ones to use is the BoxLayout.

enjoy!
 

OneFootsTheSame

Smack-Fu Master, in training
70
Just be careful using this with some swing components.

Really? Which ones don't work? I can't say as I've done this much with swing (naturally, I use LMs when I do GUI programming), but I'll whip out some quick things to test stuff and don't remember having a problem with them. What is the dependancy the component has on the LM?
 

gaev

Wise, Aged Ars Veteran
145
Really? Which ones don't work? I can't say as I've done this much with swing (naturally, I use LMs when I do GUI programming), but I'll whip out some quick things to test stuff and don't remember having a problem with them. What is the dependancy the component has on the LM?

This doesn't work:

code:<HR>
Code:
import java.awt.*;
import javax.swing.*;<P>class TestAbs
{
	public static void main(String[] args) 
	{
		JFrame j = new JFrame();
		j.getContentPane().setLayout(null);
		j.setSize(300,300);
		JButton button = new JButton("Hello");
		j.getContentPane().add(button);
		button.setLocation(new Point(10, 10));
		j.setVisible(true);
	}
}
 

OneFootsTheSame

Smack-Fu Master, in training
70
gaev wrote:

This doesn't work: ...

Uh, yeah. Forgot to note that you need to set the component size. This works.

code:<HR>
Code:
import java.awt.*;
import javax.swing.*;<P>class test
{
    public static void main(String[] args)
    {
        JFrame j = new JFrame();
        j.getContentPane().setLayout(null);
        j.setSize(300,300);
        JButton button = new JButton("Hello");
        button.setSize(80, 30);
        j.getContentPane().add(button);
        button.setLocation(new Point(10, 10));
        j.setVisible(true);
    }
}

Dave:

While you can layout components with absolute positioning, as noted by others you *really* ought to get friendly with layout managers. It'll be better for you in the long run to not put it off too long.
 

Dave88

Ars Praefectus
3,583
Subscriptor
Okay, the setLayout(null) thingy didnt work. Why not? And although I'm sure you're right and layout managers are the best way to go, I am only doing Java as part of a university course, personally I don't like the language much. give me C++ any day. But back to Java, how do I get absolute positioning (I really don't have the time to learn all about flow managers or anything and the given assignment is far too simple to spend any great amount of time on - in fact if I can figure out this positiong absolutely lark, I'll be done in 20 mins!)

So come on guys, suggestions pls, why won't it let me disable the flow layout controller and place in exact locations? I just get a blank screen as if it hadn't compiled, but VJ++ says it compiled fine. help :(
 

OneFootsTheSame

Smack-Fu Master, in training
70
I just ran this, and it works. If it doesn't run for you, then try wiht the jdk not VJ++.

code:<HR>
Code:
import java.applet.*;
import java.awt.*;
public class appletTest extends Applet
{
    public void init()
    {
        Button myButton = new Button("hi");
        myButton.setSize(60, 40);
        setLayout(null);
        add(myButton);
        myButton.setLocation(10, 10);    
    }
}
 
Status
Not open for further replies.