Java Populate JCombobox From Text File

How To Fill A JComboBox With Items From Txt File Using Java NetBeans

Fill JCombobox From Text File Using Java



In this Java Tutorial we will see How To Add Items To JCombobox From Text File Using For Loop + Array + BufferedReader In Java NetBeans .




Project Source Code:

public void fillComboFromTxtFile(){
        
        String filePath = "C:\\Users\\Desktop\\file1.txt";
        File file = new File(filePath);
        
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            Object[] lines = br.lines().toArray();
            
            for(int i = 0; i < lines.length; i++){
                String line = lines[i].toString();
                jComboBox1.addItem(line);
            }
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(jcombo_items_from_txt_file.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

OutPut:

insert items to jcombobx from txt file using java



Java Two Dates Difference In Days

How To Get Days Difference Between 2 Dates Using Java NetBeans

Days Difference Between Two Dates In Java



In this Java Tutorial we will see How To Get The Number Of Days Diffrence Between Two Date Using Calendar On Button Click Event In Java NetBeans .




Project Source Code:

            
private void jButtonDaysDiffActionPerformed(java.awt.event.ActionEvent evt) {                                                
        
        Calendar c1 = new GregorianCalendar(2000,Calendar.JULY,25);
        Calendar c2 = new GregorianCalendar(2007,Calendar.DECEMBER,31);
        
        // 1000 x 60 X 60 X 24 = MS per Day
        
        System.out.println("Days Diff = " + (c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000*60*60*24));
        
    }


OutPut: 
Days Diff = 2715




Java Using Regex

How To Use Regular Expressions In Java NetBeans

Regular Expressions In Java



In this Java Tutorial we will see How To Use Regex (Regular Expressions) To Validate An Email Address And A Phone Number On Button "Check" Click Event Using Java NetBeans .




Project Source Code:


    private void jButtonCheckActionPerformed(java.awt.event.ActionEvent evt) {                                             
       
        //Email Like: mail@mail.com          => ^([\w]+)@([\w]+)\.([\w]+)$
       //URL Like:   http://www.google.com   => ^(http://www\.)([\w]+)\.([\w]+)$
       
       // email
       Pattern pMail = Pattern.compile("^([\\w]+)@([\\w]+)\\.([\\w]+)$");
       Matcher mMail = pMail.matcher(jTextFieldEmail.getText());
       
       boolean isEmailValid = mMail.matches();
       
       //jLabelEmail.setText(Boolean.toString(isEmailValid));
       
       if(isEmailValid){
       
            jLabelEmail.setText("Valid Email");
            jLabelEmail.setForeground(Color.BLUE);
       }else{
           jLabelEmail.setText("InValid Email");
            jLabelEmail.setForeground(Color.red);
       }
       
       // URL
       Pattern pURL = Pattern.compile("^(http://www\\.)([\\w]+)\\.([\\w]+)$");
       Matcher mURL = pURL.matcher(jTextFieldURL.getText());
       
       boolean isURLValid = mURL.matches();
       
      //jLabelURL.setText(Boolean.toString(isURLValid));
        
      if(isURLValid){
       
            jLabelURL.setText("Valid URL");
            jLabelURL.setForeground(Color.green);
       }else{
           jLabelURL.setText("InValid URL");
            jLabelURL.setForeground(Color.red);
       }
       
    } 


OutPut:

Regular Expressions example In Java



Java Add Action To All JButtons

How To Add Action To All Buttons In A JPanel Using Java

add action to all buttons in java



In this Java Tutorial we will see How To Create A Function To Set An ActionListener To All The JButtons Inside A JFrame Using JPanel In Java Programming Language NetBeans IDE .




Project Source Code:

public void addAction()
    {
        Component[] components = jPanel1.getComponents();
        for(Component component : components)
        {
            if(component instanceof JButton)
            {
                JButton button = (JButton) component;
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(button.getText());
                    }
                });
            }
        }
        
    }                                         



////// OUTPUT : 

add ActionListener to all jbuttons in java