• 14
• 12
• 9
• 10
• 9

# trying to add container to JPanel with gridbaglayout

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

## 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);

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;
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;

}
repaint();
}



##### Share on other sites
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 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.