I have a JPanel which dynamically allocates buttons in a vertical layout. The problem is when I place this panel inside JScrollPane, the scrollPane appears vertically above my buttons. I'm not sure why this is happening. Here's the code:
public static void GUI ()
{
JFrame frame = new JFrame(GAME_TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400,600));
frame.setLayout(new GridLayout(0,1));
Menu theMenu = new Menu();
theMenu.setLayout(new GridLayout(mSize,0));
theMenu.setOpaque(true);
JScrollPane scroll = new JScrollPane(theMenu,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scroll);
theMenu.createGameButtons(frame);
frame.pack();
frame.setVisible(true);
}
I've tried quite a few things with no success. Also, I'm attaching a link to a screen shot
Christian Hujer answered the question with:
The bug is in the code that is not visible. The bug is in the method createGameButtons. There, the buttons are created and added to the frame instead of adding them to the Menu itself (which I guess is a subclass of JPanel)
Related
I have developed a desktop application. Now in that app I want to add panel with a scrollbar. I am trying using JScrollPane, but its not working.
JPanel paraJPanel = new JPanel();
JScrollPane SP_para_list = new JScrollPane(paraJPanel);
add(SP_para_list).setBounds(10,30,250,350);
This way I am adding scrollbars to panel. But it shows only empty panel with borders. It is not showing components in the panel. Although I have added several labels in it. Is it correct? Is there any other way to add scroll bar to panel.
Thanks in advance
You need to set the PreferredSize for the panel, to make the scrollbar show up, like below.
even you do not set a layout, the panel already has a default layout set.
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel()
{
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 1000);
}
};
panel.add(new JLabel("Test1"));
panel.add(new JLabel("Test2"));
frame.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
frame.setSize(600, 800);
frame.setVisible(true);
}
I currently have multiple jpanels on a jframe. I can add all of the jpanels fine to the jframe but when I try and add a jscrollpane, nothing shows up. Essentially I just want a jscrollbar/pane on the jframe so that I can scroll down as the size of the jpanels goes off screen. This is the main code that I used:
JPanel Jpanel = new JPanel();
JScrollPane Jpane = new JScrollPane();
frame.getContentPane().add(Jpanel);
frame.getContentPane().add(Jpane);
Any help would be appreciated. Thanks
1) code that you posted caused that (JFrame has implemented by default BorderLayout, and there only one JComponent can to fill concrete area or layst added JComponent), only frame.getContentPane().add(Jpane); is possible to dispay on the screen
2) you have to accept that you can put to the JScrollPane only one JComponent
3) JScrollPane works correctly if is there used proper LayoutManager (not AbsoluteLayout) and in the case that Dimmension of JComponent is wider that JViewport from JScrollPane
This has already been answered with the correct way to do it. The only thing I can think of that you might have done wrong (since it's not working), is that you're adding the panels to the jframe.
When you put a panel inside a scrollpane, you need to add the scrollpane to the JFrame, and not add the panel to the JFrame:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
add(scrollPane); // or if you have a panel inside a panel, add the pane to that panel. ie west.add(scrollPane);
You need to add the JPanel to the scroll pane, then add the scrollpane to the frame.
One of the way of doing it is when creating the JScrollPane, with the constructor :
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
What you could do is this:
JPanel Jpanel = new JPanel();
JScrollPane Jpane = new JScrollPane(Jpanel);
frame.setLayout(new BorderLayout());
frame.add(Jpane, BorderLayout.CENTER);
The reason your panels aren't showing is beacause your frame doesn't know where to put them. It needs a layoutmanager.
Here you can find some basic layoutmanagers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
I am developing a small desktop application in NetBeans. On my UI, I place a JPanel and put a single JLabel on it. The content of this JLabel is generated dynamically, so if the content is very large then it goes out of screen.So is there any way in which I can specify a fixed size for the JPanel and of course ScrollBars should appear when the text exceeds the screen size.
You will have to just pass Component reference to the JScrollPane Constructor.
It will work fine. You can definetely use JScrollPane
The following is the sudo example of the JScrollPane for JPanel from my past project. Hope it will be useful for you.
import javax.swing.*;
import java.awt.*;
public class Frame01
{
public static void main(String[] args){
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
JFrame frame = new JFrame("panel demo");
frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
Container c = frame.getContentPane();
panel.setSize(100,100);
panel.setLayout(new GridLayout(1000,1));
for(int i = 0; i<1000;i++)
panel.add(new JLabel("JLabel "+i));
JScrollPane jsp = new JScrollPane(panel);
c.add(jsp);
frame.setSize(100,100);
frame.setVisible(true);
}
});
}
}
Use JScrollPane to contain Your big JPanel.
so in case when the contents are very large it goes out of screen, maybe you have to look for TextComponents as JTextArea or JEditorPane, tutorial contains example including basic usage for JScrollPane
In the Navigator, click on JPanel with the right mouse button --> Enclose In --> Scroll Pane.
Done!, You have now scrolls
I would like my entire JFrame to vertically scroll.
I have added the following code, but it only creates a horizontal scrollbar.
frame.setContentPane(new JScrollPane(new GradeQuickResource()));
I want to do the opposite. I ONLY want a vertical scrollbar and NO horizontal scrollbar.
I have seen the horizontal policy code, but it doesn't seem to work. I do not know what to name it. This code looks something like this:
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Thanks!
Try this:
public class AddScrollBarToJFrame {
public static void main(String[] args) {
JPanel panel = new JPanel();
JScrollPane scrollBar = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame("AddScrollBarToJFrame");
frame.add(scrollBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
Also, you can have a look at How to use a ScrollBar in both vertical and horizontal direction.
AFIK:
JScrollPane scrollableTa=new JScrollPane(targetComp);
scrollableTa.setHorizontalScrollBar(null);
works
I have a JPanel subclass on which I add buttons, labels, tables, etc. To show on screen it I use JFrame:
MainPanel mainPanel = new MainPanel(); //JPanel subclass
JFrame mainFrame = new JFrame();
mainFrame.setTitle("main window title");
mainFrame.getContentPane().add(mainPanel);
mainFrame.setLocation(100, 100);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
But when I size the window, size of panel don't change. How to make size of panel to be the same as the size of window even if it was resized?
You can set a layout manager like BorderLayout and then define more specifically, where your panel should go:
MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
This puts the panel into the center area of the frame and lets it grow automatically when resizing the frame.
You need to set a layout manager for the JFrame to use - This deals with how components are positioned. A useful one is the BorderLayout manager.
Simply adding the following line of code should fix your problems:
mainFrame.setLayout(new BorderLayout());
(Do this before adding components to the JFrame)
If the BorderLayout option provided by our friends doesnot work, try adding ComponentListerner to the JFrame and implement the componentResized(event) method. When the JFrame object will be resized, this method will be called. So if you write the the code to set the size of the JPanel in this method, you will achieve the intended result.
Ya, I know this 'solution' is not good but use it as a safety net.
;)
From my experience, I used GridLayout.
thePanel.setLayout(new GridLayout(a,b,c,d));
a = row number, b = column number, c = horizontal gap, d = vertical gap.
For example, if I want to create panel with:
unlimited row (set a = 0)
1 column (set b = 1)
vertical gap= 3 (set d = 3)
The code is below:
thePanel.setLayout(new GridLayout(0,1,0,3));
This method is useful when you want to add JScrollPane to your JPanel. Size of the JPanel inside JScrollPane will automatically changes when you add some components on it, so the JScrollPane will automatically reset the scroll bar.
As other posters have said, you need to change the LayoutManager being used. I always preferred using a GridLayout so your code would become:
MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new GridLayout());
mainFrame.pack();
mainFrame.setVisible(true);
GridLayout seems more conceptually correct to me when you want your panel to take up the entire screen.