I have the next code:
public class MyScroll extends JFrame {
public MyScroll() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton b = new JButton("Hello-" + i);
b.setBounds(0, i * 50, 100, 45);
panel.add(b);
b.setLayout(null);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(50, 30, 100, 325);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.setBackground(Color.BLUE);
contentPane.add(scrollPane);
contentPane.setLayout(null);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
And, it is rendering this:
As you can see, the vertical and horizontal scroll aren't working, but both are defined and are displaying inside the JPanel.
Can someone explain me what am I doing wrong?
This code is based on this one:
Scrolling a JPanel
But, the is not working at the moment to use the verticall scrolling
Can someone explain me what am I doing wrong?
panel.setLayout(null);
Don't use a null layout.
The scrollbars will only appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.
It is the job of the layout manager to determine the preferred size of the panel. Since you don't use a layout manager the preferred size isn't calculated.
So the solution is to use a Layout Manager. Maybe vertical BoxLayout.
Related
I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());
I am new to Java and currently trying to learn it. I am working on a simple program to display a number of buttons on a frame. I also want to make the panel scroll vertically but instead it scrolls horizontally.
Here is my code so far:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class GridView {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Display Buttons");
frame.setBounds(30, 30, 300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(3, 4, 30, 20);
Container content = frame.getContentPane();
content.setLayout(grid);
JPanel panel = new JPanel();
JButton button = null;
for (int i = 1; i <= 100; i++) {
panel.add(button = new JButton(" Press " + i));
}
content.add(panel);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(panel), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Can anyone tell me why my scroll is shown horizontally and how to fix it? Any kind of response will be much appreciated.
EDIT:
I am sorry. My question was incomplete. I want to make it something like this, but with vertical scroll.
//GridLayout grid = new GridLayout(3, 4, 30, 20);
GridLayout grid = new GridLayout(0, 4, 30, 20);
If you want columns of component then don't specify the rows value in the layout manager. Just specify the columns and the components will wrap when required
//content.setLayout(grid);
//JPanel panel = new JPanel();
JPanel panel = new JPanel(grid);
You add the buttons to the panel so you need to set the layout manager of the panel. Otherwise the JPanel will use the default FlowLayout, which display all the buttons on a single row.
It shows horizontally because the buttons are added horizontally so the width of the panel exceeds the view port.
If you added the buttons vertically, by using a different layout manager, the scroll pane would show vertically. For example:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); //or setLayout(grid); if you meant to use tge gridlayout for the buttons
/*JButton button = null;*/ //never used
for (int i = 1; i <= 100; i++) {
panel.add(new JButton(" Press " + i));
}
Edit to answer the edited question. See comments:
public static void main(String[] args) {
JFrame frame = new JFrame("Display Buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(0, 4, 30, 20);
JPanel panel = new JPanel(grid);
for (int i = 1; i <= 100; i++) {
panel.add(new JButton(" Press " + i));
}
JScrollPane sp = new JScrollPane(panel);
//by default scrollpane will appear as needed, vertically AND horizontally
//to prevent it from showing horizontally :
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(sp, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
I am getting insane! I am adding a JPanel to an another JPanel by a method. This method is generating a grid via SpringLayout. The problem is that after adding all JComponents to the panel the preferred size of the panel is 0, thus no content is displayed. Using an another layout manager solves the visibility but I need the structures of the SpringLayout. I can't find my problem.
private JPanel panelErzeugen(ArrayList<JComponent> labels){
JPanel panel = new JPanel();
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
int abstandVert = 5;
int abstandHori = 5;
for(JComponent label : labels){
if(labels.indexOf(label) == labels.size()/2){
abstandVert = 100;
abstandHori = 5;
}
layout.putConstraint(SpringLayout.WEST, panel, abstandVert, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.NORTH, panel, abstandHori, SpringLayout.NORTH, label);
panel.add(label);
abstandHori = abstandHori + 25;
}
return panel;
}
I want to set every element location with .setLocation(x, y)
I need a JPanel in a JScrollPane. But when I add components to JPanel, only buttons are showing. But not JLabel.
Method below is calling in JFrame constructor:
private void initGUI_test() {
this.setSize(950, 700);
this.setResizable(false);
this.getContentPane().setLayout(null);
JScrollPane mainScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel container = new JPanel();
mainScroll.setSize(900,500);
mainScroll.setLocation(0,100);
mainScroll.setBorder(BorderFactory.createLineBorder(Color.blue));
mainScroll.add(container);
container.setLayout(null);
container.setLocation(0, 0);
container.setSize(900, 500);
JLabel rowA = new JLabel();
rowA.setSize(180, 26);
rowA.setLocation(10, 100);
rowA.setText("Row A");
JButton loadButton = new JButton();
loadButton.setSize(180, 34);
loadButton.setLocation(290, 110);
loadButton.setText("Load file");
container.add(rowA);
container.add(loadButton);
this.getContentPane().add(mainScroll);
}
Although I agree completely with #Frakcool about null layout, the problem you are facing has a different source. You should not add components directly into JScrollPane, but into JScrollPane's ViewPort.
The line mainScroll.add(container); should be mainScroll.getViewport().add(container);
My question is similar to this one (How to get JScrollPanes within a JScrollPane to follow parent's resizing), but that question wasn't clear and the answer there didn't help me..
I have this SSCCE (using MigLayout):
public static final int pref_height = 500;
public static void main(String[] args) {
JPanel innerPanel = new JPanel(new MigLayout());
innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));
for(int i = 0; i < 15; i++) {
JTextArea textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp = new JScrollPane(textArea);
innerPanel.add(new JLabel("Notes" + i));
innerPanel.add(jsp, "span, grow");
}
JScrollPane jsp = new JScrollPane(innerPanel) {
#Override
public Dimension getPreferredSize() {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return dim;
}
};
jsp.setBorder(new LineBorder(Color.green, 5));
JPanel outerPanel = new JPanel();
outerPanel.setBorder(new LineBorder(Color.RED, 5));
outerPanel.add(jsp);
JFrame frame = new JFrame();
JDesktopPane jdp = new JDesktopPane();
frame.add(jdp);
jdp.setPreferredSize(new Dimension(800, 600));
frame.pack();
JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
jif.pack();
jif.add(outerPanel);
jdp.add(jif);
jif.pack();
jif.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want the JScrollPane to resize whenever the parent JPanel is resized. Basically, I want the green border to line up with the red border. Right now, the green border stays the same size no matter the red border (unless you resize too small).
JPanel outerPanel = new JPanel();
A JPanel uses a FlowLayout by default which always respects the size of the component added to it. As a guess, maybe you can use:
JPanel outerPanel = new JPanel( new BorderLayout() );
A BorderLayout give all the space available to the component added to the panel. By default a JInternalFrame also uses a BorderLayout. So since all the parent components of your scroll pane use a BorderLayout all the space should go to the scroll pane.
When you post a SSCCE you should post code using classes from the JDK that simulates your problem so that everybody can test your SSCCE.
I noticed this did not have an answer that uses the original layout so here is one.
In order to make the JScrollPane resize when the parent JPanel is resized you need to do two things.
1) Set the layout of the panel to grow. This can be using the following code.
new MigLayout("", //Layout Constraints
"grow", //Column Constraints
"grow"); //Row Constraints
2) Set the component to grow. This is as simple as adding an extra argument in the add() function.
add(jsp, "grow");
ExtraIn order to make the JTextArea column grow when you resize the JScrollPane you can change the layout to only make the second column change. For example
new MigLayout("", //Layout Constraints
"[/*Column 1*/][grow /*Column 2*/]", //Column Constraints
""); //Row Constraints
Also, I would recommend you use wrap instead of span to use the next row as span refers using so many columns. For example span 2 //Means use 2 columns for this component. This would mean when you add your jsp to innerPanel it would become
innerPanel.add(jsp, "wrap, grow");
Edited SSSCE
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.miginfocom.swing.MigLayout;
public class JSPR extends JFrame {
public static final int pref_height = 500;
public static void main(String[] args) {
JPanel innerPanel = new JPanel(new MigLayout("", "[][grow]", ""));
innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));
for(int i = 0; i < 15; i++) {
JTextArea textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp = new JScrollPane(textArea);
innerPanel.add(new JLabel("Notes" + i));
innerPanel.add(jsp, "wrap, grow");
}
JScrollPane jsp = new JScrollPane(innerPanel) {
#Override
public Dimension getPreferredSize() {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return dim;
}
};
jsp.setBorder(new LineBorder(Color.green, 5));
JPanel outerPanel = new JPanel(new MigLayout("", "grow", "grow"));
outerPanel.setBorder(new LineBorder(Color.RED, 5));
outerPanel.add(jsp, "grow");
JFrame frame = new JFrame();
JDesktopPane jdp = new JDesktopPane();
frame.add(jdp);
jdp.setPreferredSize(new Dimension(800, 600));
frame.pack();
JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
jif.pack();
jif.add(outerPanel);
jdp.add(jif);
jif.pack();
jif.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}