Cant get Object to Appear

Using action listener again

Page 1 of 1

4 Replies - 614 Views - Last Post: 01 March 2009 - 01:09 PM Rate Topic: -----

#1 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Cant get Object to Appear

Posted 27 February 2009 - 06:20 PM

In this project I have to take user input (color, size, label, chart type) and allow them to make a graph.
By clickiing the "add" button it is supposed to make the new bar or new slice pf the pie chart. I have implemented some action listeners but nothing shows up when the add button is clicked. Can anybody take a look at this one and see whats going on? As of now only the bar chart works so dont try the pie chart please. This one is due on Tuesday :D

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.JComponent;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
 *
 * @author hidden
 */
public class RectangleComponent extends JComponent{
	int size;
	Color color;
	public RectangleComponent(int s, Color c)
	{
		size=s;
		color = c;
	}
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.setBackground(color);
		g2.fillRect(5, 5, size, 5);
	}
}





/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.JComponent;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
 *
 * @author hidden
 */
public class RectangleComponent extends JComponent{
	int size;
	Color color;
	public RectangleComponent(int s, Color c)
	{
		size=s;
		color = c;
	}
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.setBackground(color);
		g2.fillRect(5, 5, size, 5);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Cant get Object to Appear

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Cant get Object to Appear

Posted 27 February 2009 - 06:55 PM

You couldn't have even added the ActionListeners, you don't have the proper package(s) imported to do so.

And I don't see them anywhere.

Edit:...those are probably in another class. -_-

This post has been edited by Locke: 27 February 2009 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cant get Object to Appear

Posted 27 February 2009 - 09:40 PM

Post the class which has the "add" button
Was This Post Helpful? 0
  • +
  • -

#4 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: Cant get Object to Appear

Posted 28 February 2009 - 08:13 PM

Oh dang. I accidently posted the same class twice. I will get that fixed here in just a few.

// a class to run the program. request user inputs and work from methods

// imports whats needed
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;



  public class ChartFrame {
	// private integer making a color chooser
   
	  
	
	
  public static void main(String[] args) {
	  
	 
	//create a new frame with a title  
	JFrame cframe=new JFrame("Do it Yourself Charts!");
	
	//creates a series of panels that will be placed in the frame
	JPanel lpanel = new JPanel();
	final JPanel chartspace = new JPanel();
	JPanel radiospace = new JPanel();
	JPanel abutton = new JPanel();
	
	
	
	// Creates 2 new Text fields for user input
	final JTextField chartSize = new JTextField("Size", 20);
	final JTextField chartLabel = new JTextField("Label", 20);
	
	 
	
	//adds this panel to the west of the frame
	
	cframe.add(lpanel, BorderLayout.WEST);
	//adds this text field to the left panel
	 lpanel.add( chartLabel );
	 
	 //saves the value from the chart label into a string
	// String x = chartLabel.getText();
	 
	 //same again except with the chart size
	 lpanel.add(chartSize);
	 //String y = chartSize.getText();
	 
	  final JColorChooser tcc;
	 //makes a new color chooser
	tcc = new JColorChooser();
		// adds it to the panel
		lpanel.add(tcc );

	
	  
	//adds the panel for the checkboxes and puts them to the east
	cframe.add(radiospace, BorderLayout.EAST);
	
	//makes a new groupf of radioboxes
	CheckboxGroup cg1=new CheckboxGroup();
	// the two radio buttons set to be empty
	radiospace.add(new Checkbox("Pie", cg1, false));
	radiospace.add(new Checkbox("Bar", cg1, false));
	
	// center aligns and adds the "add" button
	cframe.add(abutton, BorderLayout.CENTER);
	JButton addButton = new JButton("Add");
	class ChartListener implements ActionListener{
	public void actionPerformed(ActionEvent event)
	  {
		int size = Integer.parseInt(chartSize.getText());
		String label = chartLabel.getText();
		//Color color = tcc.getColor();
		RectangleComponent rc = new RectangleComponent(size,Color.BLUE);
		chartspace.add(rc);
		  
	  }
	  }
	ChartListener  listener = new ChartListener();
	addButton.addActionListener(listener);
	radiospace.add(addButton);
   
	
	
	
	// the panel where the charts will go
	cframe.add(chartspace, BorderLayout.CENTER);
	
	
	// sets the frame size
	cframe.setSize(300,400);
	
	//makes the frame visible
	cframe.setVisible(true);
	
	// makes it so the frame can be closed and exit program
	
	 cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	 
		 
	 }

}

This post has been edited by Public Designs: 28 February 2009 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: Cant get Object to Appear

Posted 01 March 2009 - 01:09 PM

I am still having problems with getting this to show. I added in some default parameters such as the color to try and get it to show and its still not coming up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1