Problem with radio button loop in temperature conversion program.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1451 Views - Last Post: 07 August 2013 - 09:58 AM Rate Topic: -----

#16 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 04:40 PM

String degree = "\u00b0";

Was This Post Helpful? 0
  • +
  • -

#17 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 07:37 PM

Still unable to get my panels, buttons, and labels to look like the one on the other page. Approaching 12 hours spent on this project.
Was This Post Helpful? 0
  • +
  • -

#18 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Problem with radio button loop in temperature conversion program.

Posted 02 August 2013 - 05:40 AM

How does the app know when the user wants to perform the conversion?
Was This Post Helpful? 0
  • +
  • -

#19 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Problem with radio button loop in temperature conversion program.

Posted 02 August 2013 - 06:20 AM

View PostDubyer, on 02 August 2013 - 02:37 AM, said:

Still unable to get my panels, buttons, and labels to look like the one on the other page. Approaching 12 hours spent on this project.


I don't know too much about Java and Swing, but it seems like you can achieve the wanted look by using gridlayouts like you are already using in your code.
Was This Post Helpful? 0
  • +
  • -

#20 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Problem with radio button loop in temperature conversion program.

Posted 02 August 2013 - 06:48 AM

View PostDubyer, on 02 August 2013 - 02:37 AM, said:

Still unable to get my panels, buttons, and labels to look like the one on the other page. Approaching 12 hours spent on this project.


So now it's a layout problem? Use a combination of borderlayout and gridlayout. Have four panels for BorderLayout(PAGE_START and PAGE_END for Top and Bottom. LINE_START and LINE_END for left and right.) Put the components that belong in each panel in GridLayout. It should make this a lot easier for you.
Was This Post Helpful? 0
  • +
  • -

#21 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 04:31 PM

I practically reworked my whole code. Still having trouble with the positioning of the labels. Can't seem to put them in the right place without something else disappearing from the frame.
// Program converts temperatures.
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class ConvertFrame extends JFrame {
private JPanel convertFrom = new JPanel(); 
private JPanel convertTo = new JPanel();
private JLabel label1 = new JLabel("Input");
private JLabel label2 = new JLabel("Output");
private JLabel label3 = new JLabel("Input Scale"); 
private JLabel label4 = new JLabel("Output Scale");
private JTextField temperature1 = new JTextField(5);
private JTextField temperature2 = new JTextField(5);
private ButtonGroup radioFrom = new ButtonGroup(); 
private ButtonGroup radioTo = new ButtonGroup();
private JRadioButton celciusBoxTo = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxTo = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxTo = new JRadioButton("Kelvin");
private JRadioButton celciusBoxFrom = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxFrom = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxFrom = new JRadioButton("Kelvin");

// set up GUI
public ConvertFrame()
{
super("Converter");

fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
celciusBoxFrom = new JRadioButton( "Celcius", false );
kelvinBoxFrom = new JRadioButton( "Kelvin", false );
JPanel radioFrom = new JPanel();
radioFrom.setLayout(new GridLayout(3, 1));
radioFrom.add(fahrenheitBoxFrom);
radioFrom.add(celciusBoxFrom);
radioFrom.add(kelvinBoxFrom);
add(radioFrom, BorderLayout.WEST);


fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
celciusBoxTo = new JRadioButton( "Celcius", true );
kelvinBoxTo = new JRadioButton( "Kelvin", false );
JPanel radioTo = new JPanel();
radioTo.setLayout(new GridLayout(3, 1));
radioTo.add( fahrenheitBoxTo );
radioTo.add( celciusBoxTo );
radioTo.add( kelvinBoxTo );
add(radioTo, BorderLayout.EAST);


ButtonGroup convertFrom = new ButtonGroup();
convertFrom.add( fahrenheitBoxFrom );
convertFrom.add( celciusBoxFrom );
convertFrom.add( kelvinBoxFrom );


ButtonGroup convertTo = new ButtonGroup();
convertTo.add(fahrenheitBoxTo);
convertTo.add(celciusBoxTo);
convertTo.add(kelvinBoxTo);


JPanel label1 = new JPanel();
label1.setLayout(new BorderLayout(5,0));
label1.add(new JLabel("Input"), BorderLayout.WEST);
label1.add(temperature1, BorderLayout.CENTER);
temperature1.setHorizontalAlignment(JTextField.CENTER);
add(label1, BorderLayout.NORTH);

JPanel label2 = new JPanel();
label2.setLayout(new BorderLayout(5,0));
label2.add(new JLabel("Output"), BorderLayout.WEST);
label2.add(temperature2, BorderLayout.CENTER);
temperature2.setHorizontalAlignment(JTextField.CENTER);
temperature2.setEditable(false);
add(label2, BorderLayout.SOUTH);

setSize( 300, 200 );
setVisible( true );

temperature1.addActionListener(

new ActionListener() { // anonymous inner class

// perform conversions
public void actionPerformed( ActionEvent event )
{
double convertTemp, temp;
DecimalFormat df = new DecimalFormat("0.00");


temp = Integer.parseInt( ( ( JTextField )
event.getSource() ).getText() );

// fahrenheit to celcius
if ( fahrenheitBoxFrom.isSelected() &&
celciusBoxTo.isSelected() ) {
convertTemp = ( double ) ( 5.0 / 9.0 * ( temp - 32 ) );
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp)) +" C");
}
// fahrenheit to kelvin
else if ( fahrenheitBoxFrom.isSelected() &&
kelvinBoxTo.isSelected() ) {
convertTemp = ( double )
( 5.0 / 9.0 * ( temp - 32 ) + 273 );
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp)) +" K");
}

// celcius to fahrenheit
else if ( celciusBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected() ) {
convertTemp = ( double ) ( 9.0 / 5.0 * temp + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp))+" F");
}

// celcius to kelvin
else if ( celciusBoxFrom.isSelected() &&
kelvinBoxTo.isSelected() ) {
convertTemp = temp + 273;
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp)) + " K");
}

// kelvin to celcius
else if ( kelvinBoxFrom.isSelected() &&
celciusBoxTo.isSelected() ) {
convertTemp = temp - 273;
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp))+ " C");
}

// kelvin to fahrenheit
else if ( kelvinBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected() ) {
convertTemp = ( double ) ( 5.0 /9.0 * ( temp - 273 ) + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
temperature2.setText(" "+ (df.format(convertTemp)) + " F");
}

} // end method actionPerformed

} // end anonymous inner class

); // end call to addActionListener


}} // end constructor






Attached Image I need my display to look like this.
Attached Image This is what my display looks like at the moment.
Was This Post Helpful? 0
  • +
  • -

#22 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 04:36 PM

Can a mod change the title of this post to "Help with position labels within a frame." I got past my initial question, and just need help with some final touches.
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3358
  • View blog
  • Posts: 11,371
  • Joined: 12-December 12

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 04:39 PM

POST deleted

This post has been edited by andrewsw: 06 August 2013 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#24 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 04:47 PM

When I try to tack on label3 and label4 in respect with the radio buttons. My radio buttons disappear off the screen.

JPanel label3 = new JPanel();
label3.setLayout(new BorderLayout(5,0));
label3.add(new JLabel("Input Scale"), BorderLayout.WEST);
add(label3, BorderLayout.WEST);

JPanel label4 = new JPanel();
label4.setLayout(new BorderLayout(5,0));
label4.add(new JLabel("Output Scale"), BorderLayout.EAST);
add(label4, BorderLayout.EAST);



Attached ImageWhen I added label3 and label4 into the program, this is what happened to the frame.

This post has been edited by Dubyer: 06 August 2013 - 04:49 PM

Was This Post Helpful? 0
  • +
  • -

#25 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 05:03 PM

I changed the type of layout from border to flow, and it looks better now. Still need to find a way to shop the radio buttons.
JPanel label3 = new JPanel(new FlowLayout());
label3.add(new JLabel("Input Scale"), BorderLayout.WEST);
add(label3, BorderLayout.WEST);

JPanel label4 = new JPanel(new FlowLayout());
label4.add(new JLabel("Output Scale"), BorderLayout.EAST);
add(label4, BorderLayout.EAST);


Attached Image This is what it looks like now.
Was This Post Helpful? 0
  • +
  • -

#26 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 06:36 PM

I need one final push to finish this program. I need to align my 2 sets of radio buttons to the left and to the right.
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class ConvertFrame extends JFrame {
private JPanel convertFrom = new JPanel(); 
private JPanel convertTo = new JPanel();
private JLabel label1 = new JLabel("Input");
private JLabel label2 = new JLabel("Output");
private JLabel label3 = new JLabel("Input Scale"); 
private JLabel label4 = new JLabel("Output Scale");
private JTextField temperature1 = new JTextField(5);
private JTextField temperature2 = new JTextField(5);
private ButtonGroup radioFrom = new ButtonGroup(); 
private ButtonGroup radioTo = new ButtonGroup();
private JRadioButton celciusBoxTo = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxTo = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxTo = new JRadioButton("Kelvin");
private JRadioButton celciusBoxFrom = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxFrom = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxFrom = new JRadioButton("Kelvin");

// set up GUI
public ConvertFrame()
{
super("Converter");

fahrenheitBoxFrom = new JRadioButton("Fahrenheit", true);
celciusBoxFrom = new JRadioButton("Celcius", false);
kelvinBoxFrom = new JRadioButton("Kelvin", false);
JPanel radioFrom = new JPanel();
radioFrom.setLayout(new GridLayout(3, 1));
radioFrom.add(celciusBoxFrom);
radioFrom.add(fahrenheitBoxFrom);
radioFrom.add(kelvinBoxFrom);
add(radioFrom);


fahrenheitBoxTo = new JRadioButton("Fahrenheit", false);
celciusBoxTo = new JRadioButton("Celcius", true);
kelvinBoxTo = new JRadioButton("Kelvin", false);
JPanel radioTo = new JPanel();
radioTo.setLayout(new GridLayout(3, 1));
radioTo.add(celciusBoxTo);
radioTo.add(fahrenheitBoxTo);
radioTo.add(kelvinBoxTo);
add(radioTo);


ButtonGroup convertFrom = new ButtonGroup();
convertFrom.add(celciusBoxFrom);
convertFrom.add(fahrenheitBoxFrom);
convertFrom.add(kelvinBoxFrom);



ButtonGroup convertTo = new ButtonGroup();
convertTo.add(celciusBoxTo);
convertTo.add(fahrenheitBoxTo);
convertTo.add(kelvinBoxTo);


JPanel label1 = new JPanel();
label1.setLayout(new FlowLayout());
label1.add(new JLabel("Input"), BorderLayout.WEST);
label1.add(temperature1, BorderLayout.CENTER);
temperature1.setHorizontalAlignment(JTextField.CENTER);
add(label1, BorderLayout.NORTH);


JPanel label2 = new JPanel();
label2.setLayout(new FlowLayout());
label2.add(new JLabel("Output"), BorderLayout.WEST);
label2.add(temperature2, BorderLayout.CENTER);
temperature2.setHorizontalAlignment(JTextField.CENTER);
temperature2.setEditable(false);
add(label2, BorderLayout.SOUTH);


JPanel label3 = new JPanel(new FlowLayout());
label3.add(new JLabel("Input Scale"), BorderLayout.WEST);
add(label3, BorderLayout.WEST);


JPanel label4 = new JPanel(new FlowLayout());
label4.add(new JLabel("Output Scale"), BorderLayout.EAST);
add(label4, BorderLayout.EAST);


setSize( 300, 200 );
setVisible( true );

temperature1.addActionListener(

new ActionListener() { // anonymous inner class

// perform conversions
public void actionPerformed( ActionEvent event )
{
double convertTemp, temp;
DecimalFormat df = new DecimalFormat("0.00");


temp = Integer.parseInt( ( ( JTextField )
event.getSource() ).getText() );

// fahrenheit to celcius
if ( fahrenheitBoxFrom.isSelected() &&
celciusBoxTo.isSelected()) 
{
convertTemp = (double) ((5.0/9.0)*(temp-32));
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) +" C");
}
// fahrenheit to kelvin
else if ( fahrenheitBoxFrom.isSelected() &&
kelvinBoxTo.isSelected()) 
{
convertTemp = (double)((5.0/9.0)*(temp-32)+273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) +" K");
}

// celcius to fahrenheit
else if ( celciusBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected()) 
{
convertTemp = (double) ((9.0/5.0)*temp+32);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp))+" F");
}

// celcius to kelvin
else if ( celciusBoxFrom.isSelected() &&
kelvinBoxTo.isSelected()) 
{
convertTemp = (temp+273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) + " K");
}

// kelvin to celcius
else if ( kelvinBoxFrom.isSelected() &&
celciusBoxTo.isSelected())
{
convertTemp = (temp-273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp))+ " C");
}

// kelvin to fahrenheit
else if ( kelvinBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected()) 
{
convertTemp = (double) ((5.0/9.0)*(temp-273)+32);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) + " F");
}

} // end method actionPerformed

} // end anonymous inner class

); // end call to addActionListener


}} // end constructor




Attached Image
Attached Image So close to being the same!!
Was This Post Helpful? 0
  • +
  • -

#27 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 07:30 PM

Use a GridLayout(6,1) // 6 lines 1 column
make row one put a GridLayout(1,3)
first and third element an empty JLabel
center element a GridLayout(1,2) with a Jlabel for "Input" and the JTextField

The 4 next line GridLayout(1,3) with the header or the JRadioButton with an empty JLabel in the center

Last row as the first one
Was This Post Helpful? 0
  • +
  • -

#28 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 06 August 2013 - 09:03 PM

View Postpbl, on 06 August 2013 - 07:30 PM, said:

Use a GridLayout(6,1) // 6 lines 1 column
make row one put a GridLayout(1,3)
first and third element an empty JLabel
center element a GridLayout(1,2) with a Jlabel for "Input" and the JTextField

The 4 next line GridLayout(1,3) with the header or the JRadioButton with an empty JLabel in the center

Last row as the first one


I'm sorry, but I don't understand what you mean.
Was This Post Helpful? 0
  • +
  • -

#29 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Problem with radio button loop in temperature conversion program.

Posted 07 August 2013 - 03:24 AM

You can make things much easier for yourself by making one class for your scale chooser. The two instances would differ only in their captions.

btw you didn't answer the question i asked you
Was This Post Helpful? 0
  • +
  • -

#30 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with radio button loop in temperature conversion program.

Posted 07 August 2013 - 03:46 AM

View PostDubyer, on 07 August 2013 - 12:03 AM, said:

I'm sorry, but I don't understand what you mean.

Or even easier un GridLayout(6,3)...
      JPanel p = new JPanel(new GridLayout(6,3));
      // first line
      p.add(new JLabel(""));
      // in the center
      JPanel bTop = new JPanel(new GridLayout(1,2));
      bTop.add(new JLabel("Input:");
      bTop.add(inputJtextField);
      p.add(bTop);
      p.add(new JLabel("");

      // second line
      p.add(new JLabel("Input Scale");
      p.add(new JLabel("");
      p.add(new JLAbel("Output Scale");

      // third line
      p.add(radioCelsusIn);
      p.add(new JLabel("");
      p.add(radioCelsusOut);

      // 4 and 5 the same but with Farenheight and Kelvin

      // 6 as row one


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3