I want to have a changing output in the 2nd textfield that is 'output' in my Action class as I enter input in the 'input'textfield. I don't know why it doesn't give any output... pls help

The comboBoxes convertTo---contains "IEEE" and "DEC" while precisions contain "Long", "Single", "Double".

Any advice or tips will be appreciated! :))

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

public class Action implements DocumentListener, ItemListener{
    public JTextField output;
    public JTextField input;
    public JComboBox convertTo, precisions;
    public String toForm, prec, in;
    public char preci;
        public void insertUpdate(DocumentEvent de) {
            getIn();
        }
        public void removeUpdate(DocumentEvent de) {
            getIn();
        }
        public void changedUpdate(DocumentEvent de) {
            getIn();
        }

        public void itemStateChanged(ItemEvent ie) {
            if(ie.getStateChange() == 1) {
                if((JComboBox)ie.getSource() == convertTo)
                    toForm = (String)convertTo.getSelectedItem();
                else if((JComboBox)ie.getSource() == precisions) {
                    prec = (String)precisions.getSelectedItem();
                    preci = prec.charAt(0); }
            getIn();
            }
        }

        public boolean checkInput(String conv, char pr, String txt) {
            boolean ch = false;
            if(conv.equals("DEC") && txt.matches("^[0-9A-F]+$")) {
                if((pr == 'L' && txt.length() == 4) || (pr == 'S' && txt.length()==8) || (pr == 'D' && txt.length() == 16))
                    ch =true;
            }
            else if(conv.equals("IEEE") && txt.matches("-?[0-9]+[.][0-9]+")){
                System.out.println("true pa dito");
                    int whole = Integer.parseInt(txt.substring(0, txt.indexOf(".")));
                    double frac = Double.parseDouble("0." + txt.substring(txt.indexOf(".")+1));
                    System.out.println(whole+ " " + frac);
                    if((whole < 32767) &&((frac == 0.0) || frac < 0.00000000001)){
                        System.out.println("true");
                        ch= true;
                    }
            }
            return ch;
        } 

        public void getIn() {
            in = input.getText();
            if((toForm != null) && (preci != ' ') && (in != " ") && (in != null)) {
                if(checkInput(toForm, preci, in)) {
                    input.setForeground(Color.BLACK);
                    if(toForm.equals("DEC")) {
                    System.out.println(toForm + " " + preci + " " + in); 
                        output.setText("hi");
                        output.repaint();}
                    else if(toForm.equals("IEEE")) { 
                        output.repaint();
                        output.setText("hello");}
                }
                else
                    input.setForeground(Color.RED);
                    output.setText(null);
                    output.repaint();
            }
            else {
                if(in != "") {
                input.setForeground(Color.RED);
                output.setText(null);
                output.repaint();
                }
            }

        }


}

Recommended Answers

All 5 Replies

You have a good scattering of print statements there, so can you tell us which code is actually being called when? Eg does the document listener interface correctly call getIn() each time you type in the text field?

Yes. The input goes in the respective loop but the textfield 'output' prints nothing. I realized that in the 2nd if-else loop, the else loop doesn't have brackets, that's why (how careless)... it is already solved :))

Nice catch!
Personally I click the "format" menu item in Eclipse and NetBeans after typing each block, and after any line deletions/insertions. It's surprising how often the "official" indentation doesn't match my intentions. (Mind you, I may be the world's worst typpist.)

I put print statements to make sure of the tracking in the program. :))

Yes, absolutely, a very good thing to do. The advantage of also running format is that you spot errors immediately, without having to run and interpret test cases. It's a fundamental part of the philosophy of Java that you should try to prevent errors at compile time rather than wait until you run things - hence strict typing, uninitialised variable checks etc.
Way way back in my IBM days we had a rule of thumb that given 7 stages of OS development
spec / design / code / unit test / system test / user test / deployment
the cost of finding and fixing an error increases by a factor of 10x at each stage. Now maybe that should be 4x ? but either way the moral is to fix things at the earliest possible stage.

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.