7 Replies - 579 Views - Last Post: 25 February 2012 - 09:13 AM Rate Topic: -----

#1 NrTRuSH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 07:20 AM

Hello,

I am trying to make a GUI that does conversions. My first conversion will be Celsius to Fahrenheit and Fahrenheit to Celsius. I would like to use a Tabbed Frame similar to the code below:

TabbedPane
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;


public class TabbedPane extends JFrame {
    
    public TabbedPane() {
        
        setTitle("Conversion Program");
        JTabbedPane jtp = new JTabbedPane();
       // JFrame frame = new JFrame();
        getContentPane().add(jtp);
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        JPanel jp4 = new JPanel();
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JLabel label3 = new JLabel();
        JLabel label4 = new JLabel();
        JTextField miles = new JTextField();
        
		final JComponent[] inputs = new JComponent[] 
				{
				new JLabel("Make"),
				miles,

				};
        
        
        
        
        label1.setText("This program will convert measurements and distance from user input. Please select which tab you would like to enter data in."); // Label for About
        label2.setText("Please enter how many Miles to convert to Kilometers."); // Label for Miles to Kilometers
        label3.setText("Kilo to Mile"); // Label for Kilometers to Miles
        label4.setText("Fah to Cel"); // Label for Fahrenheit to Celcius
        jp1.add(label1);
        jp2.add(label2);
        jp3.add(label3);
        jp4.add(label4);
        jtp.addTab("About", jp1);
        jtp.addTab("Miles to Kilometer", jp2);
        jtp.addTab("Kilometers to Miles", jp3);
        jtp.addTab("Fahrenheit to Celcius", jp4);
	JButton test = new JButton("Convert");
	//frame.add(jp1);
	//frame.add(jp2);
	jp2.add(test);
	jp3.add(test);

	ButtonHandler phandler = new ButtonHandler();
	test.addActionListener(phandler);
    }
    
    
	class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JOptionPane.showMessageDialog(null, "Your entered number KILOMETERS converts to MILES.", "Kilometer to Miles", JOptionPane.	INFORMATION_MESSAGE);
		}
	}

	
    public static void main(String[] args) {
        
        TabbedPane tp = new TabbedPane();
        tp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //  tp.setVisible(true);
        tp.setSize(800,800);
        tp.setVisible(true);
    }
}



I would to have a text box to enter a tempature and when you click the button it does the conversion.

How do i accomplish this?

Is This A Good Question/Topic? 0
  • +

Replies To: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 08:00 AM

This is a VERY common homework question. We can't give you working code for your school work.

Quote

I would to have a text box to enter a tempature and when you click the button it does the conversion.


This REALLY vague. It sounds like you don't know how to react to a button click or how to obtain the text from a textbox. If that's the case you need to go back a chapter or two in your textbook and re-read the material. Talking to your professor now instead of when you get further behind wouldn't be a bad idea either.

If you know how to do those things, then you are only in need of the formulas for the conversions.
They can be found here:
http://math.about.co...ulas/a/temp.htm


Please take a try at coding a solution now that you have been given a direction to research. Once you have updated your code if you are still having issues please post in this thread rather than starting a new one.

A word to our newer members that may be anxious to help:
Please help this person by NOT giving them the code to do it. So much more is learned by trial and error than by someone just handing you the answer. The OP needs to put in the effort and do some trial and error just like you did in the beginning if they are to grow as a developer.
Was This Post Helpful? 0
  • +
  • -

#3 NrTRuSH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 08:08 AM

How do i put the text box in there. I need to pack it into the window. I understand how to get the text from the textbox. Textbox is assinged a variable and i call that variable and parse it if needed from string to double. I just need to learn how to pack everything into one window. I can find help on adding a text box or adding a radio button but nothing combined together.

I also have my code for each conversion already written. I want to make it nicer.


/*
	This class will take user input of the tempature Celcius and convert it into Fahrenheit.
	It will then display the results in a Message Dialog window.
	
*
*/

import javax.swing.*;

public class celciusConversion
{
	public static void main(String[] args)
	{
		// Gets the tempature in Celcius
		
		String celTemp = JOptionPane.showInputDialog("Enter a tempature in Celcius");
		
		// Convert the Strings into a Double
		
		double celcius = Double.parseDouble(celTemp);
		
		// Output the sum into the screen
		
		double newTemp = celcius * 9/5 + 32;
		
		JOptionPane.showMessageDialog(null, "The tempature " + celcius + " Celcius converts to "  + newTemp + " degrees Fahrenheit");
		
	}

}



Was This Post Helpful? 1
  • +
  • -

#4 NrTRuSH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 08:26 AM

Okay so now how do i add this into one of the tabs?

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

/*
 * This program will do conversions
 */

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

public class conversionDEMO extends JFrame
{
	JPanel panel;
	JButton convert;
	JLabel msg;
	JTextField txtfield;

	public conversionDEMO()
	{
		// Sets the title of the frame

		setTitle("Miles to Kilometers");

		// Sets the size of the window

		setSize(300, 400);

		// Sets the action when window is close

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Sets the visibility of the window

		setVisible(true);

		// Creates the panel

		panel = new JPanel();

		// Creates a label

		msg = new JLabel("Please enter a distance in Miles: ");

		// Creates the text field

		txtfield = new JTextField(15);

		// Creates the button

		convert = new JButton("Convert");

		// Adds to the panel

		panel.add(msg);
		panel.add(txtfield);
		panel.add(convert);

		// Add the panel to the content pane

		add(panel);

		// Creates the action listener

		convert.addActionListener(new ButtonListener());

   }

	// Handles the event if a button is clicked

	public class ButtonListener implements ActionListener
   {
		public void actionPerformed(ActionEvent e)
		{
			// If ok button is clicked, gets the input name

			if(e.getSource()==convert)
			{
				// Gets the name from the text field

				String name = txtfield.getText();

				// If the text field is empty return a message

				if(name.length() == 0)
				{
					JOptionPane.showMessageDialog(null, "You didn't enter anything");
				}
				else
				{
					
					double miles = Double.parseDouble(name);
					double newTemp = miles * 1.6;
					
					JOptionPane.showMessageDialog(null, "The distance of  " +  name + " Miles in Kilometers is " + newTemp + " Kilometers");
					

				}
				txtfield.requestFocus();
			}
		}
	}

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

	}
}



Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 08:38 AM

Quote

I can find help on adding a text box or adding a radio button but nothing combined together.


I'm not a java guy, and I don't mean for this to sound "smarty assy" but if you know how to add a textbox, and you know how to add a radiobutton wouldn't you just do both of those things in succession?

I mean you say you found individual help on adding each of those things. Do you really need to find a tutorial specifically targeted to adding both of them? Can't you take the knowledge you learned from each individual tutorial and apply them both to this situation?

What happens when you need two textboxes, a radio button, a listview and a 4 pictureboxes? Are you going to scour the internet for a tutorial that targets that exact combination of controls?
Was This Post Helpful? 0
  • +
  • -

#6 NrTRuSH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 08:46 AM

I am trying stuff. Its not like im sitting here waiting for an answer but im active duty military and going to college. I don't have my whole life to spend trying to learn this and i am trying. I keep testing things. My homework is done im trying to learn alittle something extra to make it better looking. All im trying to do is get some help once i have my tabs laid out adding things inside them. Maybe if you have a link to give me that will explain more then "You need to learn more". Im simply asking for some help which im sure anyone with as much knowledge as you could simply guide me in the right direction instead of just having smart ass comments. Sorry for asking for help on here. Ive searched the site for tutorials and have read the swings ones but once again they only show a piece of what i need. When i try to piece them together its still not working but im still working on it. Ill let you know when i fix it.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 09:02 AM

I empathize. I'm ex Signal Corps. 32-G, Fixed Cryptographic Equipment Repairer.

As I said, I'm not a JAVA guy. I code in C#. I saw the question going unanswered and thought some concept advice while not being language specific would be better than no help at all and hopefully point you in the right direction.

Since java is not my primary language I'm still at a loss as to why you can't combine the information from "how to add a textbox" and "how to a radio button". Conceptually it seems like you would just do both of these. That's how it is in other languages.

Until someone can chime in with JAVA specific examples: In C# it would be like this:

TabControl tc = new TabControl(); 
TabPage tp = new TabPage(); 
Button yogi = new Button(); 
RadioButton bear = new RadioButton(); 
TextBox booboo = new Textbox();  
tp.Controls.AddRange({yogi, bear, booboo});// Add the controls to the page's controls collection 
tc.Pages.Add(tp); // Add the page to the TabControl's page's collection


I can't imagine that it is that much different for JAVA - at least in concepts.
Make new instances of the controls
Add them to the right parent or parent's container/collection of child objects.

This post has been edited by tlhIn`toq: 25 February 2012 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 NrTRuSH  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: Tabbed GUI for Farenheit to Celcius; Question on how to use GUI

Posted 25 February 2012 - 09:13 AM

Thanks for trying atleast but im trying something right now. Ill post back if it works. First ill get all my stuff to work on one window and see if i can get that atleast. Right now i can click a button, have a input box popup and do it that way. Its not as nice as having a textbox (JTextField) and entering a value and clicking a button (Convert) and having the results popup.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1