While I am familiar java but i am basically new to creating a GUI using java. I need to make it so when a user clicks on a button another window pops up displaying various statistics about that product. The thing is the amount of information I need to display can't fit in one window vertically so I am trying to use a JScrollPane but the scroll bars dont show up. Oddly when fooling around and accidentally making everything appear on one line a horizontal scroll bar showed up but i have yet to get a vertical scroll bar to appear. Here is my code that uses the JScrollPane:

JFrame popupFrame=new JFrame();
                String title=((JButton)event.getSource()).getText();
                popupFrame.setTitle(title+" Statistics");
                popupFrame.setSize(500,700);
                JPanel popupPanel =new JPanel();
                popupPanel.setLayout(null);
                popupPanel=createLabels(popupPanel);
                JScrollPane statPane= new JScrollPane(popupPanel);
                popupFrame.add(statPane);
                popupFrame.setLocation(500,100);
                  popupFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                return popupFrame;

I have looked online and I felt like I was doing it right but as i said im pretty new to this stuff.

Recommended Answers

All 9 Replies

Use setPreferredSize method to set size (height/width) of panel.

Use setPreferredSize method to set size (height/width) of panel.

I tried this but still the same problem

Ok the above i think had ended up working but I never posted back here, however i was unable to run the program for a while and now it no longer works. Here is my code:

JFrame popupFrame=new JFrame();
                String title=((JButton)event.getSource()).getText();
                popupFrame.setTitle(title+" Statistics");
                popupFrame.setSize(650,750);
                JPanel popupPanel =new JPanel();
                popupPanel.setLayout(null);
                popupPanel.setPreferredSize(new Dimension(400,600));
                popupPanel=createLabels(popupPanel, host);
                //popupPanel.setPreferredSize(new  Dimension(400,600)); //check to see if vertical size needs change
                JScrollPane statPane= new JScrollPane(popupPanel);
                //statPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                popupFrame.add(statPane);
                popupFrame.setLocation(500,50);
                popupFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                return popupFrame;

Even when i uncomment the vertical scrollbar policy the scrollbar will appear but it wont actually let me scroll

popupPanel.setLayout(null);

^^ Looks like a bad idea although the layout manager might use a default layout as a result of you doing that. Although it was already using a default layout so.. why?

You also might want to try calling the pack() method on the popupPanel. Perhaps setVisible as well. And post all of your Java classes, I will run it and help you out.

Unfortuantely you wont be able to run my code im an intern and helping write a program and I am not allowed to post most of my code which you would need to run. When i get in tomorrow I will try your suggestions

I tried removing the setLayout(null) and it completely messed up the display. Also you cant use pack on a panel only a frame which tried but that didnt work. Im a beginner at swing and even newer to layouts so the way i had it was the popupFrame displays a bunch of statistics each one on its own line. I was using setBounds to tell it where to be positioned, increasing a variable each time to make the next stat display lower down. Im sure there is a better way to do this, I just dont know how

You are not really telling us what is happening exactly that makes it look messed up....

Additionally, you can use a null layout in your JPanel, but you will have to use setLocation(int x, int y) and setSize(int x, int y) on everything you add to that JPanel, otherwise they simply wont show up. Using setPreferredSize() on a component with null layout manager wont really do anything. You have to use setSize(). Also, you want to set the size of the JScrollPane and not the (the outer most Container) and not the component in the JScrollPane like you did.

Generally, when I deal with JFrames that open up, I make them into a new Class which extends JFrame and either make it a nested class if theres a lot of information you share between classes, or an outer class if you are able to pass the information its constructor.
When its time to open the new window, you would do: new StatFrame(info); which disposes on close. I find this to be a much cleaner way to handle JFrames.

thx for the help but yesterday was my last day at the internship so I can no longer try anything and i never got it to work. I had someone else who knew java try to help and he couldnt figure it out either.

Your main problem was probably setting the layout to null. You're supposed to use Layout Managers, which dictate how Components (such as buttons, labels, and text boxes, etc) get positioned within your Containers (JFrame and JPanel are Containers). If you set the layout manager to null, that probably means you weren't using one, which means that nothing was controlling how things were positioned. Anyway, if you're still interested in learning how to make GUIs with Swing, don't hesitate to ask for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.