-2

I am still learning java in college but I just would like to know the way or ways to put the JComponent's in specific location and specify their size.

I have seen the methods setLocation() and that sort of methods but I just really wanna know how to put a Component without using those layout managers(flow, grid, border?).

Here is the code I would like to apply that knowledge to: they are two overloaded methods that display the same window.

public void addWelcome()
{
    setWindow();
    MaxDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
    getWindow().setMaximumSize(MaxDimension);
    MinDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
    getWindow().setMinimumSize(MinDimension);
    getWindow().setTitle("Bills");
    getWindow().setSize(WindowWidth,WindowHeight);
    getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
    getWindow().setLayout(new GridLayout(5,1,2,0));
    getWindow().add(getWelcomeLabel());
    getWindow().add(getGreetLabel());
    getWindow().add(getDescriptionLabel());
    getWindow().add(getInstructionLabel());
    getWindow().add(getStartButton());  
    getWindow().pack();
    getWindow().setVisible(true);

}


public void addWelcome(final int Width, final int Height)
{
    setWindow();
    MaxDimension = new Dimension(Width,Height);
    getWindow().setMaximumSize(MaxDimension);
    MinDimension = new Dimension(Width,Height);
    getWindow().setMinimumSize(MinDimension);
    getWindow().setTitle("Bills");
    getWindow().setSize(WindowWidth,WindowHeight);
    getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
    getWindow().setLayout(new GridLayout(5,1));
    getWindow().add(getWelcomeLabel());
    getWindow().add(getGreetLabel());
    getWindow().add(getDescriptionLabel());
    getWindow().add(getInstructionLabel());
    getWindow().add(getStartButton());
    getWindow().setVisible(true);
}

Apologize if code/question are not clear enough.

Thanks in advance.

achingfingers
  • 1,827
  • 5
  • 24
  • 48
Lorenzo_g
  • 311
  • 1
  • 2
  • 8
  • 2
    Why not use layout managers? You will need to learn them sooner or later, so get familiar with them. Oracle has some [good tutorials](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – Paul Samsotha Jan 04 '14 at 16:35
  • Also try and narrow down your question to something more specific regarding your code. – Paul Samsotha Jan 04 '14 at 16:38
  • another question java noob lol. these layouts have like more ways to customize the positioning of components? and am assuming that they are used a lot in industry right? – Lorenzo_g Jan 04 '14 at 16:39
  • 1
    Yes, Layout Manager are very quite often. Different LayouManagers have different specs. i.e. some keep preferred sizes while some don't. So you need to learn wich ones do and which ones don't. For more powerful positionint precision, there's LayoutMangers like `GridBagLayout`. I strongly advise you to run through the entire tutorial. And get comforatable with them – Paul Samsotha Jan 04 '14 at 16:44
  • Thank you so much for your help! I will defenietly read those tutorials. Thanks :) – Lorenzo_g Jan 04 '14 at 17:17
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 04 '14 at 17:46
  • @peeskillet *"You will need to learn them sooner or later"* Disagree with the 'or later'.. GUIs that do not use layouts are a fragile mess. – Andrew Thompson Jan 04 '14 at 17:47

2 Answers2

1

You can use an Absolute Layout, which makes you responsible for positioning everything. Do note that this also makes it nigh impossible to create a layout that will be usable with different screen and window sizes (so yeah, learn to use proper layouts).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

Yes a layout manager is the best solution

An IDE like eclipse or netbeans can help you effortlessly visually position your components with different layouts.

It autogenerates code which you can easily observe and learn.

If you want to code it yourself you have all resources in this link http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

user3041058
  • 1,520
  • 2
  • 12
  • 16