Java Conversion Program

Hi everyone reading this. I appreciate you taking the time to even look at my problem.
I'm designing a converter app in Java to convert units, km to ms-1, degrees celsius to fahrenheit etc etc..
I have built a basic Gui using the WindowBuilder plugin in Eclipse and I have no idea where to go next.

I'm fine for the maths for the equations etc, but my main issues arise when I try to figure out how to put that in the GUI.
In the GUI, you select the unit you are converting from, eg kilometers, and you type the amount of said unit that you wish to convert, then you select the unit to convert it to and hit convert. I can't populate the drop downs and have no idea how to get it to work.

Can anybody help me get it working with 2 conversions so that I may learn how to do this, I also plan on making the program receive a 'feed' from a website for 'live' currency rates for accurate conversions.

Here is my code for Main.Java
package converter.views;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Main extends JFrame 
{

    private JPanel contentPane;
    private JDesktopPane desktopPane;
    private JButton btnNew;
    private JMenuItem mntmNew;
    private Form1 form1;
    private Form2 form2;
    private JMenuItem mntmAboutThisApplication;

    /**
     * Launch the application.
     */
    public static void main(String[] args) 
    {
        try 
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                try 
                {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() 
    {
        initComponents();
        createEvents();
    }

    private void initComponents()
    {
        setTitle("Multi-Converter Application 1.0");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 475);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        mntmNew = new JMenuItem("New Conversion");
        mntmNew.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
        mnFile.add(mntmNew);

        JMenuItem mntmExitApplication = new JMenuItem("Exit Application");
        mntmExitApplication.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                dispose();
            }
        });
        mntmExitApplication.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/close.gif")));
        mnFile.add(mntmExitApplication);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        mntmAboutThisApplication = new JMenuItem("About this application");

        mntmAboutThisApplication.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/info.png")));
        mnAbout.add(mntmAboutThisApplication);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JToolBar toolBar = new JToolBar();

        desktopPane = new JDesktopPane();
        desktopPane.setBackground(SystemColor.window);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(toolBar, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                .addComponent(desktopPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(7)
                    .addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE))
        );
        GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
        gl_desktopPane.setHorizontalGroup(
            gl_desktopPane.createParallelGroup(Alignment.LEADING)
                .addGap(0, 574, Short.MAX_VALUE)
        );
        gl_desktopPane.setVerticalGroup(
            gl_desktopPane.createParallelGroup(Alignment.LEADING)
                .addGap(0, 374, Short.MAX_VALUE)
        );
        desktopPane.setLayout(gl_desktopPane);

        btnNew = new JButton("New Conversion");


        btnNew.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
        toolBar.add(btnNew);
        contentPane.setLayout(gl_contentPane);
        //setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]()))

    }

    private void createEvents()
    {

        btnNew.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                if(form1 == null || form1.isClosed())
                {
                    form1 = new Form1();
                    desktopPane.add(form1);
                    form1.show();
                }
            }
        });

        mntmAboutThisApplication.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                if(form2 == null || form2.isClosed())
                {
                    form2 = new Form2();
                    desktopPane.add(form2);
                    form2.show();
                }
            }
        });

    }
}



## I apologise for the length of code to trudge through, following this heading is Form1.Java (The main Conversion internal frame) ##

                        package converter.views;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Form1 extends JInternalFrame 
{
    private JTextField textField;
    private JPanel panelData;
    private JPanel panelButtons;


    /**
     * Create the frame.
     */
    public Form1() 
    {
        setTitle("Conversion");
        setMaximizable(true);
        setIconifiable(true);
        setClosable(true);
        setBounds(0, 0, 450, 300);

        panelData = new JPanel();
        panelData.setBorder(new TitledBorder(null, "New Conversion", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        panelButtons = new JPanel();
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addComponent(panelButtons, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                        .addComponent(panelData, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panelData, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(panelButtons, GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                    .addContainerGap())
        );

        JLabel lblConvertFrom = new JLabel("Convert from :");

        JComboBox comboBox = new JComboBox();

        JLabel lblTo = new JLabel("To :");

        JComboBox comboBox_1 = new JComboBox();

        JLabel lblEnterAmount = new JLabel("Enter Amount :");

        textField = new JTextField();
        textField.setColumns(10);

        JLabel lblYourResult = new JLabel("Your Result :");

        JButton btnConvert = new JButton("Convert");
        btnConvert.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {

            }
        });

        GroupLayout gl_panelData = new GroupLayout(panelData);
        gl_panelData.setHorizontalGroup(
            gl_panelData.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panelData.createSequentialGroup()
                    .addGroup(gl_panelData.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panelData.createSequentialGroup()
                            .addGap(12)
                            .addComponent(lblConvertFrom)
                            .addGap(18)
                            .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE))
                        .addGroup(gl_panelData.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(gl_panelData.createParallelGroup(Alignment.TRAILING)
                                .addComponent(lblTo)
                                .addComponent(lblEnterAmount)
                                .addComponent(lblYourResult))
                            .addGroup(gl_panelData.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_panelData.createSequentialGroup()
                                    .addGap(18)
                                    .addGroup(gl_panelData.createParallelGroup(Alignment.LEADING)
                                        .addComponent(comboBox_1, 0, 160, Short.MAX_VALUE)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(Alignment.TRAILING, gl_panelData.createSequentialGroup()
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(btnConvert, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                    .addGap(25)))))
                    .addGap(141))
        );
        gl_panelData.setVerticalGroup(
            gl_panelData.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panelData.createSequentialGroup()
                    .addGroup(gl_panelData.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblConvertFrom)
                        .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(gl_panelData.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblEnterAmount)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(gl_panelData.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblTo)
                        .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addComponent(btnConvert, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                    .addGap(12)
                    .addComponent(lblYourResult)
                    .addContainerGap())
        );
        panelData.setLayout(gl_panelData);

        JButton btnAccept = new JButton("Accept");
        panelButtons.add(btnAccept);

        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0)
            {
                dispose();
            }
        });
        panelButtons.add(btnCancel);
        getContentPane().setLayout(groupLayout);

    }
}


## Here is the code for Form 2 (About Window)##

                package converter.views;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.SystemColor;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Form2 extends JInternalFrame 
{
    private JButton btnClose;

    /**
     * Create the frame.
     */
    public Form2() 
    {
        setIconifiable(true);
        setClosable(true);
        getContentPane().setBackground(SystemColor.window);
        setTitle("About");
        setBounds(0, 0, 450, 300);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "About ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .addContainerGap())
        );

        JTextPane txtpnWelcomeToMulticonvert = new JTextPane();
        txtpnWelcomeToMulticonvert.setText("Welcome to Multi-Convert.");

        btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                dispose();
            }
        });
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(txtpnWelcomeToMulticonvert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGroup(gl_panel.createSequentialGroup()
                            .addGap(169)
                            .addComponent(btnClose)))
                    .addContainerGap(174, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(txtpnWelcomeToMulticonvert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                    .addComponent(btnClose)
                    .addContainerGap())
        );
        panel.setLayout(gl_panel);
        getContentPane().setLayout(groupLayout);

    }
}
I have a part here called conversions.Java , I was ripping up code in a mad attempt to get something working
package converter.views;

public class Conversions 
{
    public float celsiusToFarenheit(float celsius)
    {
    return (celsius * 9 / 5) + 32;
    }

    public float farenheitToCelsius(float farenheit)
    {
    return (farenheit - 32) * 5 / 9;
    }
}
If anyone can help me it will be greatly appreciated. Thanks very much, Sam.

Recommended Answers

All 3 Replies

what do you mean by:

but my main issues arise when I try to figure out how to put that in the GUI.

you can create a class Conversions with static methods, for instance:

public static int getMeters(int kilom){
  return kilom*1000;
}

while in your GUI, you call it a bit like this:

...
@Override
public void actionPerformed(ActionEvent event){
    int kil = Integer.parseInt(jTextField1.getText());
    jLabel1.setText("the nr of meters = " + MyStaticClass.getMeters(kil));
}

Hi, thanks for the reply, by this,

but my main issues arise when I try to figure out how to put that in the GUI.

I mean that I can't wrap my head around how to select a method or something in the first drop down and where to put the maths,
and then how to take the value from the user, calculate it using whatever i have in the first drop down, into whatever value it should be in the second.

I dunno if I'm over-complicating this but thanks for helping me. I dunno how to populate the drop down either and have tried my best to find a good tutorial for that. Sorry for my lack of knowledge with this but I've only recently started to teach myself about Jframes & GUI development etc through online tuts.

I think I half understand what you've given me though, So in conversions, I make a seperate "static" method for each conversion, and thats that sorted
but how would I tell the program to convert from meters to kilometers, whereby the user would select meters in the first drop down, enter 1000, and then in the second drop down, select kilometers and receive the result. Theres a screenshot of exactly what the GUI looks like so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.