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

    Default Cannot Link ActionListener with ActionEvent-2

    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; //Is inialized to 0 as the next y,w,h--so it could not draw anything.
          double y;// So I see after clicking DRAW button the frame but without rectangle. [B]So action listener could not pass input data from textfield to rectangleview[/B]
          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(); //[B]So action listener could not pass input data from textfield to rectangleview[/B]Why?
                    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();
    Controller contr=new Controller(mainView,rview);
            mainView.setVisible(true);
          rectView.setVisible(false);
        }   
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Your controller constructor and your instance creation parameters don't match. Please check the signature. And their is no rview defined in Appl.java.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Cannot Link ActionListener with ActionEvent-2

    And why does class Pkutnyk extend Rectangle2D.Double, but then redefines all the properties that Rectangle2D.Double has (x,y,width,height)?

    Each Pkutnyk instance will now always have an X and Y which are 0.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Each Pkutnyk instance will now always have an X and Y which are 0--
    But when
    I directly put -- public class RectangleView extends JPanel {
    double x=5;
    double y=5;
    double w=60;
    double h=60;--
    and this values will pass somethong lower to
    Pkutnyk rect = new Pkutnyk(x, y, w, h);--here--everything will be fine.
    g2.setPaint(Color.RED);
    g2.fill(rect);
    //So the issue is in x,y...
    When I put in ..
    RectangleView rects = new RectangleView();
    JFrame frame = new JFrame("Pryamokutnyk")=>new JFrame(""+rects.x"") I will got 0.0 despite really I pass to class, that do not probably
    pass when creating object rects.

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    public class Controller {
    MainView mainView;
    RectangleView rview;
    public Controller(MainView mainView, RectangleView rview) {
    this.mainView = mainView;
    this.rview=rview;
    this.mainView.addButtonListener(new ButtonListener());
    } -- About constructor it is mishap. This is the real constructor I use.
    2.)"And their is no rview defined in Appl.java."--why?--
    RectangleView rview=new RectangleView();

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Really it is probably the reason of invokelater method or some thread, cause
    when I put 2 in first textfield
    JFrame frame = new JFrame(""+rects.x"")=>0;
    but when I use class field JFrame frame = new JFrame(""+this.x"")=>2.0; so actionlistener retrive the data from the filed but whenI use simply
    public class RectangleView extends JPanel {

    double x=4;
    double y=5;
    double w=6;
    double h...; it pass to Pkutnykconstructor this values =>Pkutnyk rect = new Pkutnyk(this.x, this.y, this.w, this.h);
    But when
    the fileds aresimply
    public class RectangleView extends JPanel {

    double x;
    double y;
    double w;
    double h; ---and is initialized through actionlistener--it lost this field
    in this code...
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Pkutnyk rect = new Pkutnyk(this.x, this.y, this.w, this.h);
    g2.setPaint(Color.RED);
    g2.fill(rect);
    }
    public void main() {
    RectangleView rects = new RectangleView();
    JFrame frame = new JFrame(""+this.x+"");--it exactly get this.x=2 and display but paint component not.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(rects);
    frame.setSize(360, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
    }

    As in the first case the Pkutnyk is constructed I think really maybe needs invokelater () or some frame.repaint()--but where to place it in main () method because when I tried to put it before setSize() it did not changed nothing?
    Really we are very close to resolving...

  7. #7
    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-2

    Member ark is banned for 1 day for persistently neglecting to format code correctly.

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

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Here is correct code for controller:
    Java Code:
     
    package recct;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class Controller {
       MainView mainView;
       static RectangleView rview;
       public Controller(MainView mainView, RectangleView rview) {
       this.mainView = mainView;
       this.mainView.addButtonListener(new ButtonListener());
    	}
    	   class  ButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			try {
                rview=new RectangleView(mainView.getFirstPointX(), mainView.getFirstPointY(),
                mainView.getWidtht(),mainView.getHeightt(),mainView.getColor());
    				rview.main();
                //mainView.setVisible(false);
                //rview.setVisible(true);     
    			} catch (NumberFormatException ex) {
    				System.out.println(ex);
    			}
    		}
    	}
    }

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Sorry but I just enclosed the code for such pattern as:
    Java Code:
    value
    --but why it did not worked..

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Java Code:
     So I made rview static and it managed to work:
     public void main() {
        RectangleView rects = Controller.rview;
        JFrame frame = new JFrame(""+this.c+"--"+(int)this.w+"*"+(int)this.h+"");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    But this rectangle just take the first coordinate (X,Y) just to 0 and 0 that is anchored to up-left corner. But I do not know why. And how to make to began to draw from initial coordiantes thar are set by two fields in the mainview?

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    If I need to use file chooser--where i should situated such code--

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.JFileChooser;
    
    public class JFileChooserSelectionOption {
    
      public static void main(String[] a) {
        JFileChooser fileChooser = new JFileChooser(".");
    
        fileChooser.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Action");
    
          }
        });
        
        int status = fileChooser.showOpenDialog(null);
    
    
    
        if (status == JFileChooser.APPROVE_OPTION) {
          File selectedFile = fileChooser.getSelectedFile();
          System.out.println(selectedFile.getParent());
          System.out.println(selectedFile.getName());
        } else if (status == JFileChooser.CANCEL_OPTION) {
          System.out.println("calceled");
    
        }
      }
    
    }
    in View or controller class?
    --"."--what it emas --currecnt directory?

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

    Default Re: Cannot Link ActionListener with ActionEvent-2

    Java Code:
    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 java.awt.Color;
    public class RectangleView extends JPanel {
          double x;
          double y;
          double w;
          double h;
          Color c;
          public RectangleView (double x, double y, double w, double h, Color c){
          this.x=x;
          this.y=y;
          this.w=w;
          this.h=h;
          this.c=c;
          };
          
      public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          Pkutnyk rect = new Pkutnyk(this.x, this.y, this.w, this.h);
          g2.setPaint(this.c);
          g2.fill(rect);
      }   
      public void main() {
        RectangleView rects = Controller.rview;
        JFrame frame = new JFrame(""+this.c+"--"+(int)this.w+"*"+(int)this.h+"");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(rects);
        frame.setLocationRelativeTo(null);
        frame.repaint();
        frame.setSize(360, 300);
        frame.setVisible(true);
      } 
    }
    --why do this code just take into account the width and height but the initial X,Y (left-upper points)--is set to 0 and 0, despite I
    enter not null coordinates. It seems to be in Jpanel in JFrame?
    The frame.setLocationRelativeTo(null); or to "this"--it should center or what?

Similar Threads

  1. Replies: 16
    Last Post: 04-17-2015, 11:16 AM
  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
  •