Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: need a dropdown multiselection list

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default need a dropdown multiselection list

    hi there.
    Im creating a gui, and in it i need a component to display a list of checkboxes to a user in a dropdown fashion such that
    a user can check any number of boxes before the component folds.

    I have tried to create a component by adding check boxes into a Jpanel into a JScrollPane into another JPanel
    which also contains a label as a title, a button for drop down and other two buttons (one for selecting all boxes and the other for selecting only checked boxes)

    But the problem is, the JScrollpane doesnt get notified when the size of the panel containing checkboxes is larger than it can be viewed therefore scrollbars do not work

    Here is my code
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.List;
    import java.util.Vector;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
     
    /**
     *
     * 
     */
    public class Test extends JPanel{
     
        JScrollPane sb;
        JPanel panel;
        List itemCB;
        List items;
        JButton b,all,marked;
        int height = 25,width = 200;
        int cbWidth = 25;
        int xshift = 5,yshift = 5;
     
        String heading;
     
        public Test(String heading){
            this.heading = heading;
            setBackground(new Color(230,230,250));
            setLayout(null);
            items = new Vector();
            itemCB = new Vector();
            initComponent();
        }
        private void initComponent(){
            b = new JButton(){
                public void paint(Graphics g){
                    super.paint(g);
                    int x[] = {10,13,16};
                    int y[] = {9,15,9};
                    g.fillPolygon(x, y, 3);
                }
            };
            b.setSize(cbWidth, height);
            b.setLocation(width-cbWidth,0);
            b.addActionListener(new ActionListener(){
                private boolean clicked = false;
                public void actionPerformed(ActionEvent e){
                    if(!clicked){
                        showItems();
                        clicked = true;
                    }
                    else{
                        setSize(width,height);
                        clicked = false;
                    }
                }
            });
            add(b);
     
            Label l = new Label(heading){
                public void paint(Graphics g){
                    g.setColor(Color.lightGray);
                    g.drawRect(0,0, getWidth()-1,getHeight()-1);
                }
            };
            l.setSize(width-cbWidth,height);
            l.setLocation(0,0);
            add(l);
     
            sb = new JScrollPane();
            sb.setSize(width,height*(items.size()+1));
            sb.setLocation(1, height+1);
            sb.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
     
            add(sb);
     
            all = new JButton("All");
            all.setSize(50, height);
            all.setLocation(width-55,getHeight()-30);
            all.addActionListener(new ActionListener(){
                boolean selected = false;
                public void actionPerformed(ActionEvent e){
                    b.getActionListeners()[0].actionPerformed(e);
                    if(selected){
                        for(int i=0;i<itemCB.size();i++)
                            ((JCheckBox)itemCB.get(i)).setSelected(false);
                        selected = false;
                    }
                    else{
                        for(int i=0;i<itemCB.size();i++)
                            ((JCheckBox)itemCB.get(i)).setSelected(true);
                        selected = true;
                    }
                }
            });
            add(all);
     
            marked = new JButton("Marked");
            marked.setSize(80, height);
            marked.setLocation(width-150,getHeight()-30);
            marked.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    b.getActionListeners()[0].actionPerformed(e);
                    getSelectedItems();
                }
            });
            add(marked);
     
            setSize(width,height);
        }
     
        public void addItem(String text){
            items.add(text);
            final JCheckBox cb = new JCheckBox(text);
            cb.setSize(width,height);
            cb.setLocation(0,height*(items.size()-1));
            cb.addMouseListener(new MouseAdapter(){
                boolean clicked = false;
                @Override
                public void mouseEntered(MouseEvent m){
                    cb.setBackground(Color.lightGray);
                }
                @Override
                public void mouseExited(MouseEvent m){
                    cb.setBackground(new Color(240,240,240));
     
                }
            });
            itemCB.add(cb);
        }
     
        private void showItems(){
            panel = new JPanel();
            panel.setLayout(null);
     
            for(int i=0;i<itemCB.size();i++){
                panel.add((JCheckBox)itemCB.get(i));
            }
     
     
            panel.setSize(width - 2,height*(items.size()+1)-height);
            if(items.size()<9){
                sb.setSize(width-2,height*(items.size()+1)-height);
                setSize(width,height*(items.size()+1)+height+yshift*2);
            }
            else{
                sb.setSize(width-2,height*(9)-height);
                setSize(width,height*(9)+height+yshift*2);
            }
            sb.setViewportView(panel);
            marked.setLocation(width-140,getHeight()-30);
            all.setLocation(width-55,getHeight()-30);
        }
     
        public void setMarkedButtonActionListener(ActionListener al){
            marked.addActionListener(al);
        }
     
        public void setAllButtonActionListener(ActionListener al){
            all.addActionListener(al);
        }
     
        public List getSelectedItems(){
            List selectedItems = new Vector();
            for(int i=0;i<itemCB.size();i++){
                if(((JCheckBox)itemCB.get(i)).isSelected()){
                    selectedItems.add(items.get(i));
                }
            }
            if(selectedItems.isEmpty())
                return null;
            return selectedItems;
        }
     
        public List getAllItems(){
            return items;
        }
     
        @Override
        public void paint(Graphics g){
            super.paint(g);
            g.drawRect(0,height, getWidth()-1,getHeight()-height-1);
        }
        public static void main(String ar[]){
            JFrame f = new JFrame();
            f.setLayout(null);
            f.setSize(500,500);
            f.setLocation(400,200);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Test sp = new Test("");
            sp.setLocation(20, 100);
            for(int i=0;i<10;i++)
            sp.addItem("Item "+(i+1));
            f.add(sp);
            f.setVisible(true);
        }
    }

    Any help please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,051
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: need a dropdown multiselection list

    This suggestion is pretty cool, but it is dated. It may also require tweaking to not fold up every time a selection is made, but it's still a good starting point.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: need a dropdown multiselection list

    i tried the pretty cool suggestion of yours before I wrote the code I posted.
    The problem is I coulnt find out what listener of the ComboBox to deal with so that I could stop it from folding upon single selection.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: need a dropdown multiselection list

    fixed it.
    just a layout problem.
    instead of:
    panel.setLayout(null);

    I put:
    panel.setLayout(new BoxLayout(panel,1));
    also added the maximum size of the checkboxes.
    there I got my dropdown multiselection list working.
    thanks @ GregBrannon.

Similar Threads

  1. Why width of dropdown is decreasing automatically on every select?
    By ayush1989 in forum Other Programming Languages
    Replies: 0
    Last Post: February 1st, 2013, 12:39 AM
  2. Replies: 19
    Last Post: December 2nd, 2012, 04:05 PM
  3. displaying data in 4 tier cascading dropdown list
    By javajobs29 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 29th, 2012, 05:55 AM
  4. How to retrieve a selected value in dropdown using JSTL
    By abc21 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 4th, 2012, 07:50 PM
  5. Button with Dropdown Menu?
    By Manish87 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 14th, 2011, 12:05 AM