-
Positioning components?
Im trying to create and layout controls
dynamically but i can't figure out how
to set the x and y alignments.
I tried using setalignmentX and using
setPosition(Point); Any ideas?
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Layout{
public static void main(String [] args){
int width = 50;
int space = 10;
float x = 0;
float y = 0;
JFrame jf = new JFrame("Hello");
Dimension d;
Container c = jf.getContentPane();
c.setLayout(null);
jf.addWindowListener(new WindowMonitor());
jf.setSize(600,450);
jf.setVisible(true);
JButton[] jb = new JButton[20];
for(int i = 0; i < jb.length; ++i){
jb[i] = new JButton(new Integer(i).toString());
d = new Dimension(50,50);
jb[i].setSize(d);
}
for(int i = 0; i < jb.length; ++i){
if(x > jf.getWidth() - (width + space)){
x = 10;
y = 60;
jb.setalignmentX = x;
jb.setalignmentY = y;
c.add(jb[i]);
}else{x += (width + space);
jb.setalignmentX = x;
c.add(jb[i]);
}
}
}
}
class WindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
-
have you tried setBounds?
I'm quite interested in this as I haven't had a chance of playing with the layout manager set to null.
DO keep us updated!
-
Here you hav an example:
jScrollPane2.setVerticalScrollBarPolicy
(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane2.setBorder(BorderFactory.createTitledBorder("Tekst"));
jScrollPane2.setBounds(new Rectangle(0, 108, 634, 156));
jScrollPane2.getViewport().add(jTextArea2, null);
this.getContentPane().add(jScrollPane2, null);
jTextArea2.setWrapStyleWord(true);
jTextArea2.setLineWrap(true);
jTextArea2.setBorder(BorderFactory.createLineBorder(Color.black, 1));
-
Thanks guys :) The methods worked good. I have
the JFrame set to a certain width which gives the
right most JButtons a nice space from the edge of
the frame. But i have to work on the line
" if(x > (jf.getWidth() - (width + space))){"
a bit so i can have consistent space from the
edge of the frame regardless of the JFrame size.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Layout{
public static void main(String [] args){
int height = 50;
int width = 50;
int space = 10;
int x = 0;
int y = 10;
JFrame jf = new JFrame("Hello");
Container c = jf.getContentPane();
c.setLayout(null);
jf.addWindowListener(new WindowMonitor());
jf.setSize(560,400);
jf.setResizable(false);
jf.setVisible(true);
JButton[] jb = new JButton[54];
for(int i = 0; i < jb.length; ++i){
jb[i] = new JButton(new Integer(i).toString());
jb[i].setSize(width, height);
}
for(int i = 0; i < jb.length; ++i){
if(x == 0){
x = 10;
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
continue;
}
x += (width + space);
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
if(x > (jf.getWidth() - (width + space))){
x = 10;
y += (height + space);
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
}
}
}
}
class WindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}