Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
JTextFields and storing data Posted by circuz_phreak on 5 Feb 2007 at 2:26 PM
I'm not sure where I should begin with this. I have 6 circles drawn in a frame. When you select one you are able to enter name and number into two seperate text fields. clicking a "set" button stores the information for that circle (and should display it in the same textFields when the circle is clicked again). Clicking a circle and clicking reset should clear that data. I'm not sure where I should begin. Should I make a class that extends the one I use for creating the circles? Should I store the data inside an array?
Report
Re: JTextFields and storing data Posted by zibadian on 5 Feb 2007 at 9:01 PM
: I'm not sure where I should begin with this. I have 6 circles drawn in a frame. When you select one you are able to enter name and number into two seperate text fields. clicking a "set" button stores the information for that circle (and should display it in the same textFields when the circle is clicked again). Clicking a circle and clicking reset should clear that data. I'm not sure where I should begin. Should I make a class that extends the one I use for creating the circles? Should I store the data inside an array?
:
I would store the data in the circle, since that is the most logical place. Extending a class from the original circle is a good way of doing this. The "set"-button's click event should en simple enough: get the data from the JTextFields (check out getText()) and set it in the selected circle.
The "reset"-button is similar to the "set"-button, but sets empty data into the circle.
Clicking a circle reverses the "set"-button: it gets the data from the circle and sets the data into the textfield (check out setText()).
Report
Re: JTextFields and storing data Posted by circuz_phreak on 6 Feb 2007 at 1:09 PM
This is some of my program. I'm not sure if I should extend the player class or not. Not 100% sure on how to manipulate arrays that will store the data either.

class HockecyLineupFrame{ 
//create two frames, and border layouts for panels, add buttons, labels and textfields...code omitted for space.
} 

private class HockeyLineUpPanel extends JPanel {	
public void paintComponent(Graphics g) {
	   super.paintComponent(g);
	   Graphics2D g2 = (Graphics2D)g;
          //Players
	  p1 = new Player(size, colour1, filled, MidX, 75);
	  p1.draw(g2);
 	  p2 = new Player(size, colour2, filled, MidX/2, MidY);
	  p2.draw(g2);
 	  p3 = new Player(size, colour2, filled, MidX+(MidX/2), MidY);	  p3.draw(g2);
 	  p4 = new Player(size, colour3, filled, MidX/3, MidY+(MidY/2));	  p4.draw(g2);
 	  p5 = new Player(size, colour3, filled, MidX, MidY+(MidY/2)+30);
 	  p5.draw(g2);
 	  p6 = new Player(size, colour3, filled, MaxX-(MidX/3), MidY+(MidY/2));
	 p6.draw(g2);
 	 //Selector, (rectangle drawn around player thats clicked)	 if(rect != null){
		rect.draw(g2);
	}
 private class ENDMouseListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
	int MidX = (int)(end_panel.getSize().getWidth() / 2),
            MidY = (int)(end_panel.getSize().getHeight() / 2),
            MaxX = (int)(end_panel.getSize().getWidth()),
            MaxY = (int)(end_panel.getSize().getHeight()); 

// Determine which object (if any) was clicked in
	Shape s = null;	String position = " ";
 	if (p1 != null && p1.contains(e.getX(), e.getY())){
		s = p1;
		point_x = MidX; //for the rectangle
                point_y = 75; //for the rectangle		         position = "Goalie"; //label text
		 index = 0; //thought it could represent the index value of the array that holds the player's information	

     }else if (p2 != null && p2.contains(e.getX(), e.getY())){		             s = p2;
		     point_x = MidX/2;
		    point_y = MidY;
		    position = "Right Defender";		            index = 1;
	}else if (p3 != null && p3.contains(e.getX(), e.getY())){		                s = p3;
		        point_x = MidX+(MidX/2);		                point_y = MidY;	
	                position = "Left Defender";		                index = 2;
	}else if (p4 != null && p4.contains(e.getX(), e.getY())){		                s = p4;
		                point_x = MidX/3;
		                point_y = MidY+(MidY/2);		            	position = "Right Wing";
		            	index = 3;	
}else if (p5 != null && p5.contains(e.getX(), e.getY())){		                s = p5;		                point_x = MidX;		                point_y = MidY+(MidY/2)+30;		            	position = "Center";
		        index = 4;
	}else if (p6 != null && p6.contains(e.getX(), e.getY())){
		s = p6;
		point_x = MaxX-(MidX/3);
		point_y = MidY+(MidY/2);
		position = "Left Wing";
		index = 5;
	} 
	// Have we got anything?
		if (s != null) { 
		   player_number_text.setEditable(true);
		   player_name_text.setEditable(true);
		   player = s;
		   rect = new Selector(Shape.MEDIUM, Color.black, false, point_x, point_y);
		   player_position_label2.setText(position);
 	       }else{
		player_number_text.setEditable(false);			      player_name_text.setEditable(false);
	       rect = null;
	       player_position_label2.setText("Click a player!");
 	      }
		repaint();
       }
 } 
private class SetOrResetListener implements ActionListener { //no code for setting player information, just conditions for the data entered (whether valid or not)
     public void actionPerformed(ActionEvent e) {
	        Object source = e.getSource();
            if (source == set_button) {
            if(player_number_text.getText().length() > 2 || player_number_text.getText().length() == 0 || player_name_text.getText().length() == 0){
		JOptionPane.showMessageDialog(null, "Invalid entry!");			player_number_text.setText("");	
	        player_name_text.setText("");
		} 
		if(player_number_text.getText().length() > 0){ 	
	   try{	
	     int playerNumber = Integer.parseInt(player_number_text.getText());	
							    player_number = player_number_text.getText();
 		   }catch(NumberFormatException nFE) {
								JOptionPane.showMessageDialog(null, "Invalid input for number")	player_number_text.setText("");
	player_name_text.setText("");
 }
}
     if(player_name_text.getText().length() > 0){
	player_name = player_name_text.getText();
     }     player_number_text.setEditable(false);
    player_number_text.setText("");
    player_name_text.setEditable(false);
    player_name_text.setText("");
    rect = null;
		               }else if(source == reset_button) {
	rect = null;
	player_number_text.setEditable(false);
	player_name_text.setEditable(false);
    }
  repaint();
}
}
 abstract class Shape {
   public Shape(float new_size, Color new_color, boolean new_filled
{
        size = new_size;
        color = new_color;
        filled = new_filled;
 }
    public void draw(Graphics2D g2) {
        g2.setColor(color);
    }
    public boolean is_filled() { 
       return filled; 
   }
    public float get_size() {
        return size;    
}
    public abstract boolean contains(int x, int y);
     public String toString() {
        return "size=" + size + " color=" + color + " filled=" + filled;
    }
     private float size;
    private Color color;
    private boolean filled;
    public static final float SMALL = 0.5f;
    public static final float MEDIUM = 1.5f;
    public static final float LARGE = 2.0f;
    public static final float XLARGE = 4.0f;
    public static final float BASE_WIDTH = 50;
    public static final float BASE_HEIGHT = 45;
}
 class Player extends Shape { 
   public Player(float new_size, Color new_color, boolean new_filled, int mid_x, int mid_y) {
        super(new_size, new_color, new_filled);
        ellipse = new Ellipse2D.Float(mid_x - BASE_WIDTH * get_size() / 2.0f,                                      mid_y - BASE_HEIGHT * get_size() / 2.0f,                                      BASE_WIDTH * get_size(), BASE_HEIGHT * get_size());

    }    

public void draw(Graphics2D g2) { 
       super.draw(g2);
        g2.draw(ellipse);
         if (is_filled() == true) { 
           g2.fill(ellipse);
        }  
  }  
   public boolean contains(int x, int y) {
	        // Find out if the x,y coordinates are in this shape	        return ellipse.contains(x, y);
    }
    public String toString() { 
       return super.toString() + "; ellipse=" + ellipse.getX() + ", " + ellipse.getY() +            ", " + ellipse.getWidth() + ", " + ellipse.getHeight();
    }    
private Ellipse2D.Float ellipse;}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.