简体   繁体   中英

How to fix JScrollBar on JLabel with ImageIcon in Java?

Can anyone help me? I'm trying to create a JFrame where I could adjust the JScrollBar and change the view from top to bottom of the JLabel with an ImageIcon , including the other JLabel that has a MouseEvent of a JFileChooser .

Unfortunately, my code isn't working as I thought it would. Any ideas why it does that? or is there any missing codes that I should be aware of? My only goal is to make the JScrollBar work on both JLabels

public class DocVieweR extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    private int yPos = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DocVieweR frame = new DocVieweR();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DocVieweR() {
        setTitle("DocVieweR");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(816, 705);
        setLocationRelativeTo(null);        
        setResizable(false);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                yPos =+ e.getValue();               
            }
        });
        scrollBar.setBounds(793, 0, 17, 676);
        contentPane.add(scrollBar);     

        JLabel lblPHOTO1x1 = new JLabel("SELECT IMAGE");
        lblPHOTO1x1.setFont(new Font("Segoe UI", Font.BOLD, 16));
        lblPHOTO1x1.setHorizontalAlignment(SwingConstants.CENTER);
        lblPHOTO1x1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser chosenFILE = new JFileChooser();
                chosenFILE.showOpenDialog(null);
                File getFILE = chosenFILE.getSelectedFile();
                ImageIcon pic = new ImageIcon(getFILE.getAbsolutePath());
                lblPHOTO1x1.setIcon(pic);
            }
        });
        lblPHOTO1x1.setBounds(585, 228, 129, 128);
        contentPane.add(lblPHOTO1x1);       

        JLabel lblBIODATA = new JLabel("");
        ImageIcon img = new ImageIcon("resources/BIODATA_96dpi.jpg");
        lblBIODATA.setIcon(img);
        lblBIODATA.setBounds(0, yPos, 793, 1122);
        contentPane.add(lblBIODATA);
    }
}

I have been using JScrollBar when I should have been using JScrollPane. In addition, I accidentally discovered that you must add an extra line of code such as "scrollPane.setViewportView(JLabelNAME);" to make it work and I also noticed that once you move the scrollbar after you imported the image using JFileChooser, the image disappears but won't be much of a problem (a few debug might help). The contentPane.setLayout(null); and setBounds(...) aren't the problem but still gave me the idea, even though. THANK YOU FRAKCOOL!!! :D

public class DocVieweR extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DocVieweR frame = new DocVieweR();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DocVieweR() {
    setTitle("DocVieweR");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(818, 705);
    setLocationRelativeTo(null);        
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);        

    JLabel lblPHOTO1x1 = new JLabel("SELECT IMAGE");
    lblPHOTO1x1.setFont(new Font("Segoe UI", Font.BOLD, 16));
    lblPHOTO1x1.setHorizontalAlignment(SwingConstants.CENTER);
    lblPHOTO1x1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JFileChooser chosenFILE = new JFileChooser();
            chosenFILE.showOpenDialog(null);
            File getFILE = chosenFILE.getSelectedFile();
            ImageIcon pic = new ImageIcon(getFILE.getAbsolutePath());
            lblPHOTO1x1.setIcon(pic);
        }
    });     
    lblPHOTO1x1.setBounds(585, 228, 129, 128);
    contentPane.add(lblPHOTO1x1);
    //scrollPane.setViewportView(lblPHOTO1x1);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(0, 0, 812, 676);
    contentPane.add(scrollPane);        

    JLabel lblBIODATA = new JLabel("");
    ImageIcon img = new ImageIcon("resources/BIODATA_96dpi.jpg");
    lblBIODATA.setIcon(img);
    lblBIODATA.setSize(793, 1122);
    scrollPane.setViewportView(lblBIODATA);
    contentPane.add(lblBIODATA);

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM