Nested JScrollPane not fitting - java

I'm having an issue with nested JScrollPanes. Basically I want to have one outer JScrollPane that scrolls vertically but not horizontally (Think the Netflix web interface). Inside this outer JScrollPane I want to have multiple JScrollPanes that scroll horizontally. My problem is that horizontal scrollbars for the inner JScrollPanes never show up as it looks like they take up the entire preferred size of their JPanels. Here is an image to describe what I am talking about:
EDIT: This code based on camickr's answer is now working:
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class NestedScrollPane extends JFrame {
public NestedScrollPane() {
ScrollablePanel outerPanel = new ScrollablePanel();
outerPanel.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
for (int j = 0; j < 20; j++) {
ScrollablePanel innerPanel = new ScrollablePanel();
innerPanel.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE);
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
JScrollPane innerScrollPane = new JScrollPane(innerPanel);
innerScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 10; i++) {
JLabel longLabel = new JLabel("asefaesfesfesfgesgersgrsgdrsgdrsgderg ");
innerPanel.add(longLabel);
}
outerPanel.add(innerScrollPane);
}
JScrollPane outerPane = new JScrollPane(outerPanel);
outerPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.setContentPane(outerPane);
this.setSize(400, 400);
outerPane.setSize(400, 400);
this.setVisible(true);
}
public static void main (String[] args) {
NestedScrollPane pane = new NestedScrollPane();
pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I took a look at How to get JScrollPanes within a JScrollPane to follow parent's resizing but using BoxLayout or BorderLayout on the outer panel doesn't seem to fix anything.

You need to implement the Scrollable interface of the outer panel added to the viewport to force the panel to fill the width of the viewport.
An easy way to do this is to use the Scrollable Panel. You should be able to use:
// JPanel outerPanel = new JPanel();
ScrollablePanel outerPanel = new ScrollablePanel();
outerPanel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

Related

Setting JPanel background while using BoxLayout

I am developing a simple application, and am currently working on the gui design using Swing. In my program I have a JPanel which I would like to have a background color black like so:
JPanel playerPanel = new JPanel();
playerPanel.setOpaque(true);
playerPanel.setBackground(Color.BLACK);
This code works fine. However, the problem is when I assign a Layout Manager to the panel:
JPanel playerPanel = new JPanel();
playerPanel.setOpaque(true);
playerPanel.setBackground(Color.BLACK);
playerPanel.setLayout(new BoxLayout(playerPanel, BoxLayout.PAGE_AXIS));
For some reason, this makes the black color of the panel go away. This happens no matter where I place the .setLayout(...) command, before or after the .setBackground(...) and .setOpaque(true).
Why is this, and how do I work around this? How do I keep a black JPanel that uses a BoxLayout manager?
Verify that your panel's content is not obscuring the altered background. Resize the example below, which I've artificially enlarged, to see the effect.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* #see https://stackoverflow.com/a/57785802/230513
*/
public class BoxTest {
public static final Random random = new Random();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new BoxTest().create();
}
});
}
void create() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Color.BLACK);
panel.add(Box.createVerticalGlue());
for (int i = 0; i < 4; i++) {
panel.add(new VariablePanel());
panel.add(Box.createVerticalGlue());
}
JFrame f = new JFrame("BoxTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setSize(f.getWidth(), f.getHeight() + 64);
}
}
/**
* A VariablePanel has a label showing its current size,
* as well as a variable number of text items.
*/
class VariablePanel extends JPanel {
private static final String text =
"Sed ut perspiciatis unde omnis iste natus error sit.";
private final JLabel sizeLabel = new JLabel("Size:");
public VariablePanel() {
this.setLayout(new GridLayout(0, 1));
this.add(sizeLabel);
int count = BoxTest.random.nextInt(5) + 1;
for (int i = 0; i < count; i++) {
this.add(new JLabel(text));
}
this.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
int w = e.getComponent().getWidth();
int h = e.getComponent().getHeight();
sizeLabel.setText("Size: " + w + "\u00d7" + h);
}
});
}
}
Swing components (except JLabel) are opaque by default. This means:
you don't need playerPanel.setOpaque(true)
most components you add to the panel will be opaque and cover the background of your playerPanel.
Also, the BoxLayout respects the maximum size of any component you add to the panel. So if you add a component:
like a JButton which has a defined maximum size, you will see the button on top of the playerPanel and the background will surround the button.
like a JPanel, which does not have a defined maximum size, the panel will be resized to fill the entire area of the playerPanel and you won't see the background of the playerPanel.
If you want to see the background of the playerPanel show through a component added to the playerPanel, then you need to use setOpaque(false) on the component. For example:
JPanel child = new JPanel();
child.setOpaque( false );
playerPanel.add( child );

Java components replacement JPanel

I'm trying to create an application in 3 parts : 3 labels and 3 gridlayouts. When we click on a label, the corresponding gridlayout disappear and the frame replace automatically the components at the right place. I created a simple snippet :
import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestLayout extends JFrame{
private JPanel content;
private JLabel[] lbl;
private JPanel[] pnl;
private Boolean[] ih;
public TestLayout(){
setTitle("Test");
setSize(new Dimension(300, 400));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
lbl = new JLabel[3];
pnl = new JPanel[3];
ih = new Boolean[3];
content = new JPanel(new GridLayout(6, 1));
for(int i=0; i<3; i++){
lbl[i] = new JLabel("Label" + i);
lbl[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
for(int i=0; i<3; i++){
if(e.getSource() == lbl[i]){
//pnl[i].setVisible(!pnl[i].isVisible());
if(ih[i]) content.remove(pnl[i]);
else content.add(pnl[i]);
ih[i] = !ih[i];
}
}
}
});
pnl[i] = new JPanel(new GridLayout(3, 3));
}
for(int i=0; i<9; i++){
pnl[0].add(new Button("" + (i+1)));
pnl[1].add(new Button("" + (i+10)));
pnl[2].add(new Button("" + (i+19)));
}
for(int i=0; i<3; i++){
content.add(lbl[i]);
content.add(pnl[i]);
ih[i] = true;
}
add(content);
setVisible(true);
}
public static void main(String[] args){
new TestLayout();
}
}
The first problem is the use of a global gridlayout which resize all the components at the same size, but I think it would be better if labels could be smaller than the grilayouts.
The second problem is that even if the gridlayout is removed or setVisible(false), it still take a blank place in the global container.
What I get :
What I was expecting :
The only thing I don't wanna use is a GridBagLayout.
I was thinking about create a method init() which one remove all components of the global container then re add all the labels and all the panels, then create another method which do the exact same as the init() method but take a number as parameter (for example 2) then re add all the components excepting the second gridlayout. But I think it's a dirty way to do that because the container will content an empty case at the end and I think there is a better way than removing and re adding all the components (which basically doesn't solve the first problem of label's size)
How can I avoid theses problems ?
Try using a vertical BoxLayout.
Read the section from the Swing tutorial on How to Use BoxLayout for more information and working examples.

Java Swing: how to add multiple JPanels to a JScrollPane

On the net I read that to add a component to a JscrollPane we must perform:
scrollPane.getViewport().setView(jpanel);
Well, this is my code. To show multiple components, in this case JButtons, I am trying to add them into multiple JPanels and add these last in order at the end. But only the last JPanel is shown. Why?
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import layout.TableLayout;
public class Main {
public static void main(String argv[]) {
JFrame jframe = new JFrame("Protocollo UTL");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(1200, 450);
JPanel body = new JPanel();
double[][] size = {
{0.05},
{0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05}
};
body.setLayout(new TableLayout(size));
for(int i=0; i<19; i++) {
body.add(new JButton(String.valueOf(i)), "0,"+String.valueOf(i));
}
JPanel body2 = new JPanel();
body2.setLayout(new TableLayout(size));
for(int j=0; j<6; j++) {
body2.add(new JButton(String.valueOf(j)), "0,"+String.valueOf(j));
}
JScrollPane scrollPane = new JScrollPane(body,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getViewport().setView(body);
scrollPane.getViewport().setView(body2);
jframe.add(scrollPane);
jframe.setVisible(true);
}
}
You are overriding the first one with the second statement
scrollPane.getViewport().setView(body);
scrollPane.getViewport().setView(body2);
You should add both JPanels to some parent component, and set that component as view. Something like this:
Container cont = new Container();
cont.add(body);
cont.add(body2);
scrollPane.getViewport().setView(cont);
EDIT
I don't think you need that line (setView(...)) at all. Try this (put this instead of last four lines)
Container cont = new Container();
cont.add(scrollPane);
cont.add(body2);
cont.setLayout(new GridLayout());
jframe.add(cont);
jframe.setVisible(true);

Using a JPanel with a null layout

So I have a class called CalendarPanel that extends JPanel. It uses a null layout. How would I use CalendarPanel as a regular component? When I put it in another JPanel and then add it to a window, it disappears. It is only visible when I add it directly to a window.
EDIT:
And yes, I realize using a JPanel with a null layout is bad practice. CalendarPanel is actually someone else's code, and I'm trying to use it for my purposes without having to refactor it.
It is only visible when I add it directly to a window.
That is because a window uses a BorderLayout by default and will automatically resize the panel to fit in the window.
When I put it in another JPanel and then add it to a window, it disappears.
The is because a JPanel uses a FlowLayout by default and a flow layout respects the preferred size of the components added to it. Since you are using a null layout your panel doesn't have a preferred size so nothing gets painted.
That is why you should NOT use null layout. Instead use layout managers because they do all this extra work for you.
NOTE: it is a very bad idea in general to use a null layout. Use a LayoutManager instead.
If you insist on using a null layout, you're going to have to position the JPanel manually as mentioned in the documentation. Here's an example.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test extends JFrame {
static int defaultX = 10;
static int defaultY = 10;
static int defaultW = 150;
static int defaultH = 50;
public Test() {
super("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// here is the outer JPanel
final JPanel outer = new JPanel(new BorderLayout());
JPanel inner = new JPanel(new BorderLayout());
// here is the main component we want to see
// when the outer panel is added to the null layout
JButton mainComponent = new JButton("Test");
inner.add("Center", mainComponent);
outer.add("Center", inner);
JPanel c = (JPanel)getContentPane();
// This panel has a null layout!
final JPanel nullLayoutPanel = new JPanel();
nullLayoutPanel.setLayout(null);
c.add("Center", nullLayoutPanel);
// set the bounds of the outer panel manually
// when using the null layout!
nullLayoutPanel.add(outer);
outer.setBounds(defaultX, defaultY, defaultW, defaultH);
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
final JTextField x = new JTextField(""+defaultX, 3);
final JTextField y = new JTextField(""+defaultY, 3);
final JTextField w = new JTextField(""+defaultW, 3);
final JTextField h = new JTextField(""+defaultH, 3);
JButton b = new JButton("Resize");
b.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
outer.setBounds(
Integer.parseInt(x.getText()),
Integer.parseInt(y.getText()),
Integer.parseInt(w.getText()),
Integer.parseInt(h.getText())
);
outer.revalidate();
} catch(Exception ex) {}
}
}
);
controlPanel.add(x);
controlPanel.add(y);
controlPanel.add(w);
controlPanel.add(h);
controlPanel.add(b);
c.add("South", controlPanel);
}
public static void main(String[] argv) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test p = new Test();
p.setSize(300, 200);
p.setVisible(true);
}
});
}
}

MigLayout row-height and changing font-size

I have a problem using MigLayout in combination with dynamically changing the font-size of the components which are shown in the MigLayout cells.
In detail: I added a JCheckBox via MigLayout to a JPanel. The font-size of the JCheckBox is default (12pt?) and the row which contains the JCheckBox has a preferred height of 17lp. That all works fine.
(View here: http://www.bilderload.com/bild/227327/migproblemcellheight1UQXP2.png)
Now I change the font-size to e.g. 20pt and start the program again. Now the text of the JCheckBox is cut because the row has also the height of 17lp.
(View here: http://www.bilderload.com/bild/227328/migproblemcellheight2DDPGJ.png)
If I for example let the row definition empty ("[]") the text shows correctly with both font sizes - the normal and the large one. But in this case the row will sadly never reach a minimum of 17lp. (It will always have a minimum of 23lp or so)
How can I change the MigLayout definition to get a minimum row-height of 17lp and to let the cell grow correctly with the components font-size/text etc.?
Or maybe this is a L&F problem?
Thanks & best regards,
Philipp
Here is my sample code (working example):
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class TestMigLayoutFontSize extends JFrame {
public TestMigLayoutFontSize() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setContentPane(getTestPanel());
setVisible(true);
}
private JPanel getTestPanel() {
JCheckBox testBox = new JCheckBox("Program argument");
Font normalFont = testBox.getFont();
Font largeFont = new Font(testBox.getFont().getName(), testBox.getFont().getStyle(), 20);
// testBox.setFont(normalFont);
testBox.setFont(largeFont);
JPanel tempPanel = new JPanel(new MigLayout("debug", "0lp![grow,fill]0lp!", "[17lp:17lp:n]"));
tempPanel.add(testBox);
JPanel testPanel = new JPanel(new MigLayout("", "[grow,fill]", "[grow,fill]"));
testPanel.add(tempPanel);
return testPanel;
}
public static void main(String[] args) {
new TestMigLayoutFontSize();
}
}
You may reduce the space around your checkbox by reducing the border size, e.g. put
testBox.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
directly after the assignment of testBox. You may then leave the row definition empty and still get a reasonable height for your panel.
The following works for me. I think the problem is , that you specify the preferred size.
Regards
Roger
package de.test;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigTest extends JFrame {
public MigTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setContentPane(getTestPanel());
setVisible(true);
}
private JPanel getTestPanel() {
JCheckBox testBox = new JCheckBox("Program argument");
Font normalFont = testBox.getFont();
Font largeFont = new Font(testBox.getFont().getName(), testBox.getFont().getStyle(), 90);
// testBox.setFont(normalFont);
testBox.setFont(largeFont);
JPanel tempPanel = new JPanel(new MigLayout("debug", "0lp![grow,fill]0lp!", "[80:n:]"));
tempPanel.add(testBox);
JPanel testPanel = new JPanel(new MigLayout("", "[grow,fill]", "[grow,fill]"));
testPanel.add(tempPanel);
return testPanel;
}
public static void main(String[] args) {
new MigTest();
}
}

Categories