Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2008, 03:27 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
new to java
I am working on this program and I think I have everything done besides action event and was wanting to know if someone could take a look at it and let me know why I am not able to run in the command console and I get the following error in command console....

error...

Exception in thread "main" java.lang.NullPointerException
at Checkerboard.<init><Checkerboard.java:39>
at Checkerboard.main<Checkerboard.java:100>
Press any key to continue

my code is:

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextField topDisplay; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton[]; private Button clearButton[]; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct components and initialize beginning values topDisplay = new TextField(16); topDisplay.setEditable(false); topPanel = new Panel(); bottomPanel = new Panel(); bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //set color to the panel //row 1 for(int i = 1; i <= 4; i++) //set color for setBackground(Color.white); //row 2 for(int i = 5; i <= 8; i++) //set color for setBackground(Color.white); //row 3 for(int i = 9; i <= 12; i++) //set color for setBackground(Color.white); //row 4 for(int i = 13; i <= 16; i++) //set color for setBackground(Color.white); //register the listener with each Button for(int i = 0; i < goButton.length; i++) goButton[i].addActionListener(this); for(int i = 0; i < clearButton.length; i++) clearButton[i].addActionListener(this); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
instructions say:

Create a panel containing an array of 16 TextArea components that change color to correspond with the start, stop, and step values entered by the user. Perform the following tasks to create the Checkerboard Array application shown below. When the user enters the start, stop, and step fields and then clicks the Go button, the results are also shown below.

1. Call your application Checkerboard.java
2. You will need the following variables… declare them as private:
a. 16 component TextArea array
b. a Panel to hold the array
c. 3 TextField components with length of 10
d. 3 int variables to receive the start, stop, and step values
e. 3 Labels to display the words Start, Stop, and Step
f. a Go button
g. a Clear button
h. a Panel to hold the 3 TextFields, 3 Labels, and the 2 Buttons
3. Create a constructor method to:
a. construct each of the components declared above and initializes the start, stop, and step variables to zero (when constructing the TextArea components, use the following parameters: null, 3, 5, 3)
b. set the Frame layout to BorderLayout
c. write a for loop to loop the array and set each of the 16 TextArea components in that array so they cannot be edited. In the same loop, set each of the TextArea components text to be 1 more than the index number. Also in this same loop, set the background of each of the TextArea components to white.
d. set the Panel for the TextArea components to GridLayout with 4 rows, 4 columns, and both gaps set to 10
e. set the Panel for the TextFields, Labels, and button to GridLayout with 3 rows, 3 columns, and both gaps set to 5
f. add the components to their respective Panels
g. make the buttons clickable
h. place the Panels in the Frame… put one in the NORTH and one in the CENTER
i. Enter the addWindowListener() method described in the chapter… this is the method that overrides the click of the X so it terminates the application
4. In your actionPerformed() method:
a. convert the data in your TextFields to int and store them in the variables declared above
b. write a loop that goes through the array setting every background color to blue
c. write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)


5. Write a main() method that creates an instance of the Checkerboard Frame.
a. set the bounds for the frame to 50, 100, 300, 400
b. set the title bar caption to Checkerboard Array
c. use the setVisible() method to display the application Frame during execution
6. After you get all of this complete, include error handling to make sure:
a. the values entered in the TextFields are valid integers
b. the start value is greater than or equal to 1 and less than or equal to 16
c. the stop value is greater than or equal to 1 and less than or equal to 16
d. the step value is greater than or equal to 1 and less than or equal to 16
e. the start condition is less than the stop condition
f. when an error occurs, give an error message in a dialog box that is specific to their error, remove the text from the invalid field, and put the cursor in that field so the user has a chance to re-enter… this can be accomplished by using multiple try/catch statements
g. only change the colors if the numbers are valid
7. Create a clear button as seen in the example below. This button should:
a. clear out all 3 TextFields
b. change the background color of all TextArea array elements to white
c. put the cursor in the start field
8. Document!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-19-2008, 09:27 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Ok,

You forgot to initialize the following:

goButton, clearButton, topPanel and bottomPanel...

You are just declare them... not initialized....

when declaring them, not all but it will be set to null... ( the value ) not the address that holds the value....

regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 09:30 PM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
I think I made the correct changes by moving things around and initializing the buttons and panels (as long as I did it correctly) and getting a new message that I think has something to do with the TextArea. If this is just something with the action event then I am not worried about it because I am programming that today but if I am still messing something up with initialization point any ideas would be helpful.

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //construct the Display for(int i = 1; i <= 16; i++) { topDisplay[i] = new TextArea(null,3,5,3); } //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //set color to the panel //row 1 for(int i = 1; i <= 4; i++) //set color for setBackground(Color.white); //row 2 for(int i = 5; i <= 8; i++) //set color for setBackground(Color.white); //row 3 for(int i = 9; i <= 12; i++) //set color for setBackground(Color.white); //row 4 for(int i = 13; i <= 16; i++) //set color for setBackground(Color.white); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Error in the console application says:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at Checkerboard.<init>(Checkerboard.java:52)
at Checkerboard.main(Checkerboard.java:110)
Press any key to continue . . .


Could importing another java package resolve this
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 10:20 PM
Member
 
Join Date: Apr 2008
Location: USA
Posts: 12
JavaLovenJoe is on a distinguished road
The tutorial I read says you must extend NewApplication if you make a new application. And where are your Utils?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-20-2008, 01:41 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
I think I have gotten really close because I have gotten my program to run...the only problem is I only can see 3 boxes (they are not even numbered), 3 labels (start, stop,and step), and two giant sized buttons(go and clear)

I really hope this is just something small and could easily be corrected but does anyone see the error I am making in my program. I wanna think it has something to do with my program being out of order but I tried moving a bunch of things around and couldnt come across any changes.

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 1; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); //set color to the panel //row 1 for(int x = 1; x <= 4; x++) //set color for setBackground(Color.white); //row 2 for(int x = 5; x <= 8; x++) //set color for setBackground(Color.white); //row 3 for(int x = 9; x <= 12; x++) //set color for setBackground(Color.white); //row 4 for(int x = 13; x <= 16; x++) //set color for setBackground(Color.white); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-20-2008, 09:53 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
FINALLY got what I needed to be done today now just working on the action...was wondering if anyone had a chance to take a look at what I have done so far to make sure everything looks good and was also wanting to know if anyone had an idea on how to put numbers on the boxes...

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topPanel.add(topDisplay[i]); //set color to the panel //row 1 for(int x = 1; x <= 4; x++) //set color for setBackground(Color.white); //row 2 for(int x = 5; x <= 8; x++) //set color for setBackground(Color.white); //row 3 for(int x = 9; x <= 12; x++) //set color for setBackground(Color.white); //row 4 for(int x = 13; x <= 16; x++) //set color for setBackground(Color.white); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); for(int i = 1; i <=16; i++) setBackground(Color.blue); for(int i = start; i <= stop; i++) setBackground(Color.yellow); //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-20-2008, 09:51 PM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
apologize for bothering you guys but I have one more question and no more bothering I promise...

It has to do with my go button...When I click it, the background turns to blue that is no problem but the issue is certain parts do not return to yellow...It is hard to explain withpout showing the instuctions so here are the instructions for the for loop

write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)

In the start stop and step fields in the example I have on my word document there shows 16 boxes looking like this

1 2 3 4
5 6 7 9
9 10 11 12
13 14 15 16

When I input 3 for start 15 for stop and 4 for step the boxes that are to be yellow are 3, 7, 11, 15...however they are all turning blue and was wondering if anyone could take a look at my loop and let me know if I am doing something wrong in it or something else.

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); //topPanel.setEditable("false"); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { for(int i = 0; i <=16; i++) topDisplay[i].setBackground(Color.blue); for(int i = start; i <= stop; step++) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-20-2008, 11:58 PM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
if that is difficult to understand what i mean just ask and i can repost
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-21-2008, 01:56 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
got the yellow boxes to come up but just one box.....so is there something wrong with my yellow set background because I am not seeing any more errors

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) && (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) && (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) && (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start; i <= stop; step++) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-21-2008, 05:47 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
Almost finished just working on my try\catch and I will be done can anyone let me know if I am on the right track and if I am any advice id greatly appreciate it if its before 11?

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) && (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) && (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) && (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start-1; i < stop; i+=step) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-21-2008, 07:05 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
last question and I will be done this is the problem I am having and not sure how to do this.....

only change the colors if the numbers are valid

Code:
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) || (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) || (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) || (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start-1; i < stop; i+=step) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 05:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org