Results 1 to 9 of 9
- 11-28-2011, 12:17 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
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
-
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.
- 11-28-2011, 01:19 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: JTextfield and updating the size of my grid in my JPanel
thats the guiJava 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){ } } }
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(); } }
- 11-28-2011, 01:26 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
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 01:38 AM.
-
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)?
- 11-28-2011, 02:22 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
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.
- 11-28-2011, 02:24 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: JTextfield and updating the size of my grid in my JPanel
Also should it be under actionPerformed not itemStateChanged?
-
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.
- 11-29-2011, 02:07 AM #9
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Updating a Jpanel
By aianta in forum AWT / SwingReplies: 5Last Post: 10-13-2011, 05:14 AM -
Help making a grid inside of a JPanel
By satanfx55 in forum AWT / SwingReplies: 3Last Post: 05-13-2011, 07:18 PM -
2d array grid in JPanel with random placed images
By Furby in forum New To JavaReplies: 10Last Post: 01-12-2011, 07:16 PM -
Applet JTextField Size Problem
By a13w in forum New To JavaReplies: 4Last Post: 09-19-2009, 07:52 AM -
calling JPanel's JTextField from another JPanel class
By k2k in forum AWT / SwingReplies: 3Last Post: 04-20-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks