9 Replies - 1200 Views - Last Post: 05 June 2011 - 05:29 PM Rate Topic: -----

#1 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Label not displaying correctly

Posted 04 June 2011 - 12:49 PM

Hi everyone I am new to Java so this might be an obvious problem that I am some how missing. The problem that I am having is an output label not displaying correctly in my second program but displays correctly in my first program. Besides my first program having a reset button, the way that the output label is used is the same as in my second program. Anyone know why the first one works fine and not the second one?

First program that works find:
import java.text.DecimalFormat;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

//import javax.swing.JOptionPane;

public class JavaSevenApplet extends Applet implements ItemListener, ActionListener 
{
	
	//applet version
	//variables
	double comRate = 0;
	double salesAmount = 0;
	String text = "";
	DecimalFormat numFormat = new DecimalFormat("$#,000.00");
	
	//add controls
	Label lblPrompt = new Label("Enter the sales amount: ");
		TextField txtAmount = new TextField(10);
	
	Label lblPrompt2 = new Label("Select the appropriate commission code: ");
		
		CheckboxGroup codeGrp = new CheckboxGroup();
	    	Checkbox telephone = new Checkbox("Telephone Sales", false, codeGrp);
	    	Checkbox inStore = new Checkbox("In-Store Sales", false, codeGrp);
	    	Checkbox outside = new Checkbox("Outside Sales", false, codeGrp);
	    	Checkbox setAllFalse = new Checkbox("",false, codeGrp);
	    
	Label lbloutput = new Label("Click an option button to calculate the sales amount");
	Button reset = new Button("Reset");   
	//initialize applet with controls
	public void init()
	{
		add(lblPrompt);
		add(txtAmount);
		add(lblPrompt2);
		add(telephone);
		add(inStore);
		add(outside);
		add(lbloutput);
		add(reset);
		
		//add action listener to check button
		reset.addActionListener(this);
		
		//add item listener to check state of checkbox
		telephone.addItemListener(this);
		inStore.addItemListener(this);
		outside.addItemListener(this);
	}
	
	public void itemStateChanged(ItemEvent choice)
	{
		try
		{
			
			//test for null exception
			text = txtAmount.getText();
			if(text == null)
				throw new NumberFormatException();
			
			salesAmount = Double.parseDouble(text);
			if(salesAmount <= 0)
				throw new NumberFormatException();
			
			lbloutput.setText("Click an option button to calculate the sales amount");
			
			if(telephone.getState() == true)
				comRate = .10;
			
			if(inStore.getState() == true)
				comRate = .14;
			
			if (outside.getState() == true)
				comRate = .18;
						
			lbloutput.setText("Your commission on sales of " + numFormat.format(salesAmount) + "is " + numFormat.format(salesAmount*comRate)+ "\n");
			
		}
		catch(NumberFormatException e)
		{
			lbloutput.setText("Error! Input a numeric value greater than 0!");
			
		}
	}//end of item change
	
	public void actionPerformed(ActionEvent e)
	{
		setAllFalse.setState(true);
		lbloutput.setText("Click an option button to calculate the sales amount");
		txtAmount.setText(null);
	}//end of button
}//end of class



Second program which does not display correctly
public class JavaEight extends Applet implements ItemListener
{
	double num = 0;
	double num2 = 0;
	String text = "";
	
	DecimalFormat numFormat = new DecimalFormat("$#,###.00");
	//create applet controls
	//create group so only one can be checked at a time
	CheckboxGroup grp = new CheckboxGroup();
		Checkbox chk1 = new Checkbox("Annual Rate", false, grp);
		Checkbox chk2 = new Checkbox("Twice a Year Rate", false, grp);
		Checkbox chk3 = new Checkbox("Monthly Rate", false, grp);
		Checkbox hidden = new Checkbox("", false, grp);
		
	//prompt
	Label prompt2 = new Label("Check the payment plan: ");
	
	//output 
	Label output = new Label("Click one of the options to select!");
	
	//text field
	TextField txtBox1 = new TextField(10);
		
	//initialize applet with controls
	public void init()
	{
		add(prompt2);
		add(txtBox1);
		add(chk1);
		add(chk2);
		add(chk3);
		add(output);
		
		chk1.addItemListener(this);
		chk2.addItemListener(this);
		chk3.addItemListener(this);
		
	}//end of init()
	
	//create method to handle items
	public void itemStateChanged(ItemEvent e)
	{
		//radio buttons
		try
		{
			text = txtBox1.getText();
			num2 = Double.parseDouble(text);
			
			if(num2 <=0)
				throw new NumberFormatException();
			
			if(chk1.getState() == true)
				num = .07;
						
			if(chk2.getState() == true)
				num = .09;
						
			if(chk3.getState() == true)
				num = .10;
		
			output.setText("You will owe "+ text+" at 10% interest which is " + numFormat.format(((Integer.parseInt(txtBox1.getText())*num))));
		}
		catch(NumberFormatException ex)
		{
			output.setText("Error!");
		}
				
	}//end of method
			
}//end of main class




Is This A Good Question/Topic? 0
  • +

Replies To: Label not displaying correctly

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,316
  • Joined: 27-December 08

Re: Label not displaying correctly

Posted 04 June 2011 - 12:50 PM

Can you be more specific as to the discrepancy? Telling us that it isn't displaying correctly doesn't help us.
Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Label not displaying correctly

Posted 04 June 2011 - 01:07 PM

The second one displays about half of the updated information until I to try re-size the window even though I made the window big enough before I input any data.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Label not displaying correctly

Posted 04 June 2011 - 06:28 PM

It is actionListener that you want on your CheckBox not itemListener
And use JComponents: JApplet, JCheckBox,...
Was This Post Helpful? 2
  • +
  • -

#5 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Label not displaying correctly

Posted 05 June 2011 - 10:36 AM

Thanks for the information. Right now I am following a book and it has not gotten into JComponents yet. I also don't know why the book tells me to implement ItemListener for checkboxes. It does tell me to use ActionListener for buttons.
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Label not displaying correctly

Posted 05 June 2011 - 10:45 AM

Well if your book isnt working directly in JComponents, I would bin it. Your using the Abstract Window Toolkit, which is old and hardly used anymore. Infact, most of it has problably depreciated by now. Here is an example of a JCheckBox using an ActionListener
public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
        boolean selected = abstractButton.getModel().isSelected();
        System.out.println(selected);
        // abstractButton.setText(newLabel);
      }
    };
    aCheckBox4.addActionListener(actionListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }


There are many ways you could listen to events. Nothing wrong with using an ItemListener, I think its more a matter of preference and needs.
Was This Post Helpful? 1
  • +
  • -

#7 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Label not displaying correctly

Posted 05 June 2011 - 11:20 AM

I didn't like the book to begin with. It was from an online intro to Java class that I took 2 years ago. Comparing it to the C++ books that I have used, it is not as thorough. Thanks for the information. I guess I'll find some Java documentation online instead of continuing with the book.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Label not displaying correctly

Posted 05 June 2011 - 12:03 PM

View PostseeP+, on 05 June 2011 - 01:36 PM, said:

Thanks for the information. Right now I am following a book and it has not gotten into JComponents yet. I also don't know why the book tells me to implement ItemListener for checkboxes. It does tell me to use ActionListener for buttons.

Your book must be pretty old and JCheckBox ARE buttons
Was This Post Helpful? 0
  • +
  • -

#9 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Label not displaying correctly

Posted 05 June 2011 - 05:22 PM

Java Programming Complete Concepts and Techniques 3rd Edition Copyright 2006
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Label not displaying correctly

Posted 05 June 2011 - 05:29 PM

View PostseeP+, on 05 June 2011 - 08:22 PM, said:

Java Programming Complete Concepts and Techniques 3rd Edition Copyright 2006

at least 5 years old... that is an eternity in IT world. Find a newer book and jump to Swing (JComponent) right now
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1