Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

Multiple Selectable Options in a Menu

Thread view: 
hellbent4u@gmail.com - 12 Apr 2006 15:41 GMT
HI all,

I am required to populate a menu with the column-names of a table, upon
right-cliking the header. Now I want that the user can select more than
one of the options in the menu. And accordingly I would show only those
columns out of the whole collection of columns originally present in
the table. This is similar to what we get to see upon right-clicking a
table-header in iTunes.

Does someone have an idea as to how to go about it?

Regards,
Ankur
Oliver Wong - 12 Apr 2006 17:50 GMT
> HI all,
>
[quoted text clipped - 6 lines]
>
> Does someone have an idea as to how to go about it?

   I can't find anything tricky about this problem; if you know how to
build GUIs in general, you should be able to tackle this. Build a menu, add
checkbox menuitems. That's it. What have you got so far, and where are you
stuck?

   - Oliver
hellbent4u@gmail.com - 12 Apr 2006 21:40 GMT
I am new to the GUI fundamentals of Java. Could you tell me how to add
JCheckBox as a MenuItem?

Also how to change the look and feel of JCheckBox so that the outer box
disappears.

Regards,
Ankur
Thomas Fritsch - 12 Apr 2006 22:07 GMT
>I am new to the GUI fundamentals of Java. Could you tell me how to add
> JCheckBox as a MenuItem?
It is "JCheckBoxMenuItem".

<http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html> "How
to use Menus"
<http://java.sun.com/docs/books/tutorial/uiswing/components/components.html>
"Visual index of Swing components"
<http://java.sun.com/docs/books/tutorial/uiswing/> "The Swing tutorial"

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Vova Reznik - 12 Apr 2006 22:12 GMT
> I am new to the GUI fundamentals of Java. Could you tell me how to add
> JCheckBox as a MenuItem?
[quoted text clipped - 4 lines]
> Regards,
> Ankur

Oliver mentioned about javax.swing.JCheckBoxMenuItem not JCheckBox.
I think menu is not a very good idea, because any changes like
checking/unchecking will close menu.
May be clicking on some menu item should call JOptionPane or JDialog
with set of possible columns (JCheckBox), for example:
JCheckBox[] columns = new JCheckBox[5];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = new JCheckBox("Column " + (i + 1));
        }

        int answer = JOptionPane.showConfirmDialog(null, columns,
                "Please, select columns...", JOptionPane.YES_NO_OPTION);
        if (answer == JOptionPane.YES_OPTION) {
            for (JCheckBox box : columns) {
                System.out.println(box.getText()
                        + (box.isSelected() ? " selected" : " not selected"));
            }
        } else {
            System.out.println("User didn't want to make changes");
        }

But, first you need fix this: "I am new to the GUI fundamentals of Java".
hellbent4u@gmail.com - 12 Apr 2006 22:47 GMT
Thanx Vova. By being new, I meant, I am not very well conversant with
specific details of GUI API's as in, in the ocean of API's of java,
where one has to really search, I am googling in order to search it
faster.

And Thomas, thanx to you too. I am in the process of trying JCheckBox
out.

here is a snippet of the code. My problem is, the check-boxes are not
getting editted, as in, checked. I've added mouseListener, still.
Please tell me how to go about it.

**************************
class MyPopupMenu extends JPopupMenu implements MouseListener
{
    public MouseListener ml;
    public MyPopupMenu(Vector columns)
    {
        super();
        System.out.println("Inside MyPopupMenu Constructor, Size :
"+columns.size());
        System.out.println((String)columns.elementAt(0));
        JCheckBoxMenuItem[] item = new JCheckBoxMenuItem[columns.size()];

        for(int i =0;i<columns.size();i++)
        {
            item[i] = new JCheckBoxMenuItem((String)columns.elementAt(i));
            item[i].addMouseListener(ml);
            add(item[i]);
        }

    }

    public void mousePressed(MouseEvent evt)
    {
              // if (evt.isPopupTrigger()) {
                   System.out.println("Ankur :
"+(String)((Object)(evt.getComponent()).getClass()));
                   show(evt.getComponent(), evt.getX(), evt.getY());
               //}

    }

    public void mouseReleased(MouseEvent evt)
    {
               System.out.println(" ");
               if (evt.isPopupTrigger()) {
                   show(evt.getComponent(), evt.getX(), evt.getY());
               }
    }

    public void    mouseClicked(MouseEvent evt){System.out.println("Ankur :
"+(String)((Object)(evt.getComponent()).getClass()));}

    public void mouseEntered(MouseEvent evt){System.out.println("Ankur :
"+(String)((Object)(evt.getComponent()).getClass()));}

    public void mouseExited(MouseEvent evt){System.out.println("Ankur :
"+(String)((Object)(evt.getComponent()).getClass()));}

****************************************

last 3 method's i have just filled some code to check if control goes
there...
alas, it isn't...

Do you have any idea how to do event-handling of the
checkboxesMenuItems??

Regards,
Ankur
Thomas Fritsch - 13 Apr 2006 10:27 GMT
> here is a snippet of the code. My problem is, the check-boxes are not
> getting editted, as in, checked. I've added mouseListener, still.
> Please tell me how to go about it.
>
> **************************
> class MyPopupMenu extends JPopupMenu implements MouseListener
[...]
> {
> ****************************************
[quoted text clipped - 5 lines]
> Do you have any idea how to do event-handling of the
> checkboxesMenuItems??
I think the MouseListener approach is the wrong one (despite that you
forgot to call to addMouseListener(...)).
Use ActionListener instead, and don't forget to call
  menuItem.addActionListener(...);
An example about event-handling for menu items in general (not only
JCheckBoxMenuitem) is given in
<http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#event>.
(By the way: It is the same tutorial page I gave you in my first answer)

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.