Results 1 to 17 of 17
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Cannot Link ActionListener with ActionEvent

    I need to crerate the interface for the input iof data for rectangle and then to display it by coordinates (and color fill).
    Here is rectangl class, rectanglview class, mainview (input) class, Controller, Aplication classes.
    1)
    Java Code:
      package recct;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import java.lang.Object; 
    import java.awt.geom.RectangularShape;
    import java.awt.geom.Rectangle2D.Double;
    
    public class Pkutnyk extends Rectangle2D.Double {
    	private double x1;
    	private double y1;
       private double w;
    	private double h;
    	private String color;
    	public Pkutnyk(double x1, double y1, double  w, double h) {
    		    this.x1=x1;
              this.y1=y1;
              this.w=w;
              this.h=h;
    	}
       
       public double getWidth(){ return w;}
       public double getHeight(){ return h;}
       public double getX1(){return x1;}
       public double getY1(){return y1;}
       
       public String getColor() {
    	return color;
    	}
       
    	public void setColor(String color) {
    		this.color = color;
    	}	
    }
    //2)
    package recct;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    public class MainView extends  JFrame {
    	private JLabel firstPointX = new JLabel(
    			"Enter X coordinate of first point");
    	private JTextField firstPointXt = new JTextField(10);
    	private JLabel firstPointY = new JLabel(
    			"Enter Y coordinate of first point");
    	private JTextField firstPointYt = new JTextField(10);
    	private JLabel secondPointX = new JLabel(
    			"Enter X coordinate of second point");
    	private JTextField secondPointXt = new JTextField(10);
    	private JLabel secondPointY = new JLabel(
    			"Enter Y coordinate of second point");
    	private JTextField secondPointYt = new JTextField(10);
    	private JLabel rectangleColor = new JLabel("Enter Color");
    	private JTextField rectangleColort = new JTextField(10);
    	private JButton button = new JButton("Draw");
    	public MainView() {
    		JPanel panel = new JPanel();
    		this.setTitle("Main");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(200, 500);
    		this.setLocationRelativeTo(null);
    		this.setLayout(new GridBagLayout());
    		this.setVisible(true);
    		panel.add(firstPointX);
    		panel.add(firstPointXt);
    		panel.add(firstPointY);
    		panel.add(firstPointYt);
    		panel.add(secondPointX);
    		panel.add(secondPointXt);
    		panel.add(secondPointY);
    		panel.add(secondPointYt);
    		panel.add(rectangleColor);
    		panel.add(rectangleColort);
    		panel.add(button);
    		this.add(panel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
    				GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(
    						2, 2, 2, 2), 0, 0));
    	}
    	public double getFirstPointX() {
    		return Double.parseDouble(firstPointXt.getText());
    	}
    	public double getFirstPointY() {
    		return Double.parseDouble(firstPointYt.getText());
    	}
    	public double getWidtht() {
    		return Double.parseDouble(secondPointXt.getText())-getFirstPointX();
    	}
    	public double getHeightt() {
    		return Double.parseDouble(secondPointYt.getText())-getFirstPointY();
    	}
    	public String getrectangleColor() {
    		return rectangleColort.getText();
    	}
    	public void addButtonListener(ActionListener listenForButton) {
    		button.addActionListener(listenForButton);
    	}
    	void displayErrorMessage(String errorMessage){
    		 JOptionPane.showMessageDialog(this, errorMessage);
    	}
    }
    
    //3)
    package recct;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.geom.Rectangle2D;
    public class RectangleView extends JPanel {
          double x;
          double y;
          double w;
          double h;
          
      public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          Pkutnyk rect = new Pkutnyk(x, y, w, h);
          g2.setPaint(Color.RED);
          g2.fill(rect);
    
      } 
      public void main() {
        RectangleView rects = new RectangleView();
        JFrame frame = new JFrame("Pryamokutnyk");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(rects);
        frame.setSize(360, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    //4)
    package recct;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Controller {
    
      private Pkutnyk pkut;
      private MainView mainView;
      private RectangleView rview;
    	
       public Controller(Pkutnyk pkut, MainView mainView) {
    		this.pkut = pkut;
    		this.mainView = mainView;
    		this.mainView.addButtonListener(new ButtonListener());
    	}
       
    	class  ButtonListener implements ActionListener {
    
    		public void actionPerformed(ActionEvent arg0) {
    			try {
    				rview.x = mainView.getFirstPointX();
    				rview.y = mainView.getFirstPointY();
    				rview.w = mainView.getWidtht();
    				rview.h = mainView.getHeightt();
    					rview.main();
                                                                mainView.setVisible(false);
    					rview.setVisible(true);
                   
    
    			} catch (NumberFormatException ex) {
    				System.out.println(ex);
    
    			}
    		}
    	}
    }
    
    //5)
    package recct;
    public class Appl {
    	public  static void main(String[] args) {
    		MainView mainView = new MainView();
          RectangleView rectView=new RectangleView();
    		mainView.setVisible(true);
          rectView.setVisible(false);
    	}	
    }
    Last edited by ark; 04-16-2015 at 10:23 AM.

  2. #2
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    Really I could to draw the rectangle but without entering the data. So it is bad linkage of input data and rectangleview classs and method.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Cannot Link ActionListener with ActionEvent

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Cannot Link ActionListener with ActionEvent

    Could you edit your post (click "Edit Post") so that it's readable? Put [code] at the start of the code and [/code] at the end. Make sure the indentation is there.

    What is the problem? Does the code compile? If not, post the compiler messages. Does the code compile but not do, at run time, what you expect or intend? If so, describe what it does do (including the stacktrace if it throws an exception) as well as what you were expecting or intending.

  5. #5
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    Yes, code compiles.
    When I launch the Appl it open MainView with input field. But When I put these data-
    and click Draw--nothing to happen. So it seems to be no actionlistener is working. Wouldyou help me?
    I really need it.
    Once I had the same problem, and it was difficult to do.

  6. #6
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    People, could anybody help? I really need it very much.
    What the reasom that action listener do not works. What is wrong--incorrect "addActionListener", "ButtonListener","Action perfomed"?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Cannot Link ActionListener with ActionEvent

    Thanks for updating the code.

    I don't think there's anywhere where you actually add that action listener. In particular I don't see where you have created an instance of Controller.

    It would be a very good idea to do the gui creation on the event dispatch thread, not the initial thread as you do at the moment. See "Initial Threads" in the Concurrency in Swing lesson of Oracle's Tutorial.

  8. #8
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    Or it should be too weird for me to using SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    }); -- where to use -- createAndShowGUI--it should be paintcomponent()?
    this.mainView.addButtonListener(new ButtonListener)--here I add -- and where I should
    include the instance of Controller--in MainView? Could you make tips in some code.
    Because I really need it there is other more difficult task.
    So it really is issue in buttonlistener as when clicking on DRAW button--it did not react in anyway/
    Except it there are links inside this MVC as when directly initialize coordinates of rectangle --it produce the rectangle.

  9. #9
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    Really after adding Controller object to Appl file the actions with button is performed but after clicking it I receive the empty rectangle frame.
    package recct;
    public class Appl {
    public static void main(String[] args) {
    MainView mainView = new MainView();
    RectangleView rview=new RectangleView();
    Controller contr=new Controller(mainView,rview);
    mainView.setVisible(true);
    rview.setVisible(false);}
    }

    So again the data from textfileds are not retrieved. Why? (Despite ButtonListener works).
    People help me -- as it is very annoying for me, Remeber once a year ago I had the same example and the situation was similar to make it works.
    Here I even added e.getsource but vain.

  10. #10
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    try {
    if (e.getSource() == mainView.button){
    rview.x = mainView.getFirstPointX();
    rview.y = mainView.getFirstPointY();
    rview.w = mainView.getWidtht();
    rview.h = mainView.getHeightt();}
    rview.main();
    } catch (NumberFormatException ex) {
    System.out.println(ex);
    }
    }

  11. #11
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    public class RectangleView extends JPanel {
    double x;
    double y;
    double w;
    double h;
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Pkutnyk rect = new Pkutnyk(x, y, w, h);
    g2.setPaint(Color.RED);
    g2.fill(rect);
    }
    public void main() {
    RectangleView rects = new RectangleView();
    // JFrame frame = new JFrame(""+rects.x+""); --when using it I get know that x is initialized to 0.0 so it is main problem that I could retrive. //////Why? -- maybe I need create one more object.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(rects);
    frame.setSize(360, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
    }

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Cannot Link ActionListener with ActionEvent

    after adding Controller object to Appl file the actions with button is performed but after clicking it I receive the empty rectangle frame.
    Your button handler sets the x, y, w, and h variables of rview, but you don't repaint the panel as far as I can see. In fact I'm not even sure it is added to your gui anywhere. (Although I assume you have changed the Controller class...)

    What did you intend rview.main() to do?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Cannot Link ActionListener with ActionEvent

    ark, I gave you a link at response #3. I expect you to check it out.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    JFrame frame = new JFrame(""+rects.x+"") --- it shows me the frame with the title 0.0 -- so the x as well as other coordinates do not set except 0.
    So maybe I need such element as invoke later or maybe due some reasons it really do not extract data from textfileds.

  15. #15
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    package recct;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.geom.Rectangle2D;
    import javax.swing.*;
    public class RectangleView extends JPanel {
    double x;
    double y;
    double w;
    double h;
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Pkutnyk rect = new Pkutnyk(x, y, w, h);
    g2.setPaint(Color.RED);
    g2.fill(rect);
    }
    public void main()
    {
    JFrameExample example = new JFrameExample();
    SwingUtilities.invokeLater(example);
    }
    public class JFrameExample implements Runnable
    {
    public void run()
    {
    RectangleView rects = new RectangleView();
    JFrame frame = new JFrame(""+rects.x+"");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(rects);
    frame.setSize(360, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
    } -- this code also evoke the same x=0.0;

  16. #16
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Cannot Link ActionListener with ActionEvent

    public void actionPerformed(ActionEvent e) {
    try {
    rview1.x = Double.parseDouble(mainView.firstPointXt.getText() );
    rview1.y = mainView.getFirstPointY();
    rview1.w = mainView.getWidtht();
    rview1.h = mainView.getHeightt();
    rview1.main();
    } catch (NumberFormatException ex) {
    System.out.println(ex);
    }
    } --even here--x=0.0--what can I to do?

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Cannot Link ActionListener with ActionEvent

    That's about enough unformatted code for one thread.

    ark, please check the link I gave you and learn how to format code correctly. Then start a new thread.

    I'm locking this.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Wait for ActionEvent
    By Razion in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 12:08 PM
  2. Need help with ActionEvent/ItemEvent
    By patriotsfan in forum AWT / Swing
    Replies: 2
    Last Post: 06-05-2011, 02:17 PM
  3. What does (this) and ActionEvent e mean?
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 05:08 PM
  4. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •