0

Good day,

I am building an Applet (JApplet to be exact) and I sub divided that into two panels. The top panel is called DisplayPanel which is a custom class that extends JPanel. The bottom panel is called InputPanel which also extends JPanel.

As mentioned above, I can add those two Panel's to the applet and they display fine. The next thing that I would like to do is have the InputPanel be able to hold a random number of JComponent Objects all listed veritcally. This means that the InputPanel should be able to have JButtons, JLabels, JTextFields etc thrown at it. I then want the InputPanel to display some sort of scrolling capability.

The catch is that since these two panels are already inside my applet, I need the InputPanel to stay the same size as it was given when added to the Applet. So for example, if my applet (from the web-browser html code) was given the size 700,700, and then the DisplayPanel was 700 by 350, and the InputPanel was below it with the same dimensions, I want to then be able to add lots of JComponents like buttons, to the InputPanel and the panel would stay 700 x 350 in the same position that it is at, only the panel would have scroll bars if needed.

I've played with many different combinations of JScrollPane, but just cannot get it.

Thank you.

Matthew
  • 3,886
  • 7
  • 47
  • 84
  • 1
    1) Do you have a question? 2) For an example of adding labels dynamically to a `JScrollPane`, see the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556). 3) For better help sooner, post an [SSCCE](http://sscce.org/). 4) Applets are harder to develop & deploy than frame based apps. So much harder that I would develop this code as a hybrid application/applet even if it were only intended to be offered as an applet. – Andrew Thompson Apr 06 '12 at 00:18
  • @AndrewThompson, thanks for the information. I do like the SSCCE, I've never heard of it before. I didn't do a good enough job explaining my problem. Thanks again! I will rephrase and readdress tomorrow. – Matthew Apr 06 '12 at 01:04
  • *"I didn't do a good enough job explaining my problem."* You did not do poorly at it either. I think I understand the basic effect needed for this GUI, but figure that nested layouts in combination with the tips for adding components dynamically in the linked example, should be enough to get you started. If it turns out you're still facing problems, making an SSCCE sure makes it easier for people to help (especially when they can copy/paste the code, compile it, see the problem, tweak the few lines needed to fix it, & post the edited code back as an answer). ;) – Andrew Thompson Apr 06 '12 at 01:12

1 Answers1

0

Good day,

Okay, I was able to answer what I was trying to do, so I thought I would post an answer for future reference. Remember, my goal was to have two panels attached to a JApplet and then the bottom panel should have been able to take in as many JComponents as need be and the size of the Applet wouldn't change, but the bottom panel would then be scrollable with JScrollPane.

This is useful for if in the top Panel you some how select an object and then down below a panel is displayed that you can then control that object that you selected. I am making a network simulator where in the top panel you have a network diagram and you can select a host for example, and then set its IP address. But then if you select a firewall for example, you can determine what ports to block.

I hope this helps and thanks @Andrew Thompson.

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class Applet extends JApplet
{
    JPanel p1;
    JPanel p2;
    JPanel p3;

    JScrollPane s1;



    public Applet()
    {
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();

        s1 = new JScrollPane(p3,s1.VERTICAL_SCROLLBAR_ALWAYS,s1.HORIZONTAL_SCROLLBAR_ALWAYS);


    }

    @Override
    public void init()
    {
        super.init();
        for(int i=0;i<100;i++)
        {
            p3.add(new JButton("Hello"));
            p3.add(new JLabel("blah"));
            p3.add(new JButton("Sup"));
        }

        p1.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
        p2.setPreferredSize(new Dimension(this.getWidth(),(int) (this.getHeight()*0.6667)));

        p3.setLayout(new BoxLayout(p3,BoxLayout.PAGE_AXIS));
        s1.setPreferredSize(new Dimension(this.getWidth(),(int)(this.getHeight()*0.33333)));

        p1.add(p2);
        p1.add(s1);

        this.add(p1);
    }

}
Matthew
  • 3,886
  • 7
  • 47
  • 84