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();
}
}
}
}