0

I am trying to make an application in java, to start with i have problems in the GUI. I have put Jpanel inside Jframe ,but i am gettingf problems when i use setMaximumSize and moreover i want to fix the size of the Jpanel, so that even if user tries to change the size of the window , jpanel remains at center. i have tried this a solution at StackOverflow
How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame? Please guide me through.This is my first post, i dont have enough reputations to post an image .Thanks

import javax.swing.*;
import java.awt.*;

 class App_Demo4 extends JFrame {

    public App_Demo4()
    {

        JFrame frm=new JFrame("Application");
        JPanel pane=new JPanel();
        frm.setVisible(true);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLayout(new BorderLayout());
        frm.add(pane,BorderLayout.CENTER);
        pane.setSize(400,400);
        Dimension dim=new Dimension(400,400);
         pane.setMinimumSize(dim);


        Box box = new Box(BoxLayout.Y_AXIS);

        box.add(Box.createVerticalGlue());
        box.add(pane);     
        box.add(Box.createVerticalGlue());

        frm.add(box);
        frm.setSize(500,500);

        JButton dbtn1=new JButton("Download File");
        pane.add(dbtn1);
        JTextField txt1=new JTextField(20);
        pane.add(txt1);
        JButton bbtn1=new JButton("Browse");
        pane.add(bbtn1);

        JButton dbtn2=new JButton("Download Mail");
        pane.add(dbtn2);
        JTextField txt2=new JTextField(20);
        pane.add(txt2);
        JButton bbtn2=new JButton("Browse");
        pane.add(bbtn2);

        JButton cbtn1=new JButton("Compile File");
        pane.add(cbtn1);
        JTextField txt3=new JTextField(20);
        pane.add(txt3);
        JButton bbtn3=new JButton("Browse");
        pane.add(bbtn3);

        JButton cbtn2=new JButton("Cancel");
        pane.add(cbtn2,BorderLayout.EAST);


    }
    public static void main(String[] args)
    {
        new App_Demo4();
    }
}
Community
  • 1
  • 1

2 Answers2

1

Don't try to set the size of a panel. That is the job of the layout manager.

You need to change the layout manager of your panel. By default a JPanel uses a FlowLayout. In this case the components just flow to a new line depending on the width of the frame.

You might want to look at a GridBagLayout. See the section from the Swing tutorial on How to Use a GridBagLayout for more information and examples.

Also, make sure you add all the components to the frame BEFORE packing the frame and making the frame visible. So you basic code should be:

frame.add(....)l
frame.pack();
frame.setVisible(true);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The person is creating a frame into a class extending a frame. that is the wrong way and no comment was said on that – JajaDrinker Apr 08 '14 at 09:46
  • @JajaDrinker, I hope you down voted yourself and all the other answers in the forum as well since the vast majority of all code posted in the forum extends JFrame. – camickr Apr 08 '14 at 16:52
  • I don't care about vast majority! If they are wrong, we should tell them all! when you extend JFrame, the right way to do it is : this.add(pane); this.pack(); this.setVisible(true); – JajaDrinker Apr 09 '14 at 04:39
  • @JajaDrinker, no that code is not correct. You can't use "this" because that implies you are extending JFrame and you just said that is wrong. Please quit being a child and be reasonable. You don't down vote because people don't point out every possible mistake with posted code. And as I already stated you are being a hypocrite because you have not yet down voted yourself and made a comment, so be consistent. Even worse you posted code that does extend a JFrame so you endorsed the OP's incorrect code. – camickr Apr 09 '14 at 04:50
  • Learn to Read! You MUST extend JFrame to do it the right way!! And you MUST run with javax.swing.SwingUtilities.invokeLater(new Runnable() .... Learn some Java! – JajaDrinker Apr 09 '14 at 06:58
-1

Thats how you do it :

Hope it helps...

import javax.swing.*;
import java.awt.*;

 public class App_Demo4 extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public App_Demo4()
    {

        super("Application");
        JPanel pane=new JPanel();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(500,500));

        GridBagLayout gl = new GridBagLayout();
        pane.setLayout(gl);

        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(6, 6, 6, 6);

        c.gridx = 0;
        c.gridy = 0;

        JButton dbtn1=new JButton("Download File");
        pane.add(dbtn1, c);

        JTextField txt1=new JTextField(20);
        c.gridx = 1;
        pane.add(txt1, c);

        JButton bbtn1=new JButton("Browse");
        c.gridx = 2;
        pane.add(bbtn1, c);

        JButton dbtn2=new JButton("Download Mail");
        c.gridy = 1;
        c.gridx = 0;
        pane.add(dbtn2, c);

        JTextField txt2=new JTextField(20);
        c.gridx = 1;
        pane.add(txt2, c);

        JButton bbtn2=new JButton("Browse");
        c.gridx = 2;
        pane.add(bbtn2, c);

        c.gridy = 2;
        c.gridx = 0;
        JButton cbtn1=new JButton("Compile File");
        pane.add(cbtn1, c);

        JTextField txt3=new JTextField(20);
        c.gridx = 1;
        pane.add(txt3, c);

        JButton bbtn3=new JButton("Browse");
        c.gridx = 2;
        pane.add(bbtn3, c);

        c.gridy = 3;
        c.gridx = 2;
        JButton cbtn2=new JButton("Cancel");
        pane.add(cbtn2,c);

        this.add(pane);
        this.pack();
        this.setVisible(true);

    }
    public static void main(String[] args)
    {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 new App_Demo4();
            }
        });

    }
}
JajaDrinker
  • 652
  • 6
  • 15
  • Spoon feedind? lol... this is a basic exemple that he will change/analyse and advance mutch more faster! And i know because when i seek help i look for code exemple. API are hard to read when you are begening! I didint give him a solution but an exemple. You give a link with code i put the code here... – JajaDrinker Apr 05 '14 at 16:04
  • The Swing tutorial is full of examples. Not only for GridBagLayout, but also for other layouts as well as other features of Swing. Posting code without explaining the problem with the original code does NOT help the OP understand what is going on. So yes this is unnecessary spoon feeding. – camickr Apr 05 '14 at 18:50
  • "I believe you received your down vote because here on Stack Overflow, it is expected that the answer include the answer, not just a link to some site. As sites change frequently, this link may not be available for future visitors, so the answer does no good to someone, say, in a year from now looking for the same help. If you know the code to use, post it here. Otherwise, the answer isn't helpful. – Charlie74 1 hour ago" @camickr http://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-a-video-control/22885762?noredirect=1#comment34922719_22885762 – JajaDrinker Apr 05 '14 at 20:57
  • @camickr i give help you down me i give link for them to shearch other guy down me. So cancel that down pls! – JajaDrinker Apr 05 '14 at 20:58
  • `You MUST extend JFrame to do it the right way!! And you MUST run with javax.swing.SwingUtilities.invokeLater(new Runnable() ` @JajaDrinker, I add your comment to your question since you are the one who posted the incorrect code. I hope you learn from your mistakes. – camickr Apr 09 '14 at 15:39
  • @camickr OMG! My Code is not the wrong Code!! Here is my code : Look above! – JajaDrinker Apr 10 '14 at 09:04
  • I did look above, you are extending JFrame. (I copied too much code for the comment when I included the part about SwingUtilities). – camickr Apr 10 '14 at 16:52