Results 1 to 8 of 8
Thread: Deselect Radio Buttons
- 03-14-2009, 05:11 AM #1
Deselect Radio Buttons
Hi!
I made a Unit Converter Program, but theres one problem...
When I run it everything works, but when i press the button clear (look in the screenshot) the text field gets cleared, but the buttons stay the same.
so what i am asking, is how whould make it so when I press clear both the text field, and the buttons get "cleared"
here's the Code for the GUI:
And here's the class that I use for converting:Java Code:/* * * A length converter */ public class ConverterGUI extends javax.swing.JFrame { private String from = ""; private String to = ""; public ConverterGUI() { initComponents(); } // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { converterPane = new javax.swing.JPanel(); outputBox = new javax.swing.JTextField(); fromLabel = new javax.swing.JLabel(); toLabel = new javax.swing.JLabel(); fromMicrometer = new javax.swing.JRadioButton(); fromMilimeter = new javax.swing.JRadioButton(); fromCentimeter = new javax.swing.JRadioButton(); fromMeter = new javax.swing.JRadioButton(); fromKilometer = new javax.swing.JRadioButton(); fromInch = new javax.swing.JRadioButton(); fromFoot = new javax.swing.JRadioButton(); fromYard = new javax.swing.JRadioButton(); fromMile = new javax.swing.JRadioButton(); convertButton = new javax.swing.JToggleButton(); toMile = new javax.swing.JRadioButton(); toYard = new javax.swing.JRadioButton(); toFoot = new javax.swing.JRadioButton(); toInch = new javax.swing.JRadioButton(); toKilometer = new javax.swing.JRadioButton(); toMeter = new javax.swing.JRadioButton(); toCentimeter = new javax.swing.JRadioButton(); toMillimeter = new javax.swing.JRadioButton(); toMicrometer = new javax.swing.JRadioButton(); clearButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); converterPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Length Converter 1.0")); fromLabel.setText("From:"); toLabel.setText("To:"); fromMicrometer.setText("Micrometer"); fromMicrometer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromMicrometerActionPerformed(evt); } }); fromMilimeter.setText("Millimeter"); fromMilimeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromMilimeterActionPerformed(evt); } }); fromCentimeter.setText("Centimeter"); fromCentimeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromCentimeterActionPerformed(evt); } }); fromMeter.setText("Meter"); fromMeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromMeterActionPerformed(evt); } }); fromKilometer.setText("Kilometer"); fromKilometer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromKilometerActionPerformed(evt); } }); fromInch.setText("Inch"); fromInch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromInchActionPerformed(evt); } }); fromFoot.setText("Foot"); fromFoot.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromFootActionPerformed(evt); } }); fromYard.setText("Yard"); fromYard.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromYardActionPerformed(evt); } }); fromMile.setText("Mile"); fromMile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fromMileActionPerformed(evt); } }); convertButton.setText("Convert!"); convertButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { convertButtonActionPerformed(evt); } }); toMile.setText("Mile"); toMile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toMileActionPerformed(evt); } }); toYard.setText("Yard"); toYard.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toYardActionPerformed(evt); } }); toFoot.setText("Foot"); toFoot.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toFootActionPerformed(evt); } }); toInch.setText("Inch"); toInch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toInchActionPerformed(evt); } }); toKilometer.setText("Kilometer"); toKilometer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toKilometerActionPerformed(evt); } }); toMeter.setText("Meter"); toMeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toMeterActionPerformed(evt); } }); toCentimeter.setText("Centimeter"); toCentimeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toCentimeterActionPerformed(evt); } }); toMillimeter.setText("Millimeter"); toMillimeter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toMillimeterActionPerformed(evt); } }); toMicrometer.setText("Micrometer"); toMicrometer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toMicrometerActionPerformed(evt); } }); clearButton.setText("Clear"); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearButtonActionPerformed(evt); } }); javax.swing.GroupLayout converterPaneLayout = new javax.swing.GroupLayout(converterPane); converterPane.setLayout(converterPaneLayout); converterPaneLayout.setHorizontalGroup( converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(converterPaneLayout.createSequentialGroup() .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(converterPaneLayout.createSequentialGroup() .addGap(84, 84, 84) .addComponent(convertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(converterPaneLayout.createSequentialGroup() .addContainerGap() .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(converterPaneLayout.createSequentialGroup() .addComponent(outputBox, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)) .addGroup(converterPaneLayout.createSequentialGroup() .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(fromMicrometer) .addComponent(fromMilimeter) .addComponent(fromLabel) .addComponent(fromCentimeter) .addComponent(fromMeter) .addComponent(fromKilometer) .addComponent(fromInch) .addComponent(fromFoot) .addComponent(fromYard) .addComponent(fromMile)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE) .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toLabel) .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toMillimeter) .addComponent(toMicrometer)) .addComponent(toMile) .addComponent(toYard) .addComponent(toFoot) .addComponent(toInch) .addComponent(toKilometer) .addComponent(toMeter) .addComponent(toCentimeter))))))) .addContainerGap()) ); converterPaneLayout.setVerticalGroup( converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(converterPaneLayout.createSequentialGroup() .addContainerGap() .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(outputBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(clearButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(fromLabel) .addComponent(toLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(converterPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, converterPaneLayout.createSequentialGroup() .addComponent(fromMicrometer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromMilimeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromCentimeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromMeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromKilometer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromInch) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromFoot) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromYard) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fromMile) .addGap(14, 14, 14)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, converterPaneLayout.createSequentialGroup() .addComponent(toMicrometer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toMillimeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toCentimeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toMeter) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toKilometer) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toInch) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toFoot) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toYard) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toMile) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(convertButton) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(converterPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(converterPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) { Converter con = new Converter(from,to,(Double.parseDouble(outputBox.getText()))); outputBox.setText(con + ""); from = ""; to = ""; } private void fromMicrometerActionPerformed(java.awt.event.ActionEvent evt) { from += "micrometers"; } private void fromMilimeterActionPerformed(java.awt.event.ActionEvent evt) { from += "milimeters"; } private void fromCentimeterActionPerformed(java.awt.event.ActionEvent evt) { from += "centimeters"; } private void fromMeterActionPerformed(java.awt.event.ActionEvent evt) { from += "meters"; } private void fromKilometerActionPerformed(java.awt.event.ActionEvent evt) { from += "kilometers"; } private void fromInchActionPerformed(java.awt.event.ActionEvent evt) { from += "inch"; } private void fromFootActionPerformed(java.awt.event.ActionEvent evt) { from += "foot"; } private void fromYardActionPerformed(java.awt.event.ActionEvent evt) { from += "mile"; } private void fromMileActionPerformed(java.awt.event.ActionEvent evt) { from += "mile"; } private void toMicrometerActionPerformed(java.awt.event.ActionEvent evt) { to += "micrometers"; } private void toMillimeterActionPerformed(java.awt.event.ActionEvent evt) { to += "milimeters"; } private void toCentimeterActionPerformed(java.awt.event.ActionEvent evt) { to += "centimeters"; } private void toMeterActionPerformed(java.awt.event.ActionEvent evt) { to += "meters"; } private void toKilometerActionPerformed(java.awt.event.ActionEvent evt) { to += "kilometers"; } private void toInchActionPerformed(java.awt.event.ActionEvent evt) { to += "inch"; } private void toFootActionPerformed(java.awt.event.ActionEvent evt) { to += "foot"; } private void toYardActionPerformed(java.awt.event.ActionEvent evt) { to += "yard"; } private void toMileActionPerformed(java.awt.event.ActionEvent evt) { to += "mile"; } private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) { to = ""; from =""; outputBox.setText(""); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ConverterGUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton clearButton; private javax.swing.JToggleButton convertButton; private javax.swing.JPanel converterPane; private javax.swing.JRadioButton fromCentimeter; private javax.swing.JRadioButton fromFoot; private javax.swing.JRadioButton fromInch; private javax.swing.JRadioButton fromKilometer; private javax.swing.JLabel fromLabel; private javax.swing.JRadioButton fromMeter; private javax.swing.JRadioButton fromMicrometer; private javax.swing.JRadioButton fromMile; private javax.swing.JRadioButton fromMilimeter; private javax.swing.JRadioButton fromYard; private javax.swing.JTextField outputBox; private javax.swing.JRadioButton toCentimeter; private javax.swing.JRadioButton toFoot; private javax.swing.JRadioButton toInch; private javax.swing.JRadioButton toKilometer; private javax.swing.JLabel toLabel; private javax.swing.JRadioButton toMeter; private javax.swing.JRadioButton toMicrometer; private javax.swing.JRadioButton toMile; private javax.swing.JRadioButton toMillimeter; private javax.swing.JRadioButton toYard; // End of variables declaration }
Java Code:/* * * The class that does all of the convertions * for the converter. */ import java.text.DecimalFormat; public class Converter { private String from; private String to; private double initial; private double converted; private double normal; private DecimalFormat dec = new DecimalFormat ("0.####"); public Converter(String f, String t, double i) { from = f; to = t; initial = i; normal = convertNormal(); } public double getInitial() { return initial; } // Puts a current measurment into centimeters. public double convertNormal() { double ans; if(from.equals("micrometers")) { ans = initial * 0.001; return ans; } else if(from.equals("milimeters")) { ans = initial * 0.1; return ans; } else if(from.equals("centimeters")) { ans = initial * 1; return ans; } else if(from.equals("meters")) { ans = initial * 100; return ans; } else if(from.equals("kilometers")) { ans = initial * 100000; return ans; } else if(from.equals("inch")) { ans = initial * 2.54; return ans; } else if(from.equals("foot")) { ans = initial * 30.48; return ans; } else if(from.equals("yard")) { ans = initial * 91.44; return ans; } else if(from.equals("mile")) { ans = initial * 160934.4; return ans; } else return -1; } // Converts the "normal" centimeters into whatever. public double convert() { double ans; if (to.equals("micrometers")) { ans = normal * 10000; return ans; } else if (to.equals("milimeters")) { ans = normal * 10; return ans; } else if (to.equals("centimeters")) { return normal; } else if (to.equals("meters")) { ans = normal * 0.01; return ans; } else if (to.equals("kilometers")) { ans = normal * 0.01; ans = ans * 0.001; return ans; } else if (to.equals("inch")) { ans = normal * 0.393700787; return ans; } else if (to.equals("foot")) { ans = normal * 0.032808399; return ans; } else if (to.equals("yard")) { ans = normal * 0.010936133; return ans; } else if (to.equals("mile")) { ans = normal * 0.010936133; ans = ans * 0.000568181818; return ans; } else return -1; } public String toString() { String ans = ""; ans += convert(); return ans; } }-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
- 03-14-2009, 06:23 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
have you try setSelected method?
-
I suggest that you add the two columns of jradiobuttons into two ButtonGroups. Then when you want to clear the selection call clearSelection() on both button groups.
- 03-14-2009, 05:58 PM #4
No, I have not tried that , thanks
-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
- 03-14-2009, 06:09 PM #5
how would i use setSelected()?
-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
- 03-15-2009, 06:51 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i agree with Fubarable
use two buttonGroup (ButtonGroup (Java Platform SE 6))
use add method of buttonGroup to add buttons
use clearSelection method to clear selection
- 03-16-2009, 08:27 AM #7
Or if you want a class that's easier to use than ButtonGroup, you can try my Select Button Group on tips4java.
Select Button Group « Java Tips Weblog
db
- 03-21-2009, 04:01 AM #8
Similar Threads
-
Radio buttons and obscure code.
By skatefreak in forum New To JavaReplies: 2Last Post: 01-20-2009, 02:24 AM -
[B]Tab Sequence problem with radio buttons[/B]
By shobha2k8 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-25-2008, 07:25 AM -
How to use Radio Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:26 PM -
radio buttons and paint
By gtraylo in forum Java AppletsReplies: 1Last Post: 04-19-2008, 12:43 PM -
property for radio buttons
By swapnanair in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-03-2007, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks