No matter what alignment I use, the JLabel is always displayed on the left of my JScrollpane and not on top of it. Here is the code:
final JPanel choseTypeOfAnswerText = new JPanel();
JLabel label = new JLabel("Answer:");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
choseTypeOfAnswerText.add(label);
//now a scroll pane for the answer area
JScrollPane answerScroller = new JScrollPane(answerArea);
answerScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
answerScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
choseTypeOfAnswerText.add(answerScroller, BorderLayout.CENTER);
//add(answerScroller);
choseTypeOfAnswerText.setVisible(true);
choseTypeOfAnswerText.add(answerScroller, BorderLayout.CENTER);
have to change LayoutManger to the BorderLayout (JPanel.setLayout(new BorderLayout()))
JPanel has implemented FlowLayout, corresponding with a.m. described issue
only Top-Level Containers have got implemented BorderLayout by default
You forgot to tell, that label is supposed to be added in the top area of the panel:
choseTypeOfAnswerText.add(label, BorderLayout.PAGE_START);
And, like mKorbel stated, you have to set the LayoutManager to BorderLayout
If you use a JScrollPane you do not need to put it in a JPanel; it actually replaces the JPanel. You can add your label to the JScrollPane.
Related
JScrollPane doesn't show what component i put inside it's constructor
panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
JButton button = new JButton("button");
panel.add(button);
scroll = new JScrollPane(panel);
scroll.setBounds(0, 0, 300, 300);
parent.add(scroll);
all parent layouts is set to null except for this one because it must use layout manager right? and the crazy thing is when i code this in a simple program it works but if i try to add this in my actual program everthing is ok except for the panel that i insert on its constructor only the border, the background of JScrollBar appears and also i notice that if i just add the panel directly to the parent component even i set the layout to GridLayout it still act that it is set to null.. Please can someone help me.
problem fixed..
panel.revalidate();
panel.repaint();
pane.revalidate();
pane.repaint();
I have a simple problem when I want to add tabs in my jpanel. The alignment of the tabs get horizontal instead of vertical, wich looks like crap =/.
It looks like this:
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
If you uncomment the three lines of code and remove the getContentPane().add(jtp); you can reproduce my probleme.
working Code:
public class TabbedPane extends JFrame
{
public TabbedPane()
{
setTitle("Tabbed Pane");
setSize(300, 300); // set size so the user can "see" it
JTabbedPane jtp = new JTabbedPane();
// JPanel panel = new JPanel();//uncomment all three lines
// panel.add(jtp);
// getContentPane().add(panel);
getContentPane().add(jtp);//remove me
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
setVisible(true); // otherwise you won't "see" it
}
public static void main(String[] args)
{
TabbedPane tab = new TabbedPane();
}
}
Thanks a lot!
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
The default layout of JPanel is FlowLayout, which "lets each component assume its natural (preferred) size." The default layout of JFrame is BorderLayout, the CENTER of which ignores preferred size. In either case, invoking setSize() precludes the layout from functioning initially; re-size the frame to see the effect. Instead, use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it
There are many things I would change in that code, starting with the recommendations of #trashgod. OTOH this is the minimal change needed in order to stretch the tabbed pane to the width/height of the parent container.
// give the panel a layout that will stretch components to available space
JPanel panel = new JPanel(new GridLayout());//uncomment all three lines
panel.add(jtp);
getContentPane().add(panel);
//getContentPane().add(jtp);//remove me
For more details see this answer.
Well firstly you can try this:
JPanel panel = new JPanel();//uncomment all three lines
panel.setLayout(new GridLayout());
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
getContentPane().add(jtp);
and in the main:
TabbedPane tab = new TabbedPane();
tab.pack();
tab.setVisible(true);
May I suggest using MigLayout to set layouts, it will make your life easier. Hope it helps.
Try GridbagLayout. Once you have mastered it, you can design UI of any sort with this layout.
I agree with prasanth regarding the use of GridBagLayout
I have gone through this problem once and I solved it by adding the JTabbedPaneto the panel via GridBagLayout, make sure you add the JTabbedPane using the ipadx and ipady according to your requirements in your GridBagConstraints object
e.g.
JPanel myPanel=new JPanel();
myPanel.setLayout(new GridBagLayout());
JTabbedPane jTP=new JTabbedPane();
jTP.add("Tab1",new JPanel());//substitute your component instead of "new JPanel"
GridBagConstraints myConstraints=new GridBagConstraints();
myConstraints.ipadx=400;//streches the component being added along x axis - 200 px on both sides
myConstraints.ipady=600;//streches the component being added along y axis - 200 px on both sides
myPanel.add(jTP,myConstraints);
You can adjust both these properties according to what is perfect for your need
I have been searching around for an easy way to implement a JScrollPlane. I am trying to add it to a JPanel, and it will contain a dynamic number of JPanels (which will be filled with other stuff).
Here is my (failing miserably) attempt to make said JScrollPane:
final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
info.setLayout(new GridLayout(0,1));
info.setLocation(10,78);
info.setSize(420,490);
infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
The primary problem you're having is the fact that the default layout manager's layout is set to FlowLayout, which means that the JScrollPane will want to use it's preferred size to be layout with, which may not fill the entire panel.
Instead, use a BorderLayout
final JPanel info = new JPanel(new BorderLayout()); // <-- Change me :D
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// These are bad ideas, setLocation and setSize won't work, as the panel should be
// under the control of a layout manager
//info.setLocation(10,78);
//info.setSize(420,490);
//infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
In this example, a series of nested panels are added to a panel having BoxLayout. That panel is used to create a JScrollPane which is then added to a JFrame.
public class BoxTest extends JPanel {
...
JScrollPane jsp = new JScrollPane(this,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
...
JFrame f = new JFrame();
f.add(jsp); // BorderLayout.CENTER, by default
How can I get the scroller around my JList component in the code given below? It doesn't seem to work properly :(
public class JButtonO extends JFrame{
String[] values = {"henry", "Michael","Uche","John","Ullan","Nelly",
"Ime","Lekan","Austine","jussi","Ossi","Imam","Empo","Austine","Becky",
"Scholar","Ruth", "Anny"};
public JButtonO()
{
super("the button");
this.setSize(400,200);
JPanel panel = new JPanel();
JLabel label = new JLabel("Output Items:");
label.setAlignmentX(1);
label.setAlignmentY(1);
JList conList = new JList(values);
conList.setVisibleRowCount(3);
JScrollPane scroller = new JScrollPane(conList);
panel.add(label);
panel.add(scroller);
panel.add(conList);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(panel);
this.setVisible(true);
}
Adding the JScrollPane scroller that includes the JList conList to the JPanel panel is enough.
The mistake is that you are adding the JList a second time.
JScrollPane scroller = new JScrollPane(conList);
panel.add(label);
panel.add(scroller);
panel.add(conList); // <---THIS LINE SHOULD BE DELETED...
Look, I may not answering what you need, because I don´t remember to much of swing layout. I don´t work with it a long time ago...
But removing setting a layout (I remember) on your JPanel it works with this code:
public JButtonO() {
super("the button");
this.setSize(400, 200);
// Create a panel with a borderlayout
JPanel jpanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Output Items:");
label.setAlignmentX(1);
label.setAlignmentY(1);
// Add Label to top of layout
jpanel.add(label, BorderLayout.NORTH);
JList conList = new JList(values);
conList.setVisibleRowCount(3);
JScrollPane scroller = new JScrollPane(conList);
//AddScroll to center
jpanel.add(scroller);
//Add Panel to JFrame
this.add(jpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
I think the problems is the default layoutmaneger of JPanel. Because of how it works your scroll was not "srink" enough to create scrolls...
Hope it helps, even without too much explanation...
ACTUALLY: After I post the answer I saw your mistake. Now I can explain what is wrong. You already added your JList inside your JScrollPane here:
JScrollPane scroller = new JScrollPane(conList);
But after that you put it inside the JPanel:
panel.add(conList);
this changes where yout JList will be displayed, and let the JScroll empty again. Without components it will be displayed with size 0x0 and will not be draw (even being there).
Now I think I helped =D
The JScrollPane has settings called the scrollbar policies which say when the scrollbars are to be displayed. You can set them using JScrolPane(Component,int,int) constructor, or by calling setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy(). The default policies are "as needed", meaning the scrollbar is only displayed if the component is too large to display whole. So if your list fits inside the window, the scrolbars will not be visible, but will become visible when you e.g. make the window smaller using the mouse. You can change one or both policies to "always" using corresponding constants in order to make the scrollbar(s) always visible if that's what you need.
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