Swing desktop java application scroll [closed]

2019-09-21 21:08发布

问题:

I am new in swing java application and I am developing a small desktop application that creates jlabels at run time with every button click.

The problem is when the jlabels number is too much, I need to display a scroll in the jframe to make the user able to see all jlabels (jlabel created after the height of the jframe).

I tried jscroll pane and jscroll bar but with no use , please provide a complete example.

Thanks in advance for your efforts.

回答1:

Seems to work for me, please post SSCCE to show specific problems, the only thing that springs to mind is you added you JLabel to the container and not the JScrollPane

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {//size frame purposefully smaller
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                JScrollPane jsp = new JScrollPane(label);

                frame.add(jsp);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

UPDATE

I see you say:

(jlabel created after the height of the jframe).

So i take it the JFrame is visible? if so:

Here is an example which shows how to add JLabel to JScrollPane after 2 seconds of showing JFrame, most important is to call revalidate() and repaint() on the containers instance after adding/removing/changing component sizes.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JScrollPane jsp = new JScrollPane();
                frame.add(jsp);
                frame.pack();
                frame.setVisible(true);

               Timer t= new Timer(2000, new AbstractAction() {//create timer to add JLabel to scrollPane after 2 seconds
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        final JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                        jsp.setViewportView(label);
                        //refelect changes
                        frame.revalidate();
                        frame.repaint();
                    }
                });
                t.setRepeats(false);
                t.start();
            }
        });
    }
}

UPADTE 2

as per comment its the exact same thing with minor alterations:

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JPanel labelPanel = new JPanel(new GridLayout(4, 1));

                final JScrollPane jsp = new JScrollPane(labelPanel);
                frame.add(jsp);
                frame.pack();
                frame.setVisible(true);

                Timer t = new Timer(2000, new AbstractAction() {//create timer to add JLabel to scrollPane after 2 seconds
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        final JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                        final JLabel label2 = new JLabel("Try it");
                        final JLabel label3 = new JLabel("Noooooo reealllly");
                        final JLabel label4 = new JLabel("Yes");
                        labelPanel.add(label);
                        labelPanel.add(label2);
                        labelPanel.add(label3);
                        labelPanel.add(label4);
                        labelPanel.revalidate();
                        labelPanel.repaint();
                    }
                });
                t.setRepeats(false);
                t.start();
            }
        });
    }
}