Results 1 to 3 of 3
  1. #1
    splen is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default Adding to GridBagLayout

    Hello community,
    got a problem with adding my Components via GridBagLayout.
    If I create a new JButton while adding it to the container everything works fine. If I initialize a global JButton field before, then try to add it to the container, it will not work. Im very confused because i cannot see a difference between this methods.

    Java Code:
    JLabel lblHeadline;
    lblHeadline= new JLabel(title);
    add(lblHeadline, c);
    Java Code:
    add(new JLabel(title), c);
    Here is where the problem exactly appears in my classes.

    Java Code:
    public class SpieltagGUI extends JFrame	{
    	private Client cl;
    	private Spielplan plan;
    	private Tabellenplatz[] platzierungen;
    	private String teamName;
    	public [B]JLabel lblHeadline[/B], lblStatus, lblPaarung, lblWürfel;
    	public SpieltagGUI(Spielplan plan, Tabellenplatz[] platzierungen, String teamName, Client cl) {
    		String title="Spieltag "+plan.n+" : "+teamName;
    		setLayout(new GridBagLayout());
    		MyGridBagConstraints c= new MyGridBagConstraints();
    		// LABELS
    		[B]lblHeadline= new JLabel(title);[/B]
    		c.setValues(0, 0, 3, 1, 1.00, 1.00, 3);
    		[B]add(lblHeadline, c);[/B]
    Java Code:
    public class MyGridBagConstraints extends GridBagConstraints {
    	public void setValues( 
    			int x, int y, 
                int width, int height, 
                double weightx, double weighty, int vfill ) 
    		switch (vfill) {
                    case 0: this.fill = GridBagConstraints.NONE; break;
    		case 1: this.fill = GridBagConstraints.HORIZONTAL; break;
    		case 2: this.fill = GridBagConstraints.VERTICAL; break;
    		default: this.fill= GridBagConstraints.BOTH; break;
    		this.gridx = x; this.gridy = y; 
    		this.gridwidth = width; this.gridheight = height; 
    		this.weightx = weightx; this.weighty = weighty; 
    Sorry for maybe not the best english.
    Thanks for any advices!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates your problem. not all your code, but not uncompilable snippets either.


  3. #3
    splen is offline Member
    Join Date
    Oct 2010
    Rep Power


    When i try the same thing in a small GUI-Testclass, it works fine.
    Thought maybe in the code posted, there is a strange mistake anywhere.

    My whole project is too huge for posting. Even this small piece of code was shortened to be
    logical to you. If you dont see why it shouldn't work in here, its okay.

    Thanks for your info anyways!

Similar Threads

  1. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 11:13 PM
  2. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 09:54 PM
  3. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 05:14 AM
  4. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 09:54 PM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 PM

Posting Permissions

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