Fr4ncx
Fr4ncx

Reputation: 356

JLabels in a JScrollPane

I have a scrollpane in which i want to add multiple jlabel. This is the code..

JPanel panelEast = new JPanel();
    panelEast.setBorder(new TitledBorder(null, "Notifiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    panelEast.setPreferredSize(new Dimension(250,120));
    panelEast.setLayout(null);

    JLabel lblNewLabel_3 = new JLabel("label1");
    lblNewLabel_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/contact.png")));
    lblNewLabel_3.setBounds(10, 81, 240, 52);
    panelEast.add(lblNewLabel_3);

    JLabel label_3 = new JLabel("label2");
    label_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
    label_3.setBounds(new Rectangle(4, 0, 0, 0));
    label_3.setAlignmentY(Component.TOP_ALIGNMENT);
    label_3.setBounds(10, 30, 240, 52);
    panelEast.add(label_3);

    JLabel label_4 = new JLabel("label3");
    label_4.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
    label_4.setBounds(new Rectangle(4, 0, 0, 0));
    label_4.setAlignmentY(Component.TOP_ALIGNMENT);
    label_4.setBounds(10, 131, 240, 52);
    panelEast.add(label_4);

    JScrollPane scrollPane = new JScrollPane(panelEast,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pan2.add(scrollPane,BorderLayout.CENTER);

And does not work, any suggestion?

Upvotes: 0

Views: 142

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

JScrollPane relies on the layout manager API in order to calculate the preferred size of the container it is showing and determine when that view is larger than itself and it should show the scroll bars

Upvotes: 3

Related Questions