8 Replies - 2582 Views - Last Post: 22 February 2011 - 10:40 AM Rate Topic: -----

#1 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

need help with submit button and combo box.

Posted 22 February 2011 - 07:09 AM

as the title states, I need help with my combo box and submit button. I am making a program that will convert between giving choices. I was wanting to use a combo box that when the user selects what he wants to convert, it calls the class file given. I have made all the classes that needs to be converted, but I don't know how to when the user selects what he wants to convert and hits the submit button it reads the class file?

Here are the classes that I am talking about:

import java.io.*;
import java.lang.*;

//decimal to octal

public class DecimalToOctal 
{
  public static void main(String[] args) throws IOException
{
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,8);
    System.out.println("octal:=" + str);
  }
} 

//octal to decimal

public class  OctalToDecimal
{
  public static void main(String[] args)throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the octal number:");
    String oct = reader.readLine();
    int i= Integer.parseInt(oct,8);
    System.out.println("Decimal:=" + i);
  }
}  

//decimal to hexadecimal

public class DecimalToHexadecimal
{
  public static void main(String[] args) throws IOException
{  
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the decimal value:");
    String hex = bf.readLine();
    int i = Integer.parseInt(hex);
    String hex1 = Integer.toHexString(i);
    System.out.println("Hexa decimal: " + hex1);
  }
} 

//hexadecimal to decimal

public class  HexadecimalToDecimal
{
  public static void main(String[] args) throws IOException
{
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the Hexadecimal number:");
    String str= bf.readLine();
    int i= Integer.parseInt(str,16);
    System.out.println("Decimal:="+ i);
    }
} 



**SCREENSHOT**

Posted Image

Posted Image

I also attached my netbeans project if it helps anyone. It runs and it will give you an example of what I am trying to explain.

Attached File(s)


This post has been edited by bedrockhorror: 22 February 2011 - 07:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need help with submit button and combo box.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:11 AM

Please post your code in the body of the thread, using code tags.

Quote

it calls the class given

What class? Please be more specific in your description of your errors.
Was This Post Helpful? 0
  • +
  • -

#3 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:25 AM

ok I put the code in for the classes I was talking about, so any help will be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:32 AM

Why do each of your classes have a main() method? Why don't you create a utility class with static methods for conversion:
public class NumberBaseConverter{

    public static String convert(int number, int baseCurrent, int baseTo){
           //code
    }

}


Was This Post Helpful? 0
  • +
  • -

#5 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:36 AM

I done that so that way when the user selects what he wants it will read the file independently. Each class file is a .java file, so the DecimalToOctal class is DecimalToOctal.java and I was wanting the program to read that file when he selected it.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:41 AM

Your number base conversion should be independent of your files. Read in the data, and invoke the appropriate method. You can use the design for one method for any number base conversion, or you can write your own individual methods. But you should not have individual classes as you currently have, all with main() methods. There should be one class with methods.
Was This Post Helpful? 0
  • +
  • -

#7 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: need help with submit button and combo box.

Posted 22 February 2011 - 07:46 AM

ok, thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#8 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: need help with submit button and combo box.

Posted 22 February 2011 - 10:14 AM

when I tick the radio buttons to select what I am converting to/from, it selects it and also puts what's selected inside the jtextfield1 and jtextfield2 boxes. I was needing to know how I can fix this.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConverterGUIwindow.java
 *
 * Created on Feb 13, 2011, 8:21:30 AM
 */

package Convertergui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author WCCStudent
 */


public class ConverterGUIWindow extends javax.swing.JFrame
        implements ActionListener {

    /** Creates new form ConverterGUIWindow */
    public ConverterGUIWindow() {
        initComponents();
        jTextField1.setText("");
        jTextField2.setText("");
        // MES addActionListener for each radio button
        jRadioButton1.addActionListener((ActionListener) this);
        jRadioButton2.addActionListener((ActionListener) this);
        jRadioButton3.addActionListener((ActionListener) this);
        jRadioButton4.addActionListener((ActionListener) this);
        jRadioButton5.addActionListener((ActionListener) this);
        jRadioButton6.addActionListener((ActionListener) this);


        }

        // MES Added method to catch the event.
    public void actionPerformed(ActionEvent e) {
    jTextField1.setText(e.getActionCommand());
    jTextField2.setText(e.getActionCommand());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Decimal,Octal,Hexadecimal Converter Program");

        jLabel2.setText("Base Number to Convert: ");

        jLabel3.setText("Converted Number: ");

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");

        jButton1.setText("Clear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Convert");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        converter = new Converter();
        String text1 = jTextField1.getText();
        String text2 = jTextField2.getText();
        if ( text1.equals("") &&  text2.equals(""))
        {
            String message = "Please enter a octal,hexadecimal, or decimal "
                    + "number";
            JOptionPane.showMessageDialog(null, message);
        } else if (!(text1.equals("")) && !(text2.equals("")))
        {
            String message = "One field must be cleared to convert";
            JOptionPane.showMessageDialog(null, message);
        } else if (!(text1.equals("")))
        {
            //Convert to decimal
            // Check for binary number
            converter = new Converter();
            converter.setBinaryNumber(jTextField1.getText());
            int num = converter.getDecimalNumber();
            jTextField2.setText(Integer.toString(num));

        } else if (!(text2.equals("")))
        {
            // Convert to binary
            // Check for positive int
            converter = new Converter();
            int num = Integer.parseInt(jTextField2.getText());
            converter.setDecimalNumber(num);
            jTextField1.setText(converter.getBinaryNumber());

        }
    String testHex = "0123456789ABCDEF";
       if (testHex.matches("[0-9A-F]+"))
          {
             System.out.println(testHex + " is a hex ");
          }
       else
          {
             System.out.println(testHex + " is not a hex ");
          }
   String testOct = "0123456789ABCDEF";
       if (testOct.matches("[0-7]+"))
          {
             System.out.println(testOct + " is a octal Number ");
          }
       else
          {
             System.out.println(testOct + " is not a octal Number ");
          }

   String testDec = "0123456789ABCDEF";
       if (testDec.matches("[0-9A-F]+"))
          {
             System.out.println(testDec + " is a Decimal ");
          }
       else
          {
             System.out.println(testDec + " is not a Decimal ");
          }

    }                                        

    


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jTextField1.setText("");
        jTextField2.setText("");
    }                                        


    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
    private Converter converter;
}




**screenshots**
Posted Image

Attached File(s)


This post has been edited by bedrockhorror: 22 February 2011 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: need help with submit button and combo box.

Posted 22 February 2011 - 10:40 AM

Duplicate threads merged. Please avoid duplicate posting.

Also, we do not support NetBeans GUI Builder code b/c it is horrid, unreadable, and unmaintainable for humans. Write your GUI Code by hand.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1