3

I want to create a JFrame by hand and use spring layout to do this. But, my finally output is not good. The space between my rows is so much, and between my radio buttons too:

example output

My code:

public final class NewUserFrame1 extends JFrame {

public NewUserFrame1() {
    add(rowComponent(), BorderLayout.CENTER);
    setLocation(200, 40);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    pack();
}

public JPanel rowComponent() {

    JPanel panel = new JPanel();
    JLabel fnamelbl = new JLabel("First name");
    JLabel lnamelbl = new JLabel("Last Name");
    JLabel fntemp = new JLabel();
    JLabel lntemp = new JLabel();
    JTextField fntf = new JTextField(10);
    JTextField lntf = new JTextField(10);
    JLabel gndlnl = new JLabel("Gender");
    JRadioButton malerb = new JRadioButton("Male");
    JRadioButton femalerb = new JRadioButton("Female");
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(malerb);
    bgroup.add(femalerb);
    JLabel registnm = new JLabel("Registration ID is:");
    JLabel showreglbl = new JLabel();
    JLabel regtemp = new JLabel();

    panel.add(fnamelbl);
    panel.add(fntf);
    panel.add(fntemp);
    panel.add(lnamelbl);
    panel.add(lntf);
    panel.add(lntemp);
    panel.add(gndlnl);
    panel.add(malerb);
    panel.add(femalerb);
    panel.add(registnm);
    panel.add(showreglbl);
    panel.add(regtemp);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);
    return panel;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            NewUserFrame1 newUserFrame1 = new NewUserFrame1();
        }
    });
}
}

Now: enter image description here

Sajad
  • 2,273
  • 11
  • 49
  • 92

2 Answers2

1
  1. Your code is not compilable (missing imports).
  2. You wrote:

    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 100);

The last argument is yPad. Change this to 10 (or lower value if you want), for example:

SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 10);

But still - label will be to high etc., but it's a different issue. Keep playing with panel's size and your component's size.

In case of radio buttons - change

panel.add(malerb);
panel.add(femalerb);

To something like:

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radioPanel.add(malerb);
radioPanel.add(femalerb);
panel.add(radioPanel);
panel.add(new JLabel());

The last line is needed because you declared your layout to have 3 columns.

Marcin Skiba
  • 103
  • 4
1

Instead of calling setSize call pack on JFrame within your NewUserFrame1 constructor.

public NewUserFrame1() {
    add(rowComponent(), BorderLayout.CENTER);
    setLocation(200, 40);
    //setSize(800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    pack();
}

Also change the parameters of SpringUtilities.makeCompactGrid method in following way:

SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);//change yPad to 4 instead of 100. It sets the vertical height between two rows
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • After use pack method, My problem was solved, But my main window was too small – Sajad Apr 09 '13 at 19:14
  • Ok..If you want to increase the width and height you should increase `xPad` and `yPad` a little..and keep testing with new value till you get the size of your desire. – Vishal K Apr 09 '13 at 19:17
  • One question: when should use pack method instead of setSize method? – Sajad Apr 09 '13 at 19:21
  • And if you want to increase the width of your `textfields` while calling the constructor of `JTextField` call it as `new JTextField(30)` (or more integer value) – Vishal K Apr 09 '13 at 19:22
  • One question: when should use pack method instead of setSize method? – Sajad Apr 09 '13 at 19:24
  • I had a general question: Is java j2EE just the same using database? – Sajad Apr 09 '13 at 19:25
  • Read this link http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack%28%29 to know why to use `pack` – Vishal K Apr 09 '13 at 19:27
  • And regarding your general question for j2EE .. You can google those question. I am sure you would get your answer. – Vishal K Apr 09 '13 at 19:29
  • I don't install SQL developer, Should i install it? – Sajad Apr 16 '13 at 10:10
  • For 11g tutorial you can download the book for beginners from here http://www.sourcecodester.com/sites/default/files/books/evilyuna/Oracle.Database.11g.A.Beginners.Guide_.By_.McGraw.Hill_.pdf . Yes you should install SQL developer also as this would help you to write sql queries more efficiently and also to understand many aspects of how SQL queries – Vishal K Apr 16 '13 at 16:31
  • Thanks for replay, Should i learn JDBC before learning Oracle or MySQL? – Sajad Apr 17 '13 at 13:04
  • First you should start learning SQL queries on Oracle..And after you become enough comfortable with SQL queries..start learning JDBC in parallel with oracle... – Vishal K Apr 17 '13 at 17:49
  • Hi, After much research and questions, I found that I should choose Mysql database to learn , because it's much easier to learn than oracle! So I installed the Mysql and Mysql WorkBench. I still do not know how to work with Mysql WorkBench! – Sajad Apr 19 '13 at 15:31
  • Should i use MySQL Command line to learn SQL queries and statements? – Sajad Apr 19 '13 at 15:33
  • Hi Mr Vishal K, I had a question, but my question is all about programming, I had a plan for our University, Our university has a wireless network that everyone can be connected toit and the do Student Affairs works with university website. My plan is that I want to install an antivirus program on the university server computer And everyone who is connected to the wireless network, be Able to scan own laptop, freely and easy and even no need to install antivirus software. Is this doable? to do this what was needed and What we should do? Thanks for attention. – Sajad Apr 23 '13 at 19:00
  • @Sajjad- Sorry..I couldn't help you in this matter as I am not an expert in it. Why not you post this as question in this site? You might get some useful suggestions from experts.. – Vishal K Apr 28 '13 at 09:09