geisterfurz007 geisterfurz007 - 1 month ago 14
Java Question

Displaying multiple Images in scrollPane results in components missing based on image size

I am using the following construct to display up to 3 images on labels based on need:

|_Panel with BorderLayout
|_Label with ImageIcon (Placed North)
|_Label with ImageIcon (Placed Center)
|_Panel with BorderLayout (Placed South)
|_Label with ImageIcon (Placed North)
|_Button (Placed West)
|_Button (Placed East)

The images placed on the labels are 1654*2340 pixels each ("null.jpg" is 1*1 pixel)

With a button on another tab I can print out various parameters I considered to be important during testing (mostly size/position of components).

If I use "null.jpg" as picture for the third label everything works as it is supposed to be; I see 2 pictures and buttons (and if hardly looking the additional pixel of "null.jpg").

If I now switch to using another 1654*2340 pixel image, everything goes nuts: the buttons are missing, the third picture is not shown and (looking into the parameters given out by the additional button) the size of the two buttons that are supposed to be there is -2305 pixels.

The size results from the image beeing (not) displayed on the second panel (2340 pixels) and the height of the second panel (35 pixels) -> 35-2340=-2305

However I have no idea why this happens as using a smaller image everything works as supposed to. As you see from the code below, I have checked various parameters ranging from label sizes over the panels to button position and size.

Why does this happen and how can I fix it? I am searching for 4 hours straight and cannot find why this happens. Below is a tested example code that actually reproduces the problem. Taking away the comment on
ImageIcon p3
and placing it in front of the other line defining
returns different results.

Again: p1-p3 are images of the size 1654*2340 pixels where "null" is 1*1.

If anything else is needed, I am eager to help!

Any help is much appreciated!

package my_package;

import java.awt.EventQueue;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class testsforSO extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testsforSO frame = new testsforSO();
frame.setSize(300, 300);
} catch (Exception e) {

public testsforSO() {
getContentPane().setLayout(new BorderLayout(0, 0));

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

JPanel panel = new JPanel();

JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("Pictures", null, scrollPane, null);

JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout(0, 0));

JLabel lblNewLabel = new JLabel();
panel_1.add(lblNewLabel, BorderLayout.NORTH);

JLabel lblNewLabel_1 = new JLabel();
panel_1.add(lblNewLabel_1, BorderLayout.CENTER);

JPanel panel_2 = new JPanel();
panel_1.add(panel_2, BorderLayout.SOUTH);
panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));
panel_2.setLayout(new BorderLayout(0, 0));

JLabel lblNewLabel_2 = new JLabel();
panel_2.add(lblNewLabel_2, BorderLayout.NORTH);

JButton btnNewButton = new JButton("Saving Button");
panel_2.add(btnNewButton, BorderLayout.WEST);

JButton btnNewButton_1 = new JButton("Printing Button");
panel_2.add(btnNewButton_1, BorderLayout.EAST);

try {
ImageIcon p1 = new ImageIcon(new File("C:\\path\\to\\p1.jpg").toURI().toURL());
ImageIcon p2 = new ImageIcon(new File("C:\\path\\to\\p2.jpg").toURI().toURL());
ImageIcon p3 = new ImageIcon(new File("C:\\path\\to\\p3.jpg").toURI().toURL());
//ImageIcon p3 = new ImageIcon(new File("C:\\path\\to\\null.jpg").toURI().toURL());


} catch (MalformedURLException e) {

JButton btnMyButton = new JButton("Details");
btnMyButton.setBounds(10, 11, 89, 23);
btnMyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("1 Height"+lblNewLabel.getHeight()+" Y: "+ lblNewLabel.getY());
System.out.println("2 Height"+lblNewLabel_1.getHeight()+" Y: "+ lblNewLabel_2.getY());
System.out.println("3 Width: "+lblNewLabel_2.getSize().getWidth()+" Height: "+lblNewLabel_2.getSize().getHeight());
System.out.println("3 x/y: "+SwingUtilities.convertPoint(lblNewLabel_2, lblNewLabel_2.getLocation(), panel_1).toString());
System.out.println("End of 2: "+(lblNewLabel_1.getY()+lblNewLabel_1.getHeight()));
System.out.println("Panel_2.isVisible: "+Boolean.toString(panel_2.isVisible()));
System.out.println("Panel_2 x/y/w/h: "+panel_2.getX()+" "+panel_2.getY()+" "+panel_2.getWidth()+" "+panel_2.getHeight());
System.out.println("Saving Button x/y/w/h: "+btnNewButton.getX()+" "+btnNewButton.getY()+" "+btnNewButton.getWidth()+" "+btnNewButton.getHeight());}
tabbedPane.add("Update Button", panel);


Quick edit: I do not get any exception or compiler error...


You have limited your panel that holds the third image and the buttons to a maximum height of 35px with this line:

panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));

This was enough to display the buttons when the image has zero size. But for a big image only the upper part was shown and there was no space left for the buttons.

Removing this line will solve your problem.