Joption acting like it's in a for loop

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 1608 Views - Last Post: 04 February 2018 - 10:37 AM Rate Topic: -----

#1 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Joption acting like it's in a for loop

Posted 20 January 2018 - 01:56 PM

I'm working on a program to enter and search for contacts.

Contact Class
package contact;

import java.awt.Component;
import java.io.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Contact implements Serializable {
 String name;
 String number;
 ArrayList <Contact>contacts=new ArrayList();
 ArrayList <Contact>a=new ArrayList();
public boolean exist=false;
static final long serialVersionUID = -1070056703658855062L;
Contact(String name,String number) throws IOException, ClassNotFoundException{
    this.name=name;
    this.number=number;
    File Dir = new File("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\Contacts");
    if (!Dir.exists()) {
        Dir.mkdir();
    }
    this.contacts.add(this);
    File txt=new File("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\Contacts\\Contacts.txt");
    if(!txt.exists()){
        txt.createNewFile();
        
    }
    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\Contacts\\Contacts.txt"));     
if (br.readLine()!= null) {
System.out.println("hello");
    FileInputStream inFileStream=new FileInputStream(txt);
    ObjectInputStream inObjectStream=new ObjectInputStream(inFileStream);
 a = (ArrayList <Contact>) inObjectStream.readObject();
for(int i=0;i<a.size();i++){
    if(this.name.equals(a.get(i).name)){
        System.out.println("contact already exist");
        exist=true;
    }
        }}
if(exist==false){
      FileOutputStream outFileStream = new FileOutputStream(txt);
        ObjectOutputStream outObjectStream =
                new ObjectOutputStream(outFileStream);
       a.add(this);
       outObjectStream.writeObject(a);
        outObjectStream.close();
        JOptionPane.showConfirmDialog(null,"Contact successfuly added" , "alert", JOptionPane.DEFAULT_OPTION);
  
}  
else{
 JOptionPane.showConfirmDialog(null,"Contact already exist" , "alert", JOptionPane.DEFAULT_OPTION);   
}}

public static ArrayList<Contact> readContactFile() throws FileNotFoundException, IOException, ClassNotFoundException{
    File txt=new File("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\Contacts\\Contacts.txt");
    FileInputStream inFileStream=new FileInputStream(txt);
    ObjectInputStream inObjectStream=new ObjectInputStream(inFileStream);
ArrayList <Contact> a = (ArrayList <Contact>) inObjectStream.readObject();    
return a;
}

}   


CreateContactGUI
package contact;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class CreateContactGui {
    JFrame createContactFrame;
    JPanel pnl_top,pnl_topLeft,pnl_topCenter,pnl_left,pnl_right,pnl_bot,pnl_name,pnl_phoneNumber;
    JLabel lbl_createContact,lbl_name,lbl_phoneNumber,lbl_jbs,lbl_back;
    JTextField tf_name,tf_phoneNumber;
    JButton btn_addContact;
    ImageIcon jbs,back;
    CreateContactGui(){
        
     
        createContactFrame=new JFrame("number book");
        
        pnl_top=new JPanel();
        pnl_top.setLayout(new BorderLayout());
        pnl_topLeft=new JPanel();
        back = new ImageIcon("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\projet2018\\src\\projet2018\\back.png");
        lbl_back = new JLabel(back);
        pnl_topLeft.add(lbl_back);
        pnl_topCenter=new JPanel();
        lbl_createContact=new JLabel("Create Contact");
        lbl_createContact.setFont(new Font("Arial",Font.BOLD,25));
        lbl_createContact.setForeground(Color.blue);
        pnl_topCenter.add(lbl_createContact);
        pnl_top.add(pnl_topLeft,BorderLayout.WEST);
        pnl_top.add(pnl_topCenter,BorderLayout.CENTER);
        pnl_left=new JPanel();
        pnl_left.setLayout(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=0;c.gridy=0;c.anchor=GridBagConstraints.WEST;c.insets=new Insets(0,0,30,0);
        pnl_name=new JPanel();
        pnl_name.setLayout(new FlowLayout(FlowLayout.LEFT,30,0));
        lbl_name=new JLabel("Name");
        tf_name=new JTextField(20);
        pnl_name.add(lbl_name);
        pnl_name.add(tf_name);
        pnl_left.add(pnl_name,c);
        
        c.gridx=0;c.gridy=1;
        pnl_phoneNumber=new JPanel();
        lbl_phoneNumber=new JLabel("Phone Number");
        tf_phoneNumber=new JTextField(20);
        pnl_phoneNumber.add(lbl_phoneNumber);
        pnl_phoneNumber.add(tf_phoneNumber);
        pnl_left.add(pnl_phoneNumber,c);
        

        
        pnl_right=new JPanel();
        jbs=new ImageIcon("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\jbs.jpg");
        lbl_jbs=new JLabel(jbs);
        pnl_right.add(lbl_jbs);
        
        pnl_bot=new JPanel();
        btn_addContact=new JButton("Add Contact");
        pnl_bot.add(btn_addContact);
        
        createContactFrame.add(pnl_top,BorderLayout.NORTH);
        createContactFrame.add(pnl_left,BorderLayout.WEST);
        createContactFrame.add(pnl_right,BorderLayout.EAST);
        createContactFrame.add(pnl_bot,BorderLayout.SOUTH);
        createContactFrame.pack();
        createContactFrame.setVisible(true);
        
        tf_name.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!isEmpty()){
                try {
                    new Contact(tf_name.getText(),tf_phoneNumber.getText());
                } catch (IOException ex) {
                    Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                }
            }}
        });
        
        tf_phoneNumber.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!isEmpty()){
                    try {
                        new Contact(tf_name.getText(),tf_phoneNumber.getText());
                    } catch (IOException ex) {
                        Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }}
        });
                
        btn_addContact.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!isEmpty()){
                try {
                    new Contact(tf_name.getText(),tf_phoneNumber.getText());
                } catch (IOException ex) {
                    Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreateContactGui.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(JOptionPane.OK_OPTION==0){
                    tf_name.setText("");
                    tf_phoneNumber.setText("");
                    
                }
            }}
        });
        
        lbl_back.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
               createContactFrame.dispose();
               new HomeFrameGui();
            }

            @Override
            public void mousePressed(MouseEvent e) {
               
            }

            @Override
            public void mouseReleased(MouseEvent e) {
               
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                
            }

            @Override
            public void mouseExited(MouseEvent e) {
           
            }
        });
    }
      public boolean  isEmpty(){
          boolean bool=false;
          if(tf_name.getText().equals("")&&(tf_phoneNumber.getText().equals(""))){
              bool=true;
              JOptionPane.showConfirmDialog(null,"Name TextField and PhoneNumber TextField are empty" , "alert", JOptionPane.DEFAULT_OPTION);
          }
          else if(tf_name.getText().equals("")){
              bool=true;
          JOptionPane.showConfirmDialog(null,"Name TextField is empty" , "alert", JOptionPane.DEFAULT_OPTION);   
         }
          else if(tf_phoneNumber.getText().equals("")){
              bool=true;
             JOptionPane.showConfirmDialog(null,"PhoneNumber TextField is empty" , "alert", JOptionPane.DEFAULT_OPTION);
         }   
          return bool;  
       }
         
}


SearchContactGUI
package contact;

import static contact.Contact.readContactFile;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

public class SearchContactGui {

    JFrame searchContactFrame;
    JPanel pnl_top,pnl_topLeft,pnl_topCenter, pnl_left, pnl_right, pnl_search;
    JList l_names;
    JScrollPane scrollPane;
    JLabel lbl_searchContact, lbl_jbs, lbl_search,lbl_back;
    JTextField tf_search;
    ImageIcon jbs,back;
    ArrayList<String> matchedNames, matchedNumber,contactsNames;
    ArrayList<Contact> contacts=new ArrayList<Contact>();

    SearchContactGui() {

        searchContactFrame = new JFrame("number book");
        pnl_top = new JPanel();
        pnl_top.setLayout(new BorderLayout());
        back = new ImageIcon("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\projet2018\\src\\projet2018\\back.png");
        lbl_back = new JLabel(back);
        pnl_topLeft=new JPanel();
        pnl_topLeft.add(lbl_back);
        pnl_topCenter=new JPanel();
        lbl_searchContact = new JLabel("Search for Contact");
        lbl_searchContact.setFont(new Font("Arial", Font.BOLD, 25));
        lbl_searchContact.setForeground(Color.blue);
        pnl_topCenter.add(lbl_searchContact);
        pnl_top.add(pnl_topLeft,BorderLayout.WEST);
        pnl_top.add(pnl_topCenter,BorderLayout.CENTER);

        pnl_left = new JPanel();
        pnl_left.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        pnl_search = new JPanel();
        lbl_search = new JLabel("Search");
        tf_search = new JTextField(18);
        pnl_search.add(lbl_search);
        pnl_search.add(tf_search);
        pnl_left.add(pnl_search, c);
        c.anchor = GridBagConstraints.EAST;
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(20, 0, 0, 9);
        try {
            contacts=readContactFile();
        } catch (IOException ex) {
            Logger.getLogger(SearchContactGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SearchContactGui.class.getName()).log(Level.SEVERE, null, ex);
        }
        contactsNames=new ArrayList<String>();
        for(int i=0;i<contacts.size();i++){
            contactsNames.add(contacts.get(i).name);
        }
        Collections.sort(contactsNames);
        l_names = new JList(contactsNames.toArray());
        scrollPane = new JScrollPane();
        scrollPane.setViewportView(l_names);
        scrollPane.setPreferredSize(new Dimension(200, 150));
        pnl_left.add(scrollPane, c);

        pnl_right = new JPanel();
        jbs = new ImageIcon("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\jbs.jpg");
        lbl_jbs = new JLabel(jbs);
        pnl_right.add(lbl_jbs);
        searchContactFrame.add(pnl_top, BorderLayout.NORTH);
        searchContactFrame.add(pnl_left, BorderLayout.WEST);
        searchContactFrame.add(pnl_right, BorderLayout.EAST);
        searchContactFrame.pack();
        searchContactFrame.setVisible(true);

        tf_search.addKeyListener(new KeyListener() {
            
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

                if (e.getKeyCode() != KeyEvent.VK_ENTER) {
                        
                    matchedNumber = new <String>ArrayList();
                    matchedNames = new <String>ArrayList();
                    try {
                        ArrayList<Contact> a = readContactFile();

                        for (int j = 0; j < a.size(); j++) {
                            if (a.get(j).name.length() >= tf_search.getText().length()) {

                                if (a.get(j).name.substring(0, tf_search.getText().length()).equals(tf_search.getText())) {
                                    matchedNames.add(a.get(j).name);
                                    matchedNumber.add(a.get(j).number);

                                   
                                }
                            }
                        }
                        Collections.sort(matchedNames);
                        l_names = new JList(matchedNames.toArray());
                        scrollPane.setViewportView(l_names);
                   
                    } catch (IOException ex) {
                        Logger.getLogger(SearchContactGui.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(SearchContactGui.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    l_names.requestFocus();
                    l_names.setSelectedIndex(0);

                    l_names.addKeyListener(new KeyListener() {
                        @Override
                        public void keyTyped(KeyEvent e) {

                        }

                        @Override
                        public void keyPressed(KeyEvent e) {

                        }

                        @Override
                        public void keyReleased(KeyEvent e) {
                            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                                

                                int input = JOptionPane.showOptionDialog(searchContactFrame, matchedNumber.get(l_names.getSelectedIndex()), "title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
                                if (input == JOptionPane.OK_OPTION) {
                                    System.out.println("done");
                                    tf_search.requestFocus();
                                   
                                    

                                }
                               
                            }
                        }

                    });
                }

            }
        });
        
          lbl_back.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
               searchContactFrame.dispose();
               new HomeFrameGui();
            }

            @Override
            public void mousePressed(MouseEvent e) {
               
            }

            @Override
            public void mouseReleased(MouseEvent e) {
               
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                
            }

            @Override
            public void mouseExited(MouseEvent e) {
           
            }
        });

    }
}



Now here's my problem,when searching for contact on enter i move to the list of names and when i click enter in the list of names a JOption box appear with the number of the contact now i try to reput the selection to the search textfield but i can't .
PS: i'm sure it's something stupid but my mind is blocked can't figure it out.

Is This A Good Question/Topic? 0
  • +

Replies To: Joption acting like it's in a for loop

#2 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 20 January 2018 - 02:33 PM

Where is the class with the main() method?
Or how do you execute the code?
Was This Post Helpful? 0
  • +
  • -

#3 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 20 January 2018 - 02:36 PM

package contact;

import java.io.IOException;

public class NumberBook {
public static void main(String[] args) throws IOException, ClassNotFoundException {
new HomeFrameGui();

    
    }
}


HomeFrameGUI
package contact;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HomeFrameGui {
    JFrame homeFrame;
    JPanel pnl_top,pnl_left,pnl_right;
    JLabel lbl_numberBook,lbl_jbsImage;
    JButton btn_createContact,btn_searchContact,btn_showAllContacts;
    ImageIcon jbs;
    HomeFrameGui(){
        homeFrame=new JFrame("number book");
        
        pnl_top=new JPanel();
        lbl_numberBook=new JLabel("Number Book");
        lbl_numberBook.setFont(new Font("Arial",Font.BOLD,25));
        lbl_numberBook.setForeground(Color.blue);
        pnl_top.add(lbl_numberBook);
        
        pnl_left=new JPanel();
        pnl_left.setLayout(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=0;c.gridy=0;c.insets=new Insets(0,0,30,0);
        btn_createContact=new JButton("Create new Contact");
        pnl_left.add(btn_createContact,c);
        
        c.gridx=0;c.gridy=1;
        btn_searchContact=new JButton("Search for Contact");
        pnl_left.add(btn_searchContact,c);
        
        c.gridx=0;c.gridy=2;
        btn_showAllContacts=new JButton("Show all Contacts");
        pnl_left.add(btn_showAllContacts,c);
        
        pnl_right=new JPanel();
        jbs=new ImageIcon("C:\\Users\\ViSion\\Documents\\NetBeansProjects\\Contact\\src\\contact\\jbs.jpg");
        lbl_jbsImage=new JLabel(jbs);
        pnl_right.add(lbl_jbsImage);
        
        homeFrame.add(pnl_top,BorderLayout.NORTH);
        homeFrame.add(pnl_left,BorderLayout.WEST);
        homeFrame.add(pnl_right,BorderLayout.EAST);
        homeFrame.pack();
        homeFrame.setVisible(true);
        
        btn_createContact.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               homeFrame.dispose();
               new CreateContactGui();
            }
        });
        
        btn_searchContact.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                homeFrame.dispose();
                new SearchContactGui();
            }
        });
    }
}


This post has been edited by ViSion91: 20 January 2018 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 20 January 2018 - 02:57 PM

There are over 500 lines of code there, besides there being lots of required files for the program to execute.

How are you trying to debug the code to see what it is doing?

You will get better help if you reduce the size of the code and remove the external files.
Make a small, simpler, self contained program that compiles and executes and shows the problem.
Was This Post Helpful? 1
  • +
  • -

#5 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 02:31 AM

Forget about the code think of it this way, i have a JList having a key listener.
if the key pressed is enter, a JOptionPane.showOptionDialog opens, its an OK_CANCEL_OPTION now when i click enter to choose ok the optionPane opens again and it keeps opening everytime i click enter because the focus is still on the JList
i tried using pane.requestFocus() but for some reasonthe focus is still in the JList.
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 06:09 AM

Ok, if the problem is that easy to describe,
can you Make a small, simpler, self contained program that compiles and executes and shows the problem.
Was This Post Helpful? 0
  • +
  • -

#7 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 06:50 AM

Sure thing, i'll make it as soon as possible maybe tonight.
Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 06:55 AM

That will make it easier to work on.
Was This Post Helpful? 0
  • +
  • -

#9 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 11:23 AM

Here's a smaller version that shows the problem

package test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

/**
 *
 * @author ViSion
 */
public class Test {

    public static class SearchContactGui {

        JFrame searchContactFrame;
        JPanel pnl_top, pnl_topLeft, pnl_topCenter, pnl_left, pnl_search;
        JList l_names;
        JScrollPane scrollPane;
        JLabel lbl_searchContact, lbl_search;
        JTextField tf_search;
        ArrayList<String> matchedNames, matchedNumber, contactsNames;

        SearchContactGui() {

            searchContactFrame = new JFrame("number book");
            pnl_top = new JPanel();

            lbl_searchContact = new JLabel("Search for Contact");
            lbl_searchContact.setFont(new Font("Arial", Font.BOLD, 25));
            lbl_searchContact.setForeground(Color.blue);
            pnl_top.add(lbl_searchContact, BorderLayout.NORTH);

            pnl_left = new JPanel();
            pnl_left.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            pnl_search = new JPanel();
            lbl_search = new JLabel("Search");
            tf_search = new JTextField(18);
            pnl_search.add(lbl_search);
            pnl_search.add(tf_search);
            pnl_left.add(pnl_search, c);
            c.anchor = GridBagConstraints.EAST;
            c.gridx = 0;
            c.gridy = 1;
            c.insets = new Insets(20, 0, 0, 9);

            contactsNames = new ArrayList<String>();
            contactsNames.add("bernard");
            contactsNames.add("brandon");
            contactsNames.add("ben");

            Collections.sort(contactsNames);
            l_names = new JList(contactsNames.toArray());
            scrollPane = new JScrollPane();
            scrollPane.setViewportView(l_names);
            scrollPane.setPreferredSize(new Dimension(200, 150));
            pnl_left.add(scrollPane, c);

            searchContactFrame.add(pnl_top, BorderLayout.NORTH);
            searchContactFrame.add(pnl_left, BorderLayout.WEST);
            searchContactFrame.pack();
            searchContactFrame.setVisible(true);
            searchContactFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            tf_search.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {

                }

                @Override
                public void keyPressed(KeyEvent e) {

                }

                @Override
                public void keyReleased(KeyEvent e) {

                    if (e.getKeyCode() != KeyEvent.VK_ENTER) {

                        matchedNumber = new <String>ArrayList();
                        matchedNames = new <String>ArrayList();

                        for (int j = 0; j < contactsNames.size(); j++) {
                            if (contactsNames.get(j).length() >= tf_search.getText().length()) {

                                if (contactsNames.get(j).substring(0, tf_search.getText().length()).equals(tf_search.getText())) {
                                    matchedNames.add(contactsNames.get(j));

                                }
                            }
                        }
                        Collections.sort(matchedNames);
                        l_names = new JList(matchedNames.toArray());
                        scrollPane.setViewportView(l_names);

                    } else {
                        l_names.requestFocus();
                        l_names.setSelectedIndex(0);

                        l_names.addKeyListener(new KeyListener() {
                            @Override
                            public void keyTyped(KeyEvent e) {

                            }

                            @Override
                            public void keyPressed(KeyEvent e) {

                            }

                            @Override
                            public void keyReleased(KeyEvent e) {
                                if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                                    JOptionPane pane = new JOptionPane();

                                    int input = pane.showOptionDialog(searchContactFrame, l_names.getSelectedValue().toString(), "title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);

                                    //pane.requestFocus();
                                }
                            }

                        });
                    }

                }
            });

        }
    }

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

}


when the JOptionPane appears click ok using enter not using the mouse ,it works fine using the mouse.
for some reason the names list listener still works when the JOptionPane window is opened .
Was This Post Helpful? 0
  • +
  • -

#10 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 11:36 AM

Quote

when the JOptionPane appears click ok

I do not see an OK to be clicked.
The program shows a window with two input boxes and a title.
Was This Post Helpful? 0
  • +
  • -

#11 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 11:38 AM

put a b in the search box and click enter now you'll be in the JList below , use arrows to choose any name from the JList then click enter.
Was This Post Helpful? 0
  • +
  • -

#12 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 01:29 PM

Ok, I can see the problem. I don't have a solution but I feel that using low level events might be wrong.
The code's execution can be changed by removing the list's keylistener when the Enter key is pressed.
That will allow the pressing of the OK button to clear the JOptionPane window and not loop.

This post has been edited by NormR: 21 January 2018 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#13 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 02:00 PM

I've already tried this, your right it fixes the loop but then after clearing the JOptionPane window u can't use enter key to select another name and open another JOptionPane.
Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Joption acting like it's in a for loop

Posted 21 January 2018 - 03:15 PM

I don't have a good solution yet, but I have a cludgey one:
add a flag of some sort that can be used to control calling return (exiting) in the listener every other call.
Was This Post Helpful? 0
  • +
  • -

#15 ViSion91  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 112
  • Joined: 26-March 17

Re: Joption acting like it's in a for loop

Posted 22 January 2018 - 12:00 AM

Do you mind showing an example ?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »