This is my first time using swing and I'm having issues with the JPanel












-1















I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.



This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.



    private void Framing()
{
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning");
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT");
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
Frame.setLayout(null);
Frame.setVisible(true);
}


What is the fastest way to fix the issue with the panel not even showing up?
Any solutions are welcomed.










share|improve this question

























  • "It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

    – Thomas
    Jan 17 at 16:22






  • 1





    Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

    – Thomas
    Jan 17 at 16:24













  • You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

    – Snowy_1803
    Jan 17 at 17:11






  • 2





    just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

    – Carlos Heuberger
    Jan 17 at 18:15








  • 1





    have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

    – Carlos Heuberger
    Jan 17 at 18:29


















-1















I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.



This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.



    private void Framing()
{
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning");
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT");
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
Frame.setLayout(null);
Frame.setVisible(true);
}


What is the fastest way to fix the issue with the panel not even showing up?
Any solutions are welcomed.










share|improve this question

























  • "It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

    – Thomas
    Jan 17 at 16:22






  • 1





    Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

    – Thomas
    Jan 17 at 16:24













  • You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

    – Snowy_1803
    Jan 17 at 17:11






  • 2





    just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

    – Carlos Heuberger
    Jan 17 at 18:15








  • 1





    have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

    – Carlos Heuberger
    Jan 17 at 18:29
















-1












-1








-1








I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.



This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.



    private void Framing()
{
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning");
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT");
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
Frame.setLayout(null);
Frame.setVisible(true);
}


What is the fastest way to fix the issue with the panel not even showing up?
Any solutions are welcomed.










share|improve this question
















I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.



This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.



    private void Framing()
{
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning");
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT");
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
Frame.setLayout(null);
Frame.setVisible(true);
}


What is the fastest way to fix the issue with the panel not even showing up?
Any solutions are welcomed.







java swing jpanel layout-manager null-layout-manager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 17 at 18:02









Andrew Thompson

153k27163340




153k27163340










asked Jan 17 at 16:18









FrostyFrosty

11




11













  • "It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

    – Thomas
    Jan 17 at 16:22






  • 1





    Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

    – Thomas
    Jan 17 at 16:24













  • You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

    – Snowy_1803
    Jan 17 at 17:11






  • 2





    just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

    – Carlos Heuberger
    Jan 17 at 18:15








  • 1





    have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

    – Carlos Heuberger
    Jan 17 at 18:29





















  • "It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

    – Thomas
    Jan 17 at 16:22






  • 1





    Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

    – Thomas
    Jan 17 at 16:24













  • You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

    – Snowy_1803
    Jan 17 at 17:11






  • 2





    just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

    – Carlos Heuberger
    Jan 17 at 18:15








  • 1





    have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

    – Carlos Heuberger
    Jan 17 at 18:29



















"It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

– Thomas
Jan 17 at 16:22





"It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways.

– Thomas
Jan 17 at 16:22




1




1





Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

– Thomas
Jan 17 at 16:24







Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: Panel.add(OK, BorderLayout.CENTER);. Here you don't know whether Panel is a class and thus add is a static method or not. According to the conventions it should be but you defined JPanel Panel = new JPanel(); - that makes things confusing in the long run. The same goes for Frame and OK.

– Thomas
Jan 17 at 16:24















You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

– Snowy_1803
Jan 17 at 17:11





You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0.

– Snowy_1803
Jan 17 at 17:11




2




2





just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

– Carlos Heuberger
Jan 17 at 18:15







just remove the line with setLayout(null) - by default JFrame comes with a BorderLayout which is good to start with (and you are already adding the panel with BorderLayout.CENTER) [assuming that this method i being correctly called...]

– Carlos Heuberger
Jan 17 at 18:15






1




1





have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

– Carlos Heuberger
Jan 17 at 18:29







have a look at the official tutorial about Layout Managers (also check the others sections of that trail)

– Carlos Heuberger
Jan 17 at 18:29














1 Answer
1






active

oldest

votes


















0














Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.



With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.



import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

public class SwingTest
{
private void Framing() //Better method name would be "showFrame()"
{
JPanel Panel = new JPanel(); //Better variable name would be "panel"
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
OK.addActionListener((ActionEvent event) -> {System.exit(0);});

//Not necessary. Layout manager will handle this.
//OK.setBounds(100,100,100,100);

Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);

//Not necessary. Layout manager will handle this.
//Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);

//This is the main problem. You should avoid this.
//Frame.setLayout(null);

Frame.setVisible(true);
}

public static void main(String args)
{
new SwingTest().Framing();
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54240089%2fthis-is-my-first-time-using-swing-and-im-having-issues-with-the-jpanel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.



    With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.



    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;

    public class SwingTest
    {
    private void Framing() //Better method name would be "showFrame()"
    {
    JPanel Panel = new JPanel(); //Better variable name would be "panel"
    Panel.setLayout(new BorderLayout());
    JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
    Frame.setUndecorated(true);
    JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
    OK.addActionListener((ActionEvent event) -> {System.exit(0);});

    //Not necessary. Layout manager will handle this.
    //OK.setBounds(100,100,100,100);

    Panel.add(OK, BorderLayout.CENTER);
    Frame.getContentPane().add(Panel, BorderLayout.CENTER);

    //Not necessary. Layout manager will handle this.
    //Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

    Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    Frame.setLocation(600, 300);
    Frame.setResizable(false);

    //This is the main problem. You should avoid this.
    //Frame.setLayout(null);

    Frame.setVisible(true);
    }

    public static void main(String args)
    {
    new SwingTest().Framing();
    }
    }





    share|improve this answer




























      0














      Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.



      With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.



      import javax.swing.*;
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;

      public class SwingTest
      {
      private void Framing() //Better method name would be "showFrame()"
      {
      JPanel Panel = new JPanel(); //Better variable name would be "panel"
      Panel.setLayout(new BorderLayout());
      JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
      Frame.setUndecorated(true);
      JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
      OK.addActionListener((ActionEvent event) -> {System.exit(0);});

      //Not necessary. Layout manager will handle this.
      //OK.setBounds(100,100,100,100);

      Panel.add(OK, BorderLayout.CENTER);
      Frame.getContentPane().add(Panel, BorderLayout.CENTER);

      //Not necessary. Layout manager will handle this.
      //Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

      Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
      Frame.setLocation(600, 300);
      Frame.setResizable(false);

      //This is the main problem. You should avoid this.
      //Frame.setLayout(null);

      Frame.setVisible(true);
      }

      public static void main(String args)
      {
      new SwingTest().Framing();
      }
      }





      share|improve this answer


























        0












        0








        0







        Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.



        With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.



        import javax.swing.*;
        import java.awt.BorderLayout;
        import java.awt.event.ActionEvent;

        public class SwingTest
        {
        private void Framing() //Better method name would be "showFrame()"
        {
        JPanel Panel = new JPanel(); //Better variable name would be "panel"
        Panel.setLayout(new BorderLayout());
        JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
        Frame.setUndecorated(true);
        JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
        OK.addActionListener((ActionEvent event) -> {System.exit(0);});

        //Not necessary. Layout manager will handle this.
        //OK.setBounds(100,100,100,100);

        Panel.add(OK, BorderLayout.CENTER);
        Frame.getContentPane().add(Panel, BorderLayout.CENTER);

        //Not necessary. Layout manager will handle this.
        //Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

        Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        Frame.setLocation(600, 300);
        Frame.setResizable(false);

        //This is the main problem. You should avoid this.
        //Frame.setLayout(null);

        Frame.setVisible(true);
        }

        public static void main(String args)
        {
        new SwingTest().Framing();
        }
        }





        share|improve this answer













        Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.



        With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.



        import javax.swing.*;
        import java.awt.BorderLayout;
        import java.awt.event.ActionEvent;

        public class SwingTest
        {
        private void Framing() //Better method name would be "showFrame()"
        {
        JPanel Panel = new JPanel(); //Better variable name would be "panel"
        Panel.setLayout(new BorderLayout());
        JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
        Frame.setUndecorated(true);
        JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
        OK.addActionListener((ActionEvent event) -> {System.exit(0);});

        //Not necessary. Layout manager will handle this.
        //OK.setBounds(100,100,100,100);

        Panel.add(OK, BorderLayout.CENTER);
        Frame.getContentPane().add(Panel, BorderLayout.CENTER);

        //Not necessary. Layout manager will handle this.
        //Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

        Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        Frame.setLocation(600, 300);
        Frame.setResizable(false);

        //This is the main problem. You should avoid this.
        //Frame.setLayout(null);

        Frame.setVisible(true);
        }

        public static void main(String args)
        {
        new SwingTest().Framing();
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 2:23









        Prasad KarunagodaPrasad Karunagoda

        1,084179




        1,084179






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54240089%2fthis-is-my-first-time-using-swing-and-im-having-issues-with-the-jpanel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage