About AWT List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitpathak
    New Member
    • Jan 2007
    • 20

    About AWT List

    Hello

    I am trying to attach a pop up menu in List
    That is going fine
    But I donno why for some area it is showing and for another it is not
    Please help me.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by amitpathak
    Hello

    I am trying to attach a pop up menu in List
    That is going fine
    But I donno why for some area it is showing and for another it is not
    Please help me.
    Can you show us some snippets of (relevant) code please?

    kind regards,

    Jos

    Comment

    • amitpathak
      New Member
      • Jan 2007
      • 20

      #3
      Originally posted by JosAH
      Can you show us some snippets of (relevant) code please?

      kind regards,

      Jos

      // Copyright (c) 2007 Ericsson AB. All rights reserved.

      package testpackage;

      import java.awt.Color;
      import java.awt.GridLa yout;
      import java.awt.List;
      import java.awt.event. ActionEvent;
      import java.awt.event. ActionListener;
      import java.awt.event. MouseAdapter;
      import java.awt.event. MouseEvent;
      import java.awt.event. MouseListener;

      import javax.swing.JFr ame;
      import javax.swing.JLa bel;
      import javax.swing.JMe nuItem;
      import javax.swing.JPo pupMenu;
      import javax.swing.JTe xtArea;

      public class CTestClass implements ActionListener
      {
      private JFrame frame;
      private JTextArea area;
      private JLabel label;
      private List wordList;
      public static JPopupMenu popup;

      public CTestClass()
      {
      frame = new JFrame("popup menu");
      frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
      frame.getConten tPane().setLayo ut(new GridLayout(3, 1));

      area = new JTextArea();
      label = new JLabel("Sample" );
      wordList = new List();

      popup = new JPopupMenu();
      JMenuItem menuItem = new JMenuItem("A popup menu item");
      menuItem.addAct ionListener(thi s);
      popup.add(menuI tem);
      menuItem = new JMenuItem("Anot her popup menu item");
      menuItem.addAct ionListener(thi s);
      popup.add(menuI tem);

      // Add listener to components that can bring up popup menus.
      MouseListener popupListener = new PopupListener() ;
      area.addMouseLi stener(popupLis tener);
      wordList.addMou seListener(popu pListener);
      frame.getConten tPane().add(are a);
      frame.getConten tPane().add(lab el);
      System.out.prin t(popup.getFore ground());

      frame.getConten tPane().add(wor dList);
      frame.setSize(4 00, 300);
      frame.setVisibl e(true);

      }

      public void actionPerformed (ActionEvent e)
      {

      }

      public static void main(String[] args)
      {
      new CTestClass();
      }
      }

      class PopupListener extends MouseAdapter
      {
      public void mousePressed(Mo useEvent e)
      {
      maybeShowPopup( e);
      }

      public void mouseReleased(M ouseEvent e)
      {
      maybeShowPopup( e);
      }

      private void maybeShowPopup( MouseEvent e)
      {
      if (e.isPopupTrigg er())
      {
      CTestClass.popu p.show(e.getCom ponent(), e.getX(), e.getY());
      }
      }
      }

      If you run this program in any editor like eclipse
      It will create frame in which there are 3 rows and 1 column
      0,0 -> TEXTAREA
      1,0 -> LABEL
      2,0 -> List

      I have attached popuplistener to TEXTAREA and LIST
      Now when you right click at any plcae within TEXTAREA it is working fine
      but try the same in LIST you will know the problem

      Thanks

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You're mixing AWT objects with Swing objects; the two don't go well together,
        i.e. the AWT objects are 'heavyweight', they use the system's windowing objects.
        Swing doesn't so AWT doesn't know about the Swing objects and vice versa.

        I suggest you'd use a JList instead (and none of the other AWT components).

        kind regards,

        Jos

        Comment

        • amitpathak
          New Member
          • Jan 2007
          • 20

          #5
          Originally posted by JosAH
          You're mixing AWT objects with Swing objects; the two don't go well together,
          i.e. the AWT objects are 'heavyweight', they use the system's windowing objects.
          Swing doesn't so AWT doesn't know about the Swing objects and vice versa.

          I suggest you'd use a JList instead (and none of the other AWT components).

          kind regards,

          Jos

          Thanks Jos
          You got the problem
          Now I used JList and the problem is Solved

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by amitpathak
            Thanks Jos
            You got the problem
            Now I used JList and the problem is Solved
            You're welcome of course; mixing AWT and Swing components is a frequent
            mistake; it can be a bit confusing when you just read the class documentation.

            kind regards,

            Jos

            Comment

            Working...