0

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);
    }
}
Frakcool
  • 10,540
  • 9
  • 46
  • 78
  • 1
    1) Can't test your code now, but in a first view to it I can see you're using this line `contentPane.setLayout(null);` and `setBounds(...)` methods, that's going to cause you a lot of problems, why? Because [Null layout is evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) and [frowned upon](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing)... 2) *"Any ideas why it does that?"* Why it does what? – Frakcool Jun 07 '17 at 19:31
  • Thank you sir Frakcool for responding so quickly, your comment made it so much clear to solve the problem. – Jv Shan Belnas Jun 07 '17 at 19:37
  • 2
    If you solved it, then post an answer, and please, don't call me sir, I'm not old at all... :) – Frakcool Jun 07 '17 at 19:55

1 Answers1

0

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);

}

}

  • 3
    *"The contentPane.setLayout(null); and setBounds(...) aren't the problem"*- You have no idea how far from the truth you are – MadProgrammer Jun 07 '17 at 22:00