Results 1 to 9 of 9
  1. #1
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default StringBuilder/ActionEvent help!

    I am new to Java and am trying to create a program that uses GridLayout, panels, has 2 labels and buttons across the top to enter a starting and end number, a button in the center to make the program calculate all of the numbers divisible by 2, and a display box centered at the bottom that uses scroll bars when necessary to display all the numbers divisible by 2 within the user defined range.

    Below is my code. I would appreciate some help resolving the errors. I am not sure what I did wrong or how to solve these errors at all. I am required to use StringBuilder, setText(), and toString(). Thank you in advance for any pointers and advice on this!!

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

    public class DivisibleByTwoGUI extends JFrame implements ActionListener{

    private final int HEIGHT = 450;
    private final int WIDTH = 600;

    private JLabel lblStartNum, lblEndNum, lblBlank;
    private JTextField txtStartNum, txtEndNum;
    private JButton btnShow;
    private JScrollPane scrollResults;
    private JTextArea txtareaResults;
    private JPanel p1, p2, p3, p4;

    private String start, end;
    private int startInt, endInt;


    public DivisibleByTwoGUI(String title){
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(HEIGHT, WIDTH);

    setLayout(new GridLayout(1,1));

    p1 = new JPanel();
    p2 = new JPanel();
    p3 = new JPanel();
    p4 = new JPanel();

    p1.setLayout(new GridLayout(3,1));
    p2.setLayout(new FlowLayout());
    p3.setLayout(new GridLayout(1,3));
    p4.setLayout(new GridLayout(1,3));

    p2.add(p1);
    p3.add(p1);
    p4.add(p1);
    add(p1);

    p2.add(lblStartNum);
    p2.add(txtStartNum);
    p2.add(lblEndNum);
    p2.add(txtEndNum);

    p3.add(lblBlank);
    p3.add(btnShow);
    p3.add(lblBlank);

    txtareaResults = new JTextArea(5,30);
    txtareaResults.setEditable(false);
    scrollResults = new JScrollPane(txtareaResults);

    p4.add(lblBlank);
    p4.add(scrollResults);
    p4.add(lblBlank);

    btnShow = new JButton("Show numbers divisible by 2");
    btnShow.addActionListener(this);

    }

    public void actionPerformed(ActionEvent ae){
    StringBuilder str = new StringBuilder();
    if(event.getSource() == btnShow){

    String start = txtStartNum.getText();
    String end = txtEndNum.getText();
    startInt = Integer.parseInt(start);
    endInt = Integer.parseInt(end);

    if(StartInt <= EndInt){
    StartInt = StartInt + EndInt;
    EndInt = StartInt - EndInt;
    StartInt = StartInt - EndInt;
    }

    while(StartInt <= EndInt){
    if(StartInt % 2 == 0){
    str.append(StartInt + "\n");
    StartInt++;
    }
    }
    }
    txtareaResults.setText(str.toString());
    }

    }

    ----------------------------------------------------------------------
    Below is seperate code to test with:

    public class DivisibleByTwoGUITest{
    public static void main(String[] args){

    DivisibleByTwoGUI screen = new DivisibleByTwoGUI("Divisible by Two");
    screen.setSize(600,450);
    screen.setVisible(true);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What errors are you having? And which lines are causing them?

    Take each error one at a time, and try to fix it based on what the error message is telling you. For instance, if it is telling you that you're using a variable, that has not been declared, such as "event", then your error is in fact just what the compiler is telling you it is. Perhaps you want to call getSource() on something else, on a variable or parameter variable (hint hint). Also, please be very careful with spelling and capitalization as both matter.
    Last edited by Fubarable; 12-06-2010 at 01:53 AM.

  3. #3
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Below are the errors I am getting. What I am having trouble with is why it is telling me that there are problems with the operators... Is there something I am not seeing here? Thanks.

    --------------------Configuration: <Default>--------------------
    F:\Java\DivisibleByTwoGUI.java:72: cannot find symbol
    symbol : variable event
    location: class DivisibleByTwoGUI
    if(event.getSource() == btnShow){
    ^
    F:\Java\DivisibleByTwoGUI.java:81: operator <= cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    if(txtStartNum <= txtEndNum){
    ^
    F:\Java\DivisibleByTwoGUI.java:82: operator + cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    txtStartNum = txtStartNum + txtEndNum;
    ^
    F:\Java\DivisibleByTwoGUI.java:83: operator - cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    txtEndNum = txtStartNum - txtEndNum;
    ^
    F:\Java\DivisibleByTwoGUI.java:84: operator - cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    txtStartNum = txtStartNum - txtEndNum;
    ^
    F:\Java\DivisibleByTwoGUI.java:87: operator <= cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    while(txtStartNum <= txtEndNum){
    ^
    F:\Java\DivisibleByTwoGUI.java:88: operator % cannot be applied to javax.swing.JTextField,int
    if(txtStartNum % 2 == 0){
    ^
    F:\Java\DivisibleByTwoGUI.java:90: operator ++ cannot be applied to javax.swing.JTextField
    txtStartNum++;
    ^
    8 errors

    Process completed.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Let's start with this one:
    Java Code:
    F:\Java\DivisibleByTwoGUI.java:72: cannot find symbol
    symbol : variable event
    location: class DivisibleByTwoGUI
    if(event.getSource() == btnShow){
    So it's saying that it can't find a variable named event declared and visible at that place in the program. So you must ask yourself, have you declared a variable event around here? I don't see one, and so you're calling this method on the wrong variable, a non-existent variable. You normally call this method on an ActionEvent object. So do you see an ActionEvent object declared nearby, perhaps as the parameter of a method?

    Your other errors are not from the code that you posted (you must have changed it!), but are again pretty straight forward. They state that you're trying to use arithmetic operators on JTextFields which doesn't make sense. Instead you need to use these operators such as greater than and less than, on numbers.

  5. #5
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    I got it all fixed! Thanks for your help! Such small errors, now that I saw them!

  6. #6
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Actually, one more question for ya. After getting the code to execute, I have to run it with the test code (see the end of my first post). Instead of displaying the window as I thought it does nothing and actually stops running altogether. Any idea why? I get the following message, but have no errors:

    --------------------Configuration: <Default>--------------------
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at DivisibleByTwoGUI.<init>(DivisibleByTwoGUI.java:45 )
    at DivisibleByTwoGUITest.main(DivisibleByTwoGUITest.j ava:9)

    Process completed.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Which line throws the NullPointerException (NPE)? It is line 45 of DivisibleByTwoGUI as indicated by this:

    at DivisibleByTwoGUI.<init>(DivisibleByTwoGUI.java:45 )

  8. #8
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you for the advice!
    I have updated my code but, now I get a blank window when I run the program. Any idea why my panels, buttons, text areas, etc. are not showing? What did I not do correctly??

    Below is the code, and below that the test code.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class DivisibleByTwoGUI extends JFrame implements ActionListener{
    
    	private final int HEIGHT = 450;
    	private final int WIDTH = 600;
    
    	private JLabel lblStartNum, lblEndNum, lblBlank;
    	private JTextField txtStartNum, txtEndNum;
    	private JButton btnShow;
    	private JScrollPane scrollResults;
    	private JTextArea txtareaResults;
    	private JPanel p1, p2, p3, p4;
    
    
    	public DivisibleByTwoGUI(String title){
    		super(title);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(HEIGHT, WIDTH);
    
    		setLayout(new GridLayout(1,1));
    
    		p1 = new JPanel();
    		p2 = new JPanel();
    		p3 = new JPanel();
    		p4 = new JPanel();
    
    		lblStartNum = new JLabel("Enter Starting Number: ");
    		lblEndNum = new JLabel("Ender Ending Number: ");
    		lblBlank = new JLabel(" ");
    
    		txtStartNum = new JTextField(10);
    		txtEndNum = new JTextField(10);
    
    		btnShow = new JButton("Show numbers divisible by 2");
    
    		p1.setLayout(new GridLayout(3,1));
    		p2.setLayout(new FlowLayout());
    		p3.setLayout(new GridLayout(1,3));
    		p4.setLayout(new GridLayout(1,3));
    
    		p2.add(p1);
    		p3.add(p1);
    		p4.add(p1);
    		add(p1);
    
    		p2.add(lblStartNum);
    		p2.add(txtStartNum);
    		p2.add(lblEndNum);
    		p2.add(txtEndNum);
    
    		p3.add(lblBlank);
    		p3.add(btnShow);
    		p3.add(lblBlank);
    
    		txtareaResults = new JTextArea(5,30);
    		txtareaResults.setEditable(false);
    		scrollResults = new JScrollPane(txtareaResults);
    
    		p4.add(lblBlank);
    		p4.add(scrollResults);
    		p4.add(lblBlank);
    
    
    		btnShow.addActionListener(this);
    
    	}
    
    	public void actionPerformed(ActionEvent event){
    
    		int startInt, endInt;
    
    		StringBuilder str = new StringBuilder();
    		if(event.getSource() == btnShow){
    
    			String start = txtStartNum.getText();
    			String end = txtEndNum.getText();
    			startInt = Integer.parseInt(start);
    			endInt = Integer.parseInt(end);
    
    			if(startInt <= endInt){
    				startInt = startInt + endInt;
    				endInt = startInt - endInt;
    				startInt = startInt - endInt;
    			}
    
    			while(startInt <= endInt){
    				if(startInt % 2 == 0){
    					str.append(startInt + "\n");
    					startInt++;
    				}
    			}
    		}
    		txtareaResults.setText(str.toString());
    	}
    
    }
    [text code]
    public class DivisibleByTwoGUITest{
    public static void main(String[] args){

    DivisibleByTwoGUI screen = new DivisibleByTwoGUI("Divisible by Two");
    screen.setSize(600,450);
    screen.setVisible(true);
    }
    }
    [/code]

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What are you trying to do here?:

    Java Code:
          p2.add(p1);
          p3.add(p1);
          p4.add(p1);
          add(p1);

    You seem to be trying to add p1 to everything, including the JFrame (the last line in the code immediately above), but you haven't added anything to p1.

Similar Threads

  1. StringBuilder
    By tmotse in forum New To Java
    Replies: 3
    Last Post: 09-13-2010, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •