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

    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.

    DOES NOT WORK
    Java Code:
    JLabel lblHeadline;
    (...)
    lblHeadline= new JLabel(title);
    add(lblHeadline, c);
    WORKS
    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) {
    		super();
    		String title="Spieltag "+plan.n+" : "+teamName;
    		setTitle(title);
    		this.cl=cl;
    		this.plan=plan;
    		this.platzierungen=platzierungen;
    		this.teamName=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!
    splen

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    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.

    db

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

    Default

    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, 10:13 PM
  2. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 08:54 PM
  3. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  4. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08: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
  •