Header Ads

[Java] JList - select multiple items, checkbox .


CODE : 

package thanhcs_bai17;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Bai17_Jlist_checkbox extends JFrame implements ActionListener, ListSelectionListener{
            JButton btntodenchan, btntodenle,btntodennt, btnboden, btnxoatoden,btntonggtlist, btnthoat, btnnhap;
            JTextField jtxt;
            JCheckBox jcheckam;
            DefaultListModel modelList;
            JList mJlist;
            int select[] = null;
    public Bai17_Jlist_checkbox() {

                setLayout(new BorderLayout());

                JPanel jptitle = new JPanel();
                JPanel jpbotton = new JPanel();
                JPanel jpControl = new JPanel();
                JPanel jplist = new JPanel();
                jplist.setLayout(new BorderLayout());
                jpControl.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.RED),"Control"));
                jplist.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.BLUE),"Jlist"));
                jptitle.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.CYAN),"title"));
                jpbotton.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.ORANGE),"bottom"));
                //title
                jptitle.add(new JLabel("Thao tac Jlist - Checkboox"));
                
                //botton
                jpbotton.add(btnthoat = new JButton("Thoat Chuong Trinh"));
                btnthoat.addActionListener(this);
                
                
                //control
                jpControl.setLayout(new BoxLayout(jpControl, BoxLayout.Y_AXIS));
                // btntodenchan, btntodenle,btntodennt, btnboden, btnxoatoden, btntonggtlist
                jpControl.add(Box.createVerticalStrut(30));
                jpControl.add(btntodenchan= new  JButton("To den so chan"));
                jpControl.add(Box.createVerticalStrut(10));
                jpControl.add(btntodenle= new  JButton("To den so le"));
                jpControl.add(Box.createVerticalStrut(10));
                jpControl.add(btntodennt= new  JButton("To den so nguyen to"));
                jpControl.add(Box.createVerticalStrut(10));
                jpControl.add(btnboden= new  JButton("Bo to den"));
                jpControl.add(Box.createVerticalStrut(10));
                jpControl.add(btnxoatoden= new  JButton("Xoa cac gia tri dang to den"));
                jpControl.add(Box.createVerticalStrut(10));
                jpControl.add(btntonggtlist= new  JButton("Tong gia tri trong jlist"));
                
                btntodenchan.setMaximumSize(btnxoatoden.getPreferredSize()); 
                btntodenle.setMaximumSize(btnxoatoden.getPreferredSize()); 
                btntodennt.setMaximumSize(btnxoatoden.getPreferredSize());
                btnboden.setMaximumSize(btnxoatoden.getPreferredSize());
                btntonggtlist.setMaximumSize(btnxoatoden.getPreferredSize());
                
                
                btntodenchan.addActionListener(this);
                btntodenle.addActionListener(this);
                btntodennt.addActionListener(this);
                btnboden.addActionListener(this);
                btntonggtlist.addActionListener(this);
                btnxoatoden.addActionListener(this);
                
                
                
                //jlist
                //jplist
                jplist.setLayout(new BorderLayout());
                JPanel jp1 = new JPanel();
                jp1.add(jtxt = new JTextField(4));
                jp1.add(btnnhap =  new JButton("Nhap"));
                jp1.add(jcheckam = new  JCheckBox("Nhap so amm"));
                btnnhap.addActionListener(this);
                jcheckam.addActionListener(this);
                
                jplist.add(jp1, BorderLayout.NORTH);
        
                
                
                
                
                
                
                modelList = new  DefaultListModel();
                modelList.addElement("1"); //sample to test
                modelList.addElement("3");
                modelList.addElement("4");
                modelList.addElement("7");
                modelList.addElement("9");
                modelList.addElement("11");
                modelList.addElement("10");
                mJlist = new JList(modelList);
                mJlist.addListSelectionListener(this);
                mJlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                jplist.add(new JScrollPane(mJlist));
                

                add(jpControl, BorderLayout.WEST);
                add(jplist, BorderLayout.CENTER);
                add(jptitle, BorderLayout.NORTH);
                add(jpbotton, BorderLayout.SOUTH);



                setVisible(true);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setSize(600, 500);
                setResizable(false);
                setLocationRelativeTo(null);
    }
    
    public static void main(String[] args) {
        new Bai17_Jlist_checkbox();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnthoat)
        {
            System.exit(0);
        }
        if(e.getSource()==btnnhap)
        {
            int temp = 0 ;
//            int check
            try {
                 temp = Integer.parseInt(jtxt.getText().toString());
                    
                if(jcheckam.isSelected()==true)
                 {
                        modelList.addElement(temp);
                        jtxt.setText(""); 
                 }
                else
                {
                    if(temp>=0)
                    {
                        modelList.addElement(temp);
                        jtxt.setText(""); 
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "khong duoc phep nhap so am", "Canh bao", JOptionPane.ERROR_MESSAGE);
                    }
                }
                
            
            } catch (Exception e2) {
                if(jtxt.getText().equalsIgnoreCase(""))
                {
                    JOptionPane.showMessageDialog(this, "Khong duoc de trong", "Canh Bao", JOptionPane.ERROR_MESSAGE);
                }
                else
                    {
                        JOptionPane.showMessageDialog(this, "Khong phai so", "Canh Bao", JOptionPane.ERROR_MESSAGE);
                    }
                jtxt.setText("");
            }

        
        }
        
        if(e.getSource()==btntonggtlist)
        {

            if(modelList.size()==0)
            {
                JOptionPane.showMessageDialog(this, "List Rong", "Warring", JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                int tong = 0;
                for(int i = 0  ; i < modelList.size() ; i++)
                {
                    int t =  Integer.parseInt(modelList.get(i).toString());
                    tong += t;
                }
                
                JOptionPane.showMessageDialog(this, "Tong Jlist = " + tong);
            }
            
            
        }
        
        if(e.getSource()==btntodenchan)
        {
            
             select =  getMangBoiden("chan");
              mJlist.setSelectedIndices(select);
        }
        
        if(e.getSource()==btntodenle)
        {
            
            select =  getMangBoiden("le");
              mJlist.setSelectedIndices(select);
        }
        

        if(e.getSource()==btntodennt)
        {
            
             select =  getMangBoiden("nguyento");
              mJlist.setSelectedIndices(select);
  
        } 
        if(e.getSource()==btnboden)
        {
            if(mJlist.getSelectedIndex()==-1)
            {
                JOptionPane.showMessageDialog(this, "Chua chon muc nao", "Canh bao", JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                mJlist.removeSelectionInterval(select[0], select[select.length-1]);    
            }
            
        } 
        
        if(e.getSource()==btnxoatoden)
        {
            for(int i = 0 ; i < select.length;i++)
            {
                modelList.removeElement(mJlist.getSelectedValue());    
            }
            
        } 
        
        
    }
    
    private int[] getMangBoiden(String string) {
        
        ArrayList<Integer> arrInt = new ArrayList<Integer>();;
        //neu chan
        if(string.equalsIgnoreCase("chan"))
        {
            for(int i = 0 ;  i  < modelList.size() ; i++)
            {
                int temp =  Integer.parseInt(modelList.get(i).toString());
                if(temp%2==0)
                {
                    arrInt.add(i);
                }
            }
             select  = new int[arrInt.size()];
            for(int i = 0 ; i < arrInt.size(); i++)
            {
                select[i]  =arrInt.get(i);
            }
        }
        
        //get ds le
        
        if(string.equalsIgnoreCase("le"))
        {
            for(int i = 0 ;  i  < modelList.size() ; i++)
            {
                int temp =  Integer.parseInt(modelList.get(i).toString());
                if(temp%2==1)
                {
                    arrInt.add(i);
                }
            }
             select  = new int[arrInt.size()];
            for(int i = 0 ; i < arrInt.size(); i++)
            {
                select[i]  =arrInt.get(i);
            }
        }
        
        
        ///nguyen to
        
        if(string.equalsIgnoreCase("nguyento"))
        {
            for(int i = 0 ;  i  < modelList.size() ; i++)
            {
                int temp =  Integer.parseInt(modelList.get(i).toString());
                if(kiemTraNguyenTo(temp)==true)
                {
                    arrInt.add(i);
                }
            }
             select  = new int[arrInt.size()];
            for(int i = 0 ; i < arrInt.size(); i++)
            {
                select[i]  =arrInt.get(i);
            }
        }
        
        
        
            
        return select;
    }

    private boolean kiemTraNguyenTo(int temp) {
        int dem=0;
        for(int i = 1 ; i <= temp ; i++)
        {
            if(temp%i==0)
            {
                dem++;
            }
        }
        if(dem==2)
            return true;
            
        
        return false;
    }

    public boolean NhapSoAm()
    {
        if(jcheckam.isSelected()==false)
        {
            return false;
        }
        return true;
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        
    }
}

No comments:

Powered by Blogger.