-1

I am very new to Java. I am supposed to have a frame which has two buttons. The first button makes the user browse an image and it works. Second button should ask parameter with dialogbox but it doesn't even appear on the frame.

edit: I have solved the problem in a stupid way but thanks anyway. I added setVisible(true) for button2 and somehow it worked. The weird thing is I didn't do the same thing for button but still it's working. I didn't get it but if it works it works.Thanks again, hope someone else could get a solution from your answers.

public class ImageBrowser extends JFrame {

private static final long serialVersionUID = 1L;
JButton button, button2;
JLabel label;

private int K;
private int IFP;



public ImageBrowser() {
    super("Image Browser");

    button = new JButton("Browse");
    button.setBounds(200, 300, 90, 40);
    label = new JLabel();
    label.setBounds(10, 10, 256, 256);
    button2 = new JButton("Parameters");
    button2.setBounds(500, 300, 150, 40);

    add(button);
    add(label);
    add(button2);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // textArea.setText("");
            JFileChooser file = new JFileChooser();
            file.setCurrentDirectory(new File(System
                    .getProperty("user.home")));
            // filter the files
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "*.Images", "jpg", "gif", "png", "tif");
            file.addChoosableFileFilter(filter);
            int result = file.showOpenDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {

                File selectedFile = file.getSelectedFile();

                String path = selectedFile.getAbsolutePath();
                label.setIcon(ResizeImage(path));
                getPixels(selectedFile);
            }

            else if (result == JFileChooser.CANCEL_OPTION) {
                System.out.println("No File Select");
            }

        }
    });

    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String input = JOptionPane
                    .showInputDialog("Please input Parameter k");
            if (input == null)
                return;
            int K = Integer.parseInt(input.trim());

            input = JOptionPane
                    .showInputDialog("Please input  Parameter ifp");
            if (input == null)
                return;
            int IFK = Integer.parseInt(input.trim());

        }
    });
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(100, 100);
    setSize(700, 400);
    setVisible(true);
}


public static void main(String[] args) throws IOException {
    new ImageBrowser();

}

}
  • This code works for me. – Steve Smith Feb 20 '17 at 12:15
  • 1
    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](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 20 '17 at 12:24
  • 1
    1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (It seems that the action listeners, as well as the `getPixels(..)` & `ResizeImage(..)` methods are irrelevant to a layout problem. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently... – Andrew Thompson Feb 20 '17 at 12:38
  • 1
    .. 4) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Feb 20 '17 at 12:38
  • 1
    *"I didn't get it but if it works it works."* (shakes head sadly) This isn't a solution. It's a hack that will probably work randomly (and by extension, fail randomly). Fix. The. Layouts. – Andrew Thompson Feb 20 '17 at 17:19

1 Answers1

4

You set layout to null so you have absolute positioning and your components overlap each other

  setLayout(null); 

You can try other layout manager for example FlowLayout:

  setLayout(new FlowLayout()); 

Here you have descriptions of layout managers

Lemonov
  • 476
  • 4
  • 17
  • Looking at the co-ords, I can't see how they would overlap? – Steve Smith Feb 20 '17 at 12:17
  • 1
    @SteveSmith There are a *lot of reasons* not to use `null` layouts. – Andrew Thompson Feb 20 '17 at 12:35
  • Agreed, but this answer didn't seem to solve the problem. The components don't overlap AFAIKS. – Steve Smith Feb 20 '17 at 12:37
  • 1
    @SteveSmith *"The components don't overlap AFAIKS."* So? When I ran the code, and added a line border to the label, I saw two buttons and a label (or at least the outline of a label). Still, the OP is claiming they ***don't*** see a second button, and that is likely due to a number of other reasons why we should not position components using magic numbers. The *first thing* they should fix is the part of the code where it does not use layouts, then I expect the current problem they report will be fixed. This answer is suggesting the right approach to a solution. – Andrew Thompson Feb 20 '17 at 12:45
  • It's a matter of opinion, but IMHO the idea of SO is primarily to answer user's question more than just re-write their code for them. I for one am still curious why OP can't see the second button. – Steve Smith Feb 20 '17 at 12:58
  • 1
    @SteveSmith *"It's a matter of opinion"* Everyone is entitled to an opinion, OTOH the consensus from [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) is 'Heck yes - if you have a better approach'. My view is of the nature that if someone posts an answer that applies a band-aid to fix a `null` layout is that it deserves a down-vote, in that it does not fix the underlying problem, and will result in the OP asking (at least) two more questions as the GUI falls apart in other ways. – Andrew Thompson Feb 20 '17 at 13:20
  • I have solved the problem in a stupid way but thanks anyway. I added setVisible(true) for button2 and somehow it worked. The weird thing is I didn't do the same thing for button but still it's working. I didn't get it but if it works it works.Thanks again, hope someone else could get a solution from your answers. –  Feb 20 '17 at 15:05
  • 2
    @SelimŞahin that shouldn't be necessary if you were using a proper [layout manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). See [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) to see why you should avoid the use of `null layout` and absolute positioning (.i.e. `setBounds(...)`) – Frakcool Feb 20 '17 at 16:34