Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By doWhile

Thread: Some problems with components added to jpanel

  1. #1
    acs5538 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Post Some problems with components added to jpanel

    Hello all,

    I am new to Java and I am currently working on a GUI project in Netbeans. I have used the GUI builder to create a jframe with a jmenubar and a jpanel. Right now I am trying to make it so that a user can click on a menu item within the menu bar in order to add a text area to the panel. Then each time they click that menu item to insert a text area each one is different (object1, object2, object3, etc). Then the user can drag and drop these text areas around the panel and right click on them in order to edit their names and delete them, for example.

    I have this working somewhat but I have encountered a couple problems:

    1. Every time an object is inserted all of the objects snap back to their original positions.
    2. The right click functionality only works when there is only one object (I do not know how I should reference the different

    All of the code that I have written is under the actionevent for the “insert object” menu item. I am also using a custom class for dragging and dropping the components. I have spent many hours trying to fix these problems. Any and all help will be appreciated, particularly regarding the problems I mentioned and just "cleaning up" my code in general.

    NOTE: The following code is likely to cause the following in experienced developers - frustration, anger, despair, and agony

    Thanks to anyone that takes time to read this and offer input!

    Java Code:
      JPopupMenu Pmenu;
      JMenuItem menuItem;
      int counter = 0;
        
            
        private void objectinsertActionPerformed(java.awt.event.ActionEvent evt) {                                             
        
            //Declare new objects
        final JTextArea object1 = new JTextArea("Object 1");
        object1.setEditable(false);
        JTextArea object2 = new JTextArea("Object 2");
        object2.setEditable(false);
            JTextArea object3 = new JTextArea("Object 3");
        object3.setEditable(false);
            JTextArea object4 = new JTextArea("Object 4");
        object4.setEditable(false);
            JTextArea object5 = new JTextArea("Object 5");
        object5.setEditable(false);
            JTextArea object6 = new JTextArea("Object 6");
        object6.setEditable(false);
            JTextArea object7 = new JTextArea("Object 7");
        object7.setEditable(false);
            JTextArea object8 = new JTextArea("Object 8");
        object8.setEditable(false);
            JTextArea object9 = new JTextArea("Object 9");
        object9.setEditable(false);
            JTextArea object10 = new JTextArea("Object 10");
        object10.setEditable(false);
        
        
            //Organize object into array
        ArrayList<JTextArea> objectarray = new ArrayList<>();
        objectarray.add(object1);
        objectarray.add(object2);
        objectarray.add(object3);
        objectarray.add(object4);
        objectarray.add(object5);
        objectarray.add(object6);
        objectarray.add(object7);
        objectarray.add(object8);
        objectarray.add(object9);
        objectarray.add(object10);
    
            
            //Create object on jpanel
        counter++;
        switch (counter){
            case 1: jPanel2.add(object1);
                break;
            case 2: jPanel2.add(object2);
                break;
            case 3: jPanel2.add(object3);
                break;
            case 4: jPanel2.add(object4);
                break;
            case 5: jPanel2.add(object5);
                break;
            case 6: jPanel2.add(object6);
                break;
            case 7: jPanel2.add(object7);
                break;
            case 8: jPanel2.add(object8);
                break;
            case 9: jPanel2.add(object9);
                break;
            case 10: jPanel2.add(object10);
                break;
        }
        
        jPanel2.revalidate();
        jPanel2.repaint();  
      
            //Drag and drop
      ComponentMover cm = new ComponentMover();
      cm.registerComponent(object1);
      cm.registerComponent(object2);
      cm.registerComponent(object3);
      cm.registerComponent(object4);
      cm.registerComponent(object5);
      cm.registerComponent(object6);
      cm.registerComponent(object7);
      cm.registerComponent(object8);
      cm.registerComponent(object9);
      cm.registerComponent(object10);
      
            //Right click menu for objects
    Pmenu = new JPopupMenu();
      JMenuItem editobject = new JMenuItem("Edit name");
      Pmenu.add(editobject);
      JMenuItem deleteobject = new JMenuItem("Delete");
      Pmenu.add(deleteobject);
      JMenuItem addattribute = new JMenuItem("Add attributes");
      Pmenu.add(addattribute);
      
      //right click functions - needs to reference object it is clicked from
      editobject.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e){
          String a = JOptionPane.showInputDialog(null, "Edit object name");
          object1.setText(a);
      }
      });
      
        deleteobject.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e){
          object1.setVisible(false);
      }
      });
      
      
        //Object mouselisteners
      object1.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
      
        object2.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
        
      object3.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
            
      object4.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
      
          object5.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
                
          object6.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
          
         object7.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
         
             object8.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
             
         object9.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
                
         object10.addMouseListener(new MouseAdapter(){
      public void mouseReleased(MouseEvent Me){
      if(Me.isPopupTrigger()){
      Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
      }
      }
      });
    
        }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Some problems with components added to jpanel

    First off, I highly recommend against using a GUI builder if you don't know the fundamentals of the underlying API - in this case Swing. Using a GUI builder prevents you from learning customization, maintanance, or simply the skills to read someone else's code. Second, I recommend concentrating on one problem at a time, and post an SSCCE that demonstrates that problem.

  3. #3
    acs5538 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Some problems with components added to jpanel

    Quote Originally Posted by doWhile View Post
    First off, I highly recommend against using a GUI builder if you don't know the fundamentals of the underlying API - in this case Swing. Using a GUI builder prevents you from learning customization, maintanance, or simply the skills to read someone else's code. Second, I recommend concentrating on one problem at a time, and post an SSCCE that demonstrates that problem.
    What makes you think I don't know the basics of the API?

    Okay, I will narrow down what I am focusing on. If anyone makes any attempt at helping me I'd most like it to be with this- how do I make the right click menu options affect only the components they are clicked from? Right now I have
    Java Code:
      editobject.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e){
          String a = JOptionPane.showInputDialog(null, "Edit object name");
          object1.setText(a);
      }
      });
       
        deleteobject.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e){
          object1.setVisible(false);
      }
      });
    As you can see the actionPerformed both reference object1. But I actually want them to reference the component they are clicked from. This seems pretty basic, I just can't think how to do it.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Some problems with components added to jpanel

    Quote Originally Posted by acs5538 View Post
    What makes you think I don't know the basics of the API?
    Read the first line of your original post, in particular where you say "I am new to java"

    If anyone makes any attempt at helping me
    Now there is a show of gratitude. I wish you the best of luck solving your problem, as I guess you don't consider my responses attempts to help, in which case I'm done helping.

  5. #5
    acs5538 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Some problems with components added to jpanel

    Quote Originally Posted by doWhile View Post
    Read the first line of your original post, in particular where you say "I am new to java"



    Now there is a show of gratitude. I wish you the best of luck solving your problem, as I guess you don't consider my responses attempts to help, in which case I'm done helping.
    Yes, and I am posting this on the New To Java board. But I have some experience with C++ and I have been working with this for a couple of weeks with tutorials and such. I was asking that because I thought you saw something indicative of that in my code. I apologize for offending you, but I did not intend to. I never said you weren't making an attempt to help, and I thought we would be looking at my code here, not my wording.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Some problems with components added to jpanel

    Quote Originally Posted by acs5538 View Post
    Yes, and I am posting this on the New To Java board. But I have some experience with C++ and I have been working with this for a couple of weeks with tutorials and such.
    Swing is complicated and extensive - my advice to avoid a GUI builder is not dependent upon your programming experience, but your experience with Swing itself. I have seen many individuals over the years who could have solved problems much faster if they learned the low level attributes of Swing rather than the nuances of a builder.

    Quote Originally Posted by acs5538 View Post
    I was asking that because I thought you saw something indicative of that in my code.
    It is difficult to see anything indicative in your code, which is why I asked in my first post for an SSCCE - demonstrate the problem in as little code as possible - something that we can compile, debug, and easily reproduce the problem (often in the process of doing just this the solution manifests itself)

    And to answer one of your questions....

    As you can see the actionPerformed both reference object1. But I actually want them to reference the component they are clicked from
    You can create separate menus for each. Alternatively - and a general suggestion - you may wish to create an array of your components, so all those lines of code boil down to a few lines within a loop. This would open up an alternative method which would be to push the index in the array through the action commands of the popup menu to the final ActionListener. And yet another alternative to do this would be to set a variable to store which object fired the pop up menu event.
    DarrylBurke likes this.

Similar Threads

  1. Setting a JPanel height after a menu has been added?
    By jazzermonty in forum AWT / Swing
    Replies: 12
    Last Post: 02-29-2012, 12:49 AM
  2. Replies: 0
    Last Post: 02-19-2012, 06:53 PM
  3. Multiple components in Jpanel
    By shrutisurulkar in forum AWT / Swing
    Replies: 1
    Last Post: 01-07-2011, 02:13 PM
  4. Components Layout in a JPanel
    By jboy in forum New To Java
    Replies: 4
    Last Post: 10-11-2009, 12:08 PM
  5. Replies: 1
    Last Post: 04-01-2009, 02:00 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
  •