How to make it so it fit window
Hey!
What I'm trying to do is do a program where you can write your name, date and post in a text field. I have done that but
they are in horizontal (row) so it wont fit in my window, who has to be this.setSize(400, 400);
So I'm trying to get it in column (vertical), anyone can help me with the code? Would be thankful :)
Like this :
Name : Textfield
Date : Textfield
Post: Textfield
Here is my code (its shorten)
Code:
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Vindu extends JFrame{
JTextArea jta = new JTextArea();
JTextField jtf = new JTextField(20);
String alt ="";
JTextField jtaInnlegg = new JTextField();//lag tekstområde
ArrayList<String> al;
public Vindu(){
al= new ArrayList<String>();
Container cp = this.getContentPane();//Hent innholdsfortegnelese
cp.add(jta,BorderLayout.SOUTH);
JPanel jpWest = new JPanel();//lag panel
GridLayout gl = new GridLayout(3,1);
jpWest.setLayout(gl);
JLabel jlNavn = new JLabel("Name");
JLabel jlDato = new JLabel("Date");
JLabel jlInnlegg = new JLabel("Post");
JTextField jtfNavn = new JTextField(20);
JTextField jtfDato = new JTextField(20);
JTextField jtfInlegg = new JTextField(20);
final JButton jb = new JButton("Lagre innlegg");//lag knapp
jpWest.add(jlName);
jpWest.add(jlDate);
jpWest.add(jlIPost);
jpWest.add(jtfName);
jpWest.add(jtfDate);
//Component jtfDate;
jpWest.add(jtaPost);
jpWest.add(jb);
//legger panel west i et nytt panel som legge si west
JPanel jpInner = new JPanel();
jpInner.add(jpWest);
cp.add(jpInner,BorderLayout.WEST);//legg panel i west
this.setTitle("Blogg program");
this.setLocation(150, 150);
this.setSize(400, 400);
this.setVisible(true);
}
public static void main(String[] args) {
new Vindu();
}
}
Re: How to make it so it fit window
Please use "code" tags when posting code. Put [code] at the start of the and [/code] at the end: that way the formatting is preserved when it appears here. It is also a good idea to use spaces rather than tabs to indent code as tabs can tend to get rendered rather wide.
Re: How to make it so it fit window
Re: How to make it so it fit window
Quote:
Like this :
Name : Textfield
Date : Textfield
Post: Textfield
That's lots of rows and two columns, so the layout constructor would be "GridLayout gl = new GridLayout(0,2);"
The labels and other components get laid out (left to right, top to bottom) in the order you add them. So it's quite important to add them label/component, label/component, etc. Something like:
Code:
JPanel jpWest = new JPanel();//lag panel
GridLayout gl = new GridLayout(0,2);
jpWest.setLayout(gl);
JLabel jlNavn = new JLabel("Name");
JTextField jtfNavn = new JTextField(20);
jpWest.add(jlNavn);
jpWest.add(jtfNavn);
JLabel jlDato = new JLabel("Date");
JTextField jtfDato = new JTextField(20);
jpWest.add(jlDato);
jpWest.add(jtfDato);
JLabel jlInnlegg = new JLabel("Post");
jpWest.add(jlInnlegg);
/* ??? */JTextField jtfInlegg = new JTextField(20);
jpWest.add(jtaInnlegg);
final JButton jb = new JButton("Lagre innlegg");//lag knapp
jpWest.add(jb);
There are lots of details at Laying Out Components Within a Container lesson in Oracle's Tutorial.
-----
I'll move this whole thread to the Awt/Swing forum where it belongs.
Re: How to make it so it fit window
Thank you it looks much better! But the text field don't still fit the window, there is too much space between the labels and text field.
It looks like this :
Name -Space- text field
Date -Space- text field
Post -Space- text field
Also the text area is not showing completely in the window, when I press the button "Lagre innlegg" the information I put on Post text field get printed out and the text area is enlarging, but I would like to have so it showing the entire text area from the beginning.
I have had this problem with other assignments too and I never figured out how to do it
Re: How to make it so it fit window
I can't add much to what's explained in the Tutorial lesson already linked to. You can do a lot by nesting layouts: especially BorderLayout and BoxLayout which are both rather simple. Nesting them gives you the flexibility that GridLayout lacks.
Re: How to make it so it fit window
Oh I didn't see your link until now, thanks!