d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question
Add Reply New Topic New Poll
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Apr 19 2013 05:07pm
Trying to get this to convert inches to centimeters but it wont do help would be much appreciated

Code

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

public class WhiteBoard extends JFrame
                       implements ActionListener, TextListener
{
   private static int WIDTH = 550;
   private static int HEIGHT = 350;

   private int row = 10;
   private int col = 20;

     
   private JLabel headingL;
   private JTextField lineTF;
   private JTextArea whiteBoardTA;
   private JButton exitB, appendB;

   public WhiteBoard()
   {
       setTitle("Converter");
       Container pane  = getContentPane();
       setSize(WIDTH,HEIGHT);

       headingL = new JLabel("Converter");
       lineTF = new JTextField(20);

       whiteBoardTA = new JTextArea(row,col);
       exitB = new JButton("Exit");
       exitB.addActionListener(this);

       appendB = new JButton("Append");
       appendB.addActionListener(this);

       pane.setLayout(null);

       headingL.setLocation(50, 20);
       lineTF.setLocation(20, 100);
       whiteBoardTA.setLocation(320, 50);
       appendB.setLocation(230, 100);
       exitB.setLocation(230, 250);

       headingL.setSize(200, 30);
       lineTF.setSize(200, 30);
       whiteBoardTA.setSize(200, 200);
       appendB.setSize(80, 30);
       exitB.setSize(80, 30);

       pane.add(headingL);
       pane.add(lineTF);
       pane.add(whiteBoardTA);
       pane.add(appendB);
       pane.add(exitB);

       setVisible(true);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

   public static void main(String[] args)
   {
        WhiteBoard board = new WhiteBoard();
   }
   
   public void textValueChanged(TextEvent e) {
    if (e.getSource() == lineTF) {
    String Inches = lineTF.getText();
    double inch = Double.parseDouble(Inches);
    double cm = inch * 2.54;
    whiteBoardTA.setText (cm + " centimetres");
    }
   }
   
   public void actionPerformed(ActionEvent e)
   {
       if(e.getActionCommand().equals("Append"))
          whiteBoardTA.append(lineTF.getText());
       else if(e.getActionCommand().equals("Exit"))
           System.exit(0);
   }
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 19 2013 05:10pm
Where are you setting your TextListener? i'm not seeing it
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Apr 19 2013 05:12pm
Quote (carteblanche @ Apr 19 2013 06:10pm)
Where are you setting your TextListener? i'm not seeing it


Still extremely new to java

public void textValueChanged(TextEvent e) {
if (e.getSource() == lineTF) {
String Inches = lineTF.getText();
double inch = Double.parseDouble(Inches);
double cm = inch * 2.54;
whiteBoardTA.setText (cm + " centimetres");
}
}



/edit also thanks for fast response

This post was edited by Beat on Apr 19 2013 05:13pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 19 2013 05:30pm
Quote (Beat @ Apr 19 2013 07:12pm)
Still extremely new to java

  public void textValueChanged(TextEvent e) {
    if (e.getSource() == lineTF) {
    String Inches = lineTF.getText();
    double inch = Double.parseDouble(Inches);
    double cm = inch * 2.54;
    whiteBoardTA.setText (cm + " centimetres");
    }
  }



/edit also thanks for fast response


i didnt ask where your method is. i asked where you're setting the listener

for buttons, you did addActionListener. where is the analogous call for the textbox?
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Apr 19 2013 05:39pm
Oh alright so if I add

lineTF.addTextListener(this);

would that work? The book I'm using doesn't really go over it too well so I'm googling other codes and trying to study them to figure out how to use it
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 19 2013 06:15pm
give it a try and find out.
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Apr 19 2013 06:29pm
Quote (carteblanche @ Apr 19 2013 07:15pm)
give it a try and find out.


It didnt work :(
but I figured out a way to get it to work without the textlistener

This post was edited by Beat on Apr 19 2013 06:29pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll