Jump to content
  • Advertisement
Sign in to follow this  
agm_ultimatex

trying to add container to JPanel with gridbaglayout

This topic is 3832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im working on a program that reads lines from a text file, and loads them into a jpanel. This is to help me keep track of things I need to do, such as assignments. I use the gridBagLayout and gridBagConstraints to display the JTextFields. The problem is, when I click add (to add a note) it appears on the top, instead below the last note. It is at the end of the ArrayList. Heres the code for the add method. If you want to see all my source code, I can zip it and email it to you.
public void addNotePanel(String note)
	{
		//create NotePanel and note
		NotePanel notePanel = new NotePanel(note);
	    //note is given its id.
	    Note noteForPanel = notePanel.getPanelNote();
	    noteForPanel.setCounter(notes.size() + 1);
	    
	    //note added to the arraylist
	    notes.add(notePanel);
	    c = null;
	    c = new GridBagConstraints();
	    c.gridy = 0;
		c.gridx = 0;
		
		removeAll();
		
		Iterator<NotePanel> i = notes.iterator();
		while(i.hasNext())
		{
			//puts each one down a position
			c.gridy += 1;
			//each Panel is verticle, same x position
			c.gridx = 0;
			//give bottom padding
			c.insets = new Insets(0,0,5,0);
			c.fill = GridBagConstraints.HORIZONTAL;
			c.anchor = GridBagConstraints.NORTH;
			//allows the NotePanels to span to edge of the window
		    c.weightx = 1.0;
		    
		    add(i.next(), c);
		}
	    repaint();
	}

Share this post


Link to post
Share on other sites
Advertisement
Ive been messing around with the code a bit. I use this code (or similar to) to generate all the fields when the object (called TextFileReader, extends JPanel) is made, and it creates them and adds them to it. I wonder if it the container is then set to a limit on how many it can have?

Share this post


Link to post
Share on other sites
my advise would be to not use the gridbaglayout,
If you can't get away with any of the simpler layout managers just manually position the form components and if you need them to dynamically resize overload the resize method.

Share this post


Link to post
Share on other sites
I got rid of the grid baglayout, im just using flow layout for now. Is there a way to have the elements flow vertically rather than horizontal?

Kind of like this: (however i have the elements on the top now, rather then center, which is what i want)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!