2

Im trying to show images and/or labels in my JPanel. The thing is, I want to show many JLabels in custom locations but currently I can only show like 4 labels. I understand I need to use ScrollPane. I have tried using it but its not letting me alocate them with customs locations. (By custom locations I mean using coords to place my label.)

Here is the code WITHOUT using ScrollPane:

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame{
    private JPanel contentPane;
    public static void main(String[] args) {
        Images image=new Images();
    }
    public Images(){
        setVisible(true);
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 800);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo);
        int coordY=150;
        for(int i=0;i<10;i++){
            JLabel myLabel = new JLabel("attempt: "+i);
            myLabel.setBounds(147,coordY, 145, 14);
            contentPane.add(myLabel);
            coordY=coordY+150;
        }

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stellsh
  • 61
  • 3
  • You should post your code containing your attempt trying to use a `JScrollPanel`. What you posted looks more like a "please write code for me" request, instead of "I tried; where did I go wrong?" But to answer some problems with the above code: You are creating your `JPanel` with the default `FlowLayout`, which overrides the any position information given to children added to the `JPanel`. – AJNeufeld Jun 09 '17 at 21:49
  • Recommend against using a `null layout` - rather, use an appropriate `LayoutManager`. See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – copeg Jun 09 '17 at 21:50

1 Answers1

2

Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.

Here is an example. Adjust the layout padding and component border parameters to provide the 'exact' requirement & note further comments in the code.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        new Images();
    }

    public Images() {
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setBounds(100, 100, 800, 800); call pack() instead
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //contentPane.setLayout(null); // never do that!
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
        //lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
        //int coordY = 150;
        JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
        contentPane.add(new JScrollPane(scrollPanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        for (int i = 0; i < 10; i++) {
            JLabel myLabel = new JLabel("attempt: " + i);
            myLabel.setBorder(new EmptyBorder(20, 140, 20, 140));
            //myLabel.setBounds(147, coordY, 145, 14);
            scrollPanel.add(myLabel);
            //coordY = coordY + 150;
        }
        pack();
        // now the preferred size has been calculated, we can shorten the height
        Dimension d = getSize();
        setSize(new Dimension(d.width, 250));
        setLocationByPlatform(true);
        setMinimumSize(getSize());
        setVisible(true); //should be last
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you very much. I found your comments really useful. It fixed my problems with my labels and images. Thank you again :D – stellsh Jun 10 '17 at 17:51
  • I have one tiny problem now. I cant modify the size of my images. Currently Im using JLabel myLabel = new JLabel(new ImageIcon("myImage.png")); If you happen to know how I can fix it, it would be great. Otherwise, thanks anyway. – stellsh Jun 10 '17 at 17:57
  • *"If you happen to know how I can fix it.."* Ask another (new) question. – Andrew Thompson Jun 10 '17 at 18:48