Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default 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)

    Java 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();
    			
    		}
    
    
    	}
    Last edited by pbrockway2; 02-17-2012 at 11:32 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default 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.

  3. #3
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: How to make it so it fit window

    Ok will do next time (Y)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: How to make it so it fit window

    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:

    Java 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.

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default 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

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default 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.

  7. #7
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: How to make it so it fit window

    Oh I didn't see your link until now, thanks!

Similar Threads

  1. Noob question: How to make smooth window changes?
    By fr33d4n in forum AWT / Swing
    Replies: 1
    Last Post: 08-19-2011, 09:55 PM
  2. Hide taskbar and make window full screen
    By tmparisi in forum New To Java
    Replies: 6
    Last Post: 05-18-2011, 05:48 PM
  3. Need to Understand code to make window centralized
    By vicky15 in forum New To Java
    Replies: 3
    Last Post: 05-14-2011, 01:49 PM
  4. How do you make another window appear
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-22-2010, 08:05 PM
  5. how to make window active and textfield focused
    By whwillisiv in forum New To Java
    Replies: 0
    Last Post: 04-28-2009, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •