Results 1 to 1 of 1
  1. #1
    alexander.s is offline Member
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default can't get setVisible(false) to work

    Hi!

    So I'm fetching strings from a database and consequently display them graphically in the form of JCheckBoxes, when the user clicks on one of these boxes some new boxes appear. Which is the point, but the first batch of boxes won't go away.

    So atm everything just gets bunched together. And as you continue clicking you end up with 200 checkboxes not having a clue what's going on.

    Help please :)

    EDIT:
    So I've tried with a lot of diff work arounds none of them really work. I have a list of string results from the database. Everytime I've called my generateMenu I -now- try to delete what is in it so I won't generate "old" checkboxes, and I just can't understand how strn.clear() and strn = new ArrayList<String>(); just has no effect at all ?! The next time the method runs it somehow remembers what was in the the list of strings before I cleared it, even though System.out.print(strn.toString()); says it's empty ?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class Main extends JPanel {
        ButtonListener bl = new ButtonListener(this);
        TextFieldListener tfl = new TextFieldListener(this);
        ArrayList<Box> boxes = new ArrayList<Box>();
        ArrayList<JPanel> panels = new ArrayList<JPanel>();
        Query query = new Query();
       
        TextField text;
        ArrayList<JButton> buttons;
        ArrayList<JCheckBox> checkboxes;
        ArrayList<String> strn;
        JFrame frame;
        int count=1;
        Box b0;
    
        JPanel bottomPanel,topPanel;
        public Main(){
            strn = new ArrayList<String>();
    
            JButton search = new JButton("Search");
            text = new TextField("",30);
            checkboxes = new ArrayList<JCheckBox>();
            frame = new JFrame();
            bottomPanel = new JPanel();
           
            topPanel = new JPanel();
            frame.setSize(800, 800);
            strn.addAll(query.getQuery("SELECT Name FROM Utrymme"));
            Collections.sort(strn);
            b0 = new Box(BoxLayout.Y_AXIS);
            boxes.add(b0);
            for(String str : strn){
    
                JCheckBox jb = new JCheckBox(str);
                checkboxes.add(jb);
                jb.addActionListener(bl);
                b0.add(jb);
                if(count%10==0){
                    bottomPanel.add(b0);
                    b0 = new Box(BoxLayout.Y_AXIS);
                    boxes.add(b0);
                }
                count++;
            }
            search.addActionListener(tfl);
            text.addActionListener(tfl);
            
            topPanel.add(text);
            topPanel.add(search);
    
            frame.getContentPane().add(topPanel, BorderLayout.NORTH);
            frame.getContentPane().add(bottomPanel, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public static void main(String[] args){
             Main m = new Main();
    
        }
        public void hideMenu(){
          for(JCheckBox j: checkboxes){
            j.setVisible(false);
          }
          bottomPanel.updateUI();
        }
        public void generateMenu(ArrayList<String> s){
            hideMenu();
            int countz=1;
            b0 = new Box(BoxLayout.Y_AXIS);
            boxes.add(b0);
            for(String string : s){
                JCheckBox jj = new JCheckBox(string);
                checkboxes.add(jj);
                jj.addActionListener(bl);
                b0.add(jj);
                if(countz%10==0){
                    bottomPanel.add(b0);
                    b0 = new Box(BoxLayout.Y_AXIS);
                    boxes.add(b0);
                }
                countz++;
            }
            countz=0;
            bottomPanel.updateUI();
        }
    }
    class TextFieldListener implements ActionListener{
        Main main;
        public TextFieldListener(Main main){
            this.main = main;
        }
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equalsIgnoreCase("Search")){
                main.text.getText();
            }
        }
    }
    class ButtonListener implements ActionListener {
        boolean utrymme=false,enheter=false;
        ArrayList<String> temp = new ArrayList<String>();
        Main main;
        int n=0;
      public ButtonListener(){}
      public ButtonListener(Main main) {
        this.main = main;
      }
      public void actionPerformed(ActionEvent e) {
        
        
          for(String str : main.strn){
                if (e.getActionCommand().equals(str) && n==0) {
                    
                    temp.addAll(main.query.getQuery("SELECT Child FROM rel_u_e " +
                            "WHERE Parent='"+str+"' AND Active='True'"));
                    Collections.sort(temp);
                    main.generateMenu(temp);
                    utrymme=true;
                }
          }
          
          for(String strr : temp){
                if (e.getActionCommand().equals(strr) && n==1) {
                     
                    ArrayList<String> lista = new ArrayList<String>();
                    lista.addAll(main.query.getQuery("SELECT Child FROM rel_e_f " +
                            "WHERE Parent='"+strr+"' AND Active='True'"));
                    Collections.sort(lista);
                    main.generateMenu(lista);
                    enheter=true;    
                }
          }
          n++;
          
           
      }
    }
    Last edited by alexander.s; 04-27-2010 at 11:03 PM.

Similar Threads

  1. resultset.next() = false ;; Can't Guess why.. please help.
    By Stephen Douglas in forum New To Java
    Replies: 11
    Last Post: 04-12-2010, 10:21 AM
  2. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM
  3. Replies: 4
    Last Post: 02-26-2009, 11:46 PM
  4. [SOLVED] Problem with setVisible(); on LINUX
    By lepetitprince in forum AWT / Swing
    Replies: 9
    Last Post: 11-22-2008, 04:14 PM
  5. setVisible(true) does not work
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •