[Java] Jlist - bai 16

Public on October 9, 2014
CODE :




package thanhcs.bai16;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

public class BaiTap_Jlist extends JFrame implements ActionListener {

    DefaultListModel modelList;
    JList mJlist;
    JButton btnadd, btnremove;
    public BaiTap_Jlist() {
        // TODO Auto-generated constructor stub
        setLayout(new BorderLayout());

        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"));

        jpControl.add(btnadd= new  JButton("Add"));
        jpControl.add(btnremove= new  JButton("Remove"));

        modelList = new  DefaultListModel();
        mJlist = new JList(modelList);
        jplist.add(new JScrollPane(mJlist));


        add(jpControl, BorderLayout.NORTH);
        add(jplist, BorderLayout.CENTER);



        btnremove.addActionListener(this);
        btnadd.addActionListener(this);

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
    }

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

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnadd)
        {
            String temp =  JOptionPane.showInputDialog(this, "Enter name", "Enter name", JOptionPane.QUESTION_MESSAGE);
            if(temp.toString().trim().equalsIgnoreCase(""))
            {

                JOptionPane.showMessageDialog(this, "empty String", "Warring", JOptionPane.ERROR_MESSAGE);

            }
            else
            {
                modelList.addElement(temp);
            }
        }
        if(e.getSource()==btnremove)
        {

            if(mJlist.getSelectedIndex()==-1)
            {
                JOptionPane.showMessageDialog(this, "You not selected", "Warring", JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                int check = JOptionPane.showConfirmDialog(this, "Are you sure to delele - " +mJlist.getSelectedValue(), "DELETE", JOptionPane.YES_NO_OPTION);
                if(check ==JOptionPane.YES_OPTION)
                {
                    modelList.removeElement(mJlist.getSelectedValue());
                }
                else
                {

                }
            }

        }

    }

}




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon