user2358330 user2358330 - 2 months ago 8
Java Question

Using WindowBuilder as GUI for Java Program

I am a beginner in Java. I have devloped a GUI using WindowBuilder in Eclipse. I want to use this GUI to get input from user and pass it on to my Java program which performs some operations. Basically I want to use the GUI as a substitute for Console in Eclipse for entering the input. How do I do this?

Please point me to some tutorials or examples which can help. Thanks!

Answer

For a start you can i.e. look here for an example how to implement buttons. You can browse the site also for other components.

The general concept is that you create your GUI with Window Builder visually. Then you can attach action handlers, which are called when the object is triggered. So for example in ordeer to perform some action when a button is pressed you do something like this:

in the main code:

createGUI(this);

In the gui code:

class MyGui
{
    private JButton jButton;
    private MyButtonListener mListener;

    public void createGUI(MyButtonListener oListener)
    {
        mListener = oListener;
        createGUIElements();
    }

    private createGUIElements()
    {
        jButton = new JButton();
        jButton.setText("MyButton");
        jButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent e)
            {
                mListener.onButtonClicked(e);             
            }
        });
     }
 }

Or an alternative approach where you directly create the action listener in the main application and jsut pass it to the GUi element.

class MyGui
{
    private JButton jButton;
    private ActionListener mListener;

    public void createGUI(ActionListener oListener)
    {
        mListener = oListener;
        createGUIElements();
    }

    private createGUIElements()
    {
        jButton = new JButton();
        jButton.setText("MyButton");
        jButton.addActionListener(mListener);
     }
 }

The same basically applies to most other controls as well, so you can attach an action handler on a combobox, checkbox, etc..

So to get started, just create a simple window with a single button and try to implement something when the button is pressed.