0

I have done an layout in my GUI but it did not work , all the component just appear one after another.

Here is my codes :

import javax.swing.*;
public class main extends JFrame {
public main() {
    try {

        add(new FYP_Tx.GUI());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("FYP Video Platform");
        setResizable(true);
        setVisible(true);

    } catch (Exception e) {
        System.out.println("Exception: " + e.toString());
    }
}

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



public class GUI extends JPanel implements Runnable, ActionListener, ItemListener {

private JButton btnStart, btnStop, btnPause, btnFile;
private JLabel lblDisplay, lblSNR, lblStatus, lblConfig;
private JCheckBox chkLoop, chkNeg;
private Thread animator;
private JFileChooser fileChooser;
private JList lstML;
private JTextField txtSNR;
private boolean pauseAnimator, loop;
Matlab_options matlab = new Matlab_options();

public GUI() {
    GroupLayout guiLayout = new GroupLayout(this);

    lblDisplay = new JLabel();
    lblStatus = new JLabel();
    lblConfig = new JLabel();
    lblSNR = new JLabel("SNR: ");
    btnStart = new JButton("Start");
    btnStop = new JButton("Stop");
    btnPause = new JButton("Pause");
    btnFile = new JButton("Open File");
    chkLoop = new JCheckBox("Loop");
    chkNeg = new JCheckBox("Negative SNR");
    txtSNR = new JTextField(3);
    txtSNR.setText("10");
    lblDisplay.setDoubleBuffered(true);

    btnStart.setEnabled(false);
    btnStop.setEnabled(false);
    btnPause.setEnabled(false);

    btnStart.setActionCommand("start");
    btnStop.setActionCommand("stop");
    btnPause.setActionCommand("pause");
    btnFile.setActionCommand("file");

    guiLayout.setAutoCreateContainerGaps(true);
    guiLayout.setAutoCreateGaps(true);
    GroupLayout.SequentialGroup hGroup = guiLayout.createSequentialGroup();
    GroupLayout.SequentialGroup vGroup = guiLayout.createSequentialGroup();

    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(lblDisplay));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnStart).addComponent(btnStop).addComponent(btnPause).addComponent(chkLoop));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnFile).addComponent(chkLoop));
    guiLayout.setHorizontalGroup(hGroup);

    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lblDisplay).addComponent(btnStart).addComponent(btnFile));
    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(btnStop).addComponent(chkLoop));
    guiLayout.setVerticalGroup(vGroup);

    btnStart.addActionListener(this);
    btnStop.addActionListener(this);
    btnPause.addActionListener(this);
    btnFile.addActionListener(this);
    chkLoop.addItemListener(this);
    chkNeg.addItemListener(this);
}

public void addNotify() {
    super.addNotify();
    loop = false;
    lblDisplay.setSize(400, 400);
    lblDisplay.setVisible(true);
    animator = new Thread(this);
}

public void paint(Graphics g) {
    super.paint(g);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

`

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
HH.
  • 769
  • 2
  • 10
  • 21
  • 1
    GroupLayout was designed for GUI building tools, personally I'd hand code using a different layout manager such as GridBagLayout or BoxLayout – pstanton Jan 11 '10 at 01:58
  • BoxLayout won't handle this stuff without some serious nested components. I might use GridBagLayout but I can understand it intimidates a lot of people. – Carl Smotricz Jan 11 '10 at 02:01
  • yes it's all personal preference, but i find hand coding GroupLayout more intimidating! – pstanton Jan 11 '10 at 02:27
  • See what splungebob has to say [here](http://stackoverflow.com/questions/21010799/looking-for-general-method-for-gridbaglayout-component-creation) about GridBagLayout – DSlomer64 Nov 19 '14 at 21:46

1 Answers1

3

Don't you have to set the Layoutmanager for the JPanel?

    public GUI() {
        GroupLayout guiLayout = new GroupLayout(this);
        this.setLayout(guiLayout);
 ......
    }
Fabiano
  • 5,124
  • 6
  • 42
  • 69
  • I can't believe it, just one line.... You know, I have tried so many way before posting here. Thanks for the help. – HH. Jan 11 '10 at 02:34
  • @HH: For future reference, instead of `new main()`, consider constructing your Swing GUI on the event dispatch thread: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html – trashgod Jan 11 '10 at 05:52
  • I'm embarrassed to have missed that and offered a much more complicated solution. Deleting... – Carl Smotricz Jan 11 '10 at 08:41
  • @Carl: IIRC, it was an interesting alternative. I'd vote for undelete & edit. – trashgod Jan 11 '10 at 16:57