Results 1 to 9 of 9
  1. #1
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default JTextfield and updating the size of my grid in my JPanel

    So I would post the code but it is many classes. I have a text field that Im trying to put integers into and its suppose to update the size of my appletPanel so far I have:

    public void itemStateChanged(ItemEvent e) {
    if(e.getSource() == label1Text){

    appletPanel = new CityMap(Integer.parseInt(label1Text.getText()));



    }

    not sure what else I have to do. label1Text is my JTextField

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

    Default Re: JTextfield and updating the size of my grid in my JPanel

    I doubt that there's any way we can help you based on the information that you've presented, and I think that you'll likely have to tell us more and show more code. You'll also want to use code tags where you place the [code] tag above your code block and the tag [/code] below your pasted code block.

  3. #3
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: JTextfield and updating the size of my grid in my JPanel

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    
    public class ParkingGUIPanel extends JFrame implements ActionListener,
    		ItemListener {
    	
    	private final int DEFAULT_SIZE = 15;
    	
    	private JButton randomGrid = new JButton("Random Grid");//
    	private JButton manual = new JButton("Manual");        //part of buttonPanel
    	private JButton resetGrid = new JButton("Reset Grid");//
    	
    	private JButton zoomIn = new JButton("zoom in");// part of zoomPanel
    	private JButton zoomOut = new JButton("zoom out");//
    	
    	
    	private JPanel  mainPanel = new JPanel(), 
    					setupPanel = new JPanel(), 
    					statusPanel = new JPanel(), 
    					buttonPanel = new JPanel(),
    					labelPanel = new JPanel(),
    					rightPanel = new JPanel(),
    					gridPanel = new JPanel(),
    					zoomPanel = new JPanel(), 
    					//userEntryPanel = new JPanel(),
    					centerPanel = new JPanel();
    	
    	private CityMap appletPanel = new CityMap(DEFAULT_SIZE);
    	private JTextArea display = new JTextArea(10,35);
    	private JTextField label1Text = new JTextField(10);
    	private JTextField label2Text = new JTextField(10);
    	private JTextField label3Text = new JTextField(10);
    	private JLabel label1 = new JLabel("grid size: ");
    	private JLabel label2 = new JLabel("#parking spots: ");
    	private JLabel label3 = new JLabel("#cars: ");
    	
    	public ParkingGUIPanel(){
    		
    		mainPanel.setLayout(new BorderLayout());
    		rightPanel.setLayout(new GridLayout(3,1,1,1));
    		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
    		centerPanel.setLayout(new GridLayout(3,2,1,1));
    		setupPanel.setLayout(new GridLayout(4, 1, 1, 1));
    		gridPanel.setLayout(new BorderLayout());
    		
    		resetGrid.addActionListener(this);
    		manual.addActionListener(this);
    		randomGrid.addActionListener(this);
    		zoomIn.addActionListener(this);
    		zoomOut.addActionListener(this);
    		label1Text.addActionListener(this);
    		label2Text.addActionListener(this);
    		label3Text.addActionListener(this);
    		
    		
    		
    		setupPanel.setBorder(BorderFactory.createTitledBorder("Setup"));
    		statusPanel.setBorder(BorderFactory.createTitledBorder("Status"));
    		gridPanel.setBorder(BorderFactory.createEtchedBorder());
    		appletPanel.setSize(appletPanel.getWidth());
    		
    		mainPanel.add(rightPanel, BorderLayout.EAST);
    		mainPanel.add(gridPanel, BorderLayout.CENTER);
    		
    		setupPanel.add(buttonPanel, BorderLayout.NORTH);
    		setupPanel.add(centerPanel, BorderLayout.CENTER);
    	
    		gridPanel.add(appletPanel, BorderLayout.CENTER);
    		gridPanel.add(zoomPanel, BorderLayout.NORTH);
    		
    		zoomPanel.add(zoomIn);
    		zoomPanel.add(zoomOut);
    		
    		buttonPanel.add(resetGrid);
    		buttonPanel.add(manual);
    		buttonPanel.add(randomGrid);
    			
    		rightPanel.add(setupPanel);
    		rightPanel.add(statusPanel);
    		
    		centerPanel.add(label1);
    		centerPanel.add(label1Text);
    		centerPanel.add(label2);
    		centerPanel.add(label2Text);
    		centerPanel.add(label3);
    		centerPanel.add(label3Text);
    		
    		statusPanel.add(display);
    		
    		getContentPane().add(mainPanel);
    	
    	}
    
    	@Override
    	public void itemStateChanged(ItemEvent e) {
    		if(e.getSource() == label1Text){
    		
    			appletPanel = new CityMap(Integer.parseInt(label1Text.getText()));
    	
    			
    			
    		}
    		
    	}
    	
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == resetGrid) {
    			 appletPanel = new CityMap(DEFAULT_SIZE);
    			 
    	}
    		if(e.getSource() == manual){
    
    }
    
    		
    }
    }
    thats the gui

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    /**
     * Animates the movement of a Car on a city map. The city map is shown in
     * Cartesian coordinates where the origin (0, 0) is at the left bottom corner.
     * 
     * @author amit
     * 
     */
    @SuppressWarnings("serial")
    public class CityMap extends JPanel 
    {
        private int gridSize;
        private int displaySize = 700;
        private int blockSize;
        private int carSize;
        private int offset;
        private int numBlocks;
        private int delay = 500; // milliseconds
        private ArrayList<Car> cars;
        private ArrayList<ParkingSpot> spots;
        private Random rg;
        private ArrayList<Color> colors;
      
     
    
        /**
         * 
         * @param car
         *            The Car object to animate on the city map
         * @param gridSize
         *            The size of the grid: from (0,0) to (gridSize,gridSize)
         */
        public CityMap(int gridSize)
        {	rg = new Random();
        	cars = new ArrayList<Car>();
        	spots= new ArrayList<ParkingSpot>();
    		initColors();
            this.gridSize = gridSize;
            setParameters(gridSize);
            setBackground(Color.white);
            setPreferredSize(new Dimension(displaySize, displaySize));
            startAnimation();
        }
        public void setSize(int n){
        	displaySize = n;
        }
        private void initColors() {
        	colors = new ArrayList<Color>();
        	colors.add(new Color(200, 200, 200));
    		colors.add(new Color(51, 153, 51));
    		colors.add(new Color(102, 153, 51));
    		colors.add(new Color(153, 153, 51));
    		colors.add(new Color(153, 102, 51));
    		colors.add(new Color(153, 51, 51));
    		colors.add(new Color(153, 51, 102));
    		colors.add(new Color(153, 51, 153));
    		colors.add(new Color(102, 51, 153));
    		colors.add(new Color(51, 51, 153));
    		colors.add(new Color(51, 102, 153));
    		colors.add(new Color(51, 153, 153));
    		colors.add(new Color(51, 153, 102));
        }
        /**
         * Set parameters for the drawing based on the given size.
         * @param size 
         *            The size of the grid to draw from (0,0).
         */
        private void setParameters(int size) 
        {
            numBlocks = size;
            blockSize = displaySize/(numBlocks + 1);
            offset = blockSize;
            carSize = blockSize/4;  
        }
        
    
        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
         */
        public void paintComponent(Graphics obj)
        {
            Graphics2D page = (Graphics2D) obj;
            super.paintComponent(page);
    
            Font font = new Font(Font.MONOSPACED, Font.PLAIN, blockSize/3);
            page.setFont(font);
            
            drawMap(page);
            drawCars(page);
            drawSpots(page);
        }
        
        
    
        /**
         * Draw the parking spots on the map.
         * @param page
         */
        private void drawSpots(Graphics2D page) 
        { String pid = "spot" + rg.nextInt();
          spots.add(new ParkingSpot(pid, rg.nextInt(gridSize)*(blockSize)+offset-15,rg.nextInt(gridSize)*(blockSize)+offset-15, rg.nextInt()));
          for (ParkingSpot p : spots){
        	  page.setColor(Color.BLACK);
        
        	  page.drawOval((int)p.getLocationX(), (int)p.getLocationY(), 30, 30);
        	  
          }
        }
    
        /**
         * Draw the cars on the map.@param page0
         */
        private void drawCars(Graphics2D page) 
        {	String id = "car" + rg.nextInt();
        	cars.add(new Car(new Point(rg.nextInt(gridSize)*(blockSize)+offset-10, rg.nextInt(gridSize)*(blockSize)+offset-10), id));
        	
        	for(Car c : cars){
        		page.setColor( randomColor());
        		Point p = c.getLocation();
        		page.fillRect( p.x, p.y, 20, 20);
        		
        	}
    
        }
        public Color randomColor() {
    		return colors.get( rg.nextInt( colors.size()));
    	}
    
        /**
         * Draws the city grid and labels it using Cartesian coordinates in terms of blocks.
         * @param page
         */
        private void drawMap(Graphics2D page)
        {
            page.setColor(Color.red);
            // draw east-west roads for blocks 0 .. numBlocks - 1
            for (int i = 0; i < numBlocks - 1; i++) {
                page.drawString((numBlocks - i - 1) + "", offset/2, i * blockSize   + offset);
                for (int j = 0; j < numBlocks; j++) {
                    page.drawLine(i * blockSize + offset, j * blockSize + offset,
                            (i + 1) * blockSize + offset, j * blockSize + offset);
                }
            }
            page.drawString(0 + "", offset/2, (numBlocks - 1) * blockSize + offset);
    
            // draw north-south roads for blocks 0 .. numBlocks - 1
            for (int i = 0; i < numBlocks; i++) {
                page.drawString(i + "", i * blockSize + offset, offset/2);
                for (int j = 0; j < numBlocks - 1; j++) {
                    page.drawLine(i * blockSize + offset, j * blockSize + offset, i
                            * blockSize + offset, (j + 1) * blockSize + offset);
                }
            }
        }
    
      
        /**
         * Create an animation thread that runs periodically
         */
        private void startAnimation()
        {
            ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt)
                {
                    repaint();
                }
            };
            new Timer(delay, taskPerformer).start();
        }
    }

  4. #4
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: JTextfield and updating the size of my grid in my JPanel

    Well I posted the gui if you load the gui you will see a text field on the right for grid size I need it to change the size of the grid when you enter something. I havent been able to get any of the buttons to work...
    Last edited by Tax; 11-28-2011 at 02:38 AM.

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

    Default Re: JTextfield and updating the size of my grid in my JPanel

    1) Where do you add an ItemListener to anything?
    2) If you don't add it to anything, when will your itemStateChanged method be called?
    3) How could a JLabel ever be the source in an ItemEvent object (unless you create the ItemEvent object yourself)?

  6. #6
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: JTextfield and updating the size of my grid in my JPanel

    well Im no good at this... obviously.... if I call the method itemStateChanged it will execute it I fixed that... how would I get the manual button to take text from the TextFields and update the appletPanel when I push it maybe just starting with grid size. I have the DEFAULT_SIZE 15 for the grid.

  7. #7
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: JTextfield and updating the size of my grid in my JPanel

    Also should it be under actionPerformed not itemStateChanged?

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

    Default Re: JTextfield and updating the size of my grid in my JPanel

    An ItemListener is used when you're selecting something that toggles like a JToggleButton, JCheckBox or JRadioButton. An ActionListener is for listening to button presses and menu presses. Perhaps you want a MouseListener to listen for mouse presses? I'm not sure since only you know what behavior you are trying to get with this program. It sounds like you should reevaluate what you want to do, check the Swing tutorials to see which tools you should use, and perhaps re-write this program but starting from a pre-thought out plan.

  9. #9
    Tax
    Tax is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: JTextfield and updating the size of my grid in my JPanel

    I dont know why I would start over seriously... I was only asking when I click the button I need to update one of my panels.... i was looking for revalidate()... also i needed to remove and readd the panel then call revalidate().

Similar Threads

  1. Updating a Jpanel
    By aianta in forum AWT / Swing
    Replies: 5
    Last Post: 10-13-2011, 06:14 AM
  2. Help making a grid inside of a JPanel
    By satanfx55 in forum AWT / Swing
    Replies: 3
    Last Post: 05-13-2011, 08:18 PM
  3. Replies: 10
    Last Post: 01-12-2011, 08:16 PM
  4. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 08:52 AM
  5. Replies: 3
    Last Post: 04-21-2009, 12:31 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
  •