Jump to content

Trouble getting buttons in grid

- - - - -

  • Please log in to reply
1 reply to this topic

#1
ejay8400

ejay8400

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, i got some problems with figuring out how to get my buttons to not expand when i change the size of my window. want them to stick to their size.

here you got my code, sorry for some of it is in Norwegian, but shouldn be a problem understanding it either way.

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;


import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;



public class Oppgave2 extends JFrame{


	

	public Oppgave2(){

		//size og synlighet

  	 this.setVisible(true);

  	 this.setSize(500,500);

  	 //container, henter innhold

  	 Container innhold = this.getContentPane();

  	 //meny øverst

  	 JMenuBar menybar = new JMenuBar();

  	 this.setJMenuBar(menybar);

  	 //lage menyer til menybaren

  	     	//fil

  	 JMenu fil = new JMenu("File");

  	 JMenuItem save = new JMenuItem("save");

  	 fil.add(save);

  	 menybar.add(fil);

  	     	//Statestikk

  	 JMenu statestikk = new JMenu("Statistics");

  	 JMenuItem item = new JMenuItem("Max");

  	statestikk.add(item);

  	 JMenuItem item2 = new JMenuItem("Min");

  	statestikk.add(item2);

  	 JMenuItem item3 = new JMenuItem("Sum");

  	statestikk.add(item3);

  	 menybar.add(statestikk);

  	 //knapper

  	 	// knapper til panel sør

  	 JButton l = new JButton("Land");

  	 JButton å = new JButton("år");

  	 JButton d  = new JButton("deltagelse");

  	 


  	 

  	 	//knapper til panel vest

  	 JButton max = new JButton("max");

  	 JButton min = new JButton("min");

  	 JButton sum = new JButton("sum");

  	String[] patternExamples = {

  	         "2000",

  	         "2001",

  	         "2002",

  	         "2003",

  	};

  	JComboBox patternList = new JComboBox(patternExamples);

  	patternList.setEditable(false);

  	 //tekstområde

  	 JTextArea landtekst = new JTextArea();

  	 JTextArea årtekst = new JTextArea();

  	 JTextArea delttekst = new JTextArea();

  	 //tekstfelt

  	 JTextField tekst = new JTextField();

  	 //labeler

  	 JLabel country = new JLabel("Land");

  	 JLabel år = new JLabel("år");

  	 JLabel deltagelse = new JLabel("deltagelse");

  	 //paneler

  	 JPanel panel = new JPanel();

  	 JPanel panelvest = new JPanel();

   	//lager nett

  	 GridLayout nett = new GridLayout(3,1);

  	 GridLayout nettvest = new GridLayout(3,1);

  	 //legger til i paneler

  	 //panel med knapper sør

  	 panel.setLayout(nett);

  	 panel.add(country);

  	 panel.add(landtekst);

  	 panel.add(l);

  	 panel.add(år);

	 panel.add(patternList);

  	 panel.add(å);

  	 panel.add(deltagelse);

  	panel.add(delttekst);

  	 panel.add(d);

  	 //panel med knapper øst

  	 panelvest.setLayout(nettvest);

  	 panelvest.add(max);

  	 panelvest.add(min);

  	 panelvest.add(sum);

  	 //legger til i innhold

  	 innhold.add(panel,BorderLayout.SOUTH);

   	innhold.add(tekst,BorderLayout.CENTER);

   	innhold.add(panelvest,BorderLayout.WEST);


}

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
As you've noticed everything in a BorderLayout will stretch according to the available space.
So your 3 buttons are in a JPanel with a gridlayout, which is in the WEST. problem is the JPanel will stretch because it's in the west as will the buttons. (Mainly because of that's how GridLayout works, gridlayout stretches everything it contains as far as possible both horizontal and vertical).

Solution is to put an extra jpanel in the WEST which is allowed to stretch, and in that extra jpanel you add the jpanel with 3 buttons.

Code probably makes it more clear than trying to explain it :P

[strike]innhold.add(panelvest,BorderLayout.WEST);[/strike]
JPanel westPanel = new JPanel();
westPanel.add(panelvest);
innhold.add(westPanel,BorderLayout.WEST);
Now the westPanel will stretch, but because it has a FlowLayout (JPanel's default) it won't cause its children (panelvest) to stretch along with it.

Another possible solution is changing the layout of panelvest to boxlayout instead of gridlayout:
panelvest.setLayout(new BoxLayout(panelvest, BoxLayout.Y_AXIS));
Same effect but 1 small difference. Buttons aren't equally wide now, they are as wide as they are needed to be (to fit the text), and nothing more.
With the gridLayout the biggest button will be as big as it needs to be, stretching the gridLayout, and as I said

Quote

gridlayout stretches everything as far as possibly both horizontal and vertical
This will cause the "smaller" buttons to stretch as well, so they become equally long.

In my opinion the first solution looks prettier with them being equally big.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users