0

So I have a program that at the start only contains an 'add movie' button at the bottom of the frame. Above it I inserted a scrollpane. I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie. Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).

So I figured I just needed to do this:

private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        MovieForm movie = new MovieForm();
        MovieScrollPane.add(movie);
}                                                   

But nothing new appears.

I tried validate() and repaint(), but so far these don't seem to work. I made the interface in Eclipse btw.

Anyone who can help me? Thanks anyway!

Dragorian
  • 17
  • 3
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See also this [example of dynamically adding labels](http://stackoverflow.com/a/5630271/418556). – Andrew Thompson May 21 '14 at 00:06

1 Answers1

1

MovieScrollPane.add(movie);

Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.

Then, whenever you add a component to a visible GUI the basic code is:

panel.add(...);
panel.revalidate();
panel.repaint();

This makes sure the layout manager is invoked to the preferred size can be recalculated.

Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @Dragorian, yes it does work. Make sure you are using layout managers for your panel. Post your [SSCCE](http://www.sscce.org/) that demonstrates the problem. That is create a simple frame with a button and a scroll pane. Every time you click the button add a JLabel or something simple to the panel. This is for a proof of concept. Once you get this working then you compare the code to see what is different with your real code. – camickr May 21 '14 at 00:06
  • Ok it works now! Turns out I needed to use getViewport().add(...). I thought I already tried that, but apparently I did something wrong. Only problem is the panel fills the whole scrollpane, while it should had room to display other forms next to it. I tried setPreferedSize() on the form, but I can only make the form bigger (and use the scrollbars). Setting it smaller doesn't work – Dragorian May 21 '14 at 10:03
  • @Dragorian, then if it works because of my suggestion to use the `viewport`, the answer should be accepted so people know the problem has been solved. `I tried setPreferedSize() on the form,` - don't use setPreferredSize(). I already said that is the responsibility of the layout manager. ` it should had room to display other forms next to it` - use an appropriate layout manager. – camickr May 21 '14 at 15:34
  • Ok then I'll have to do some research about layout manager. Thanks for the help! – Dragorian May 21 '14 at 16:23