1 / 7

Event Handling on List

Event Handling on List . private class ListListener implements ListSelectionListener { public void valueChanged ( ListSelectionEvent e) { msg = (String) scrollingList.getSelectedValue (); } }. String bookList [] = {" Doraemon ",

margo
Download Presentation

Event Handling on List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Handling on List private class ListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { msg = (String) scrollingList.getSelectedValue(); } } String bookList[] = {" Doraemon ", " A-Ra-Re ", " DragonBall ", " BirdLand ", " Pung-Pong "}; JListscrollingList = new JList(bookList); scrollingList.setVisibleRowCount(4); scrollingList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); p.add(new JScrollPane(scrollingList)); scrollingList.addListSelectionListener (new ListListener()); 1

  2. private class RadioButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == n1) { day = "1 วัน"; baht = 10.0f; } if (source == n2) { day = "3 วัน"; baht = 30.0f; } } Event Handling on List ButtonGroupbg = new ButtonGroup(); JRadioButton n1 = new JRadioButton("1 day"); bg.add(n1); JRadioButton n2 = new JRadioButton("3 day"); bg.add(n2); p.add(n1); p.add(n2); n1.addActionListener(new RadioButtonListener()); n2.addActionListener(new RadioButtonListener()); 2

  3. private class ComboListener implements ActionListener { public void actionPerformed(ActionEvent e) { int idx = mcombo.getSelectedIndex(); switch (idx) { case 0: d=0.02f; break; case 1: d=0.05f; break; } } } • Event Handling on Combobox String discount[] = {" discount 2% ", " discount 5% "}; JComboBoxmcombo = new JComboBox(discount); p.add(mcombo); mcombo.addActionListener (new ComboListener()); 3

  4. Event Handling on Button JButtoncalcbtn = new JButton("calculation"); JButtonresetbtn = new JButton("reset"); Jbuttonclosebtn = new JButton("close"); p.add(calcbtn); p.add(resetbtn); p.add(closebtn); calcbtn.addActionListener(new ButtonListener()); resetbtn.addActionListener(new ButtonListener()); closebtn.addActionListener(new ButtonListener()); 4

  5. Event Handling on Button private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == calcbtn) { baht = baht - (d*baht); if (nametxt.getText().equals("")) { JOptionPane.showMessageDialog(null,"ป้อนข้อมูลชื่อด้วยค่ะ !!!"); } else if (teltxt.getText().equals("")) { JOptionPane.showMessageDialog(null,"ป้อนข้อมูลเบอร์โทรด้วยค่ะ !!!"); } else if (scrollingList.isSelectionEmpty()) { JOptionPane.showMessageDialog(null, "เลือกรายการที่ต้องการเช่าด้วยค่ะ !!!"); } else if (!n1.isSelected() && (!n2.isSelected())) { JOptionPane.showMessageDialog(null,"เลือกจำนวนวันด้วยค่ะ !!!"); } else { String m = "คุณ " + nametxt.getText() +" เช่า" + msg + day + " = " + new Float(baht).toString()+ " บาท" ; JOptionPane.showMessageDialog(null,m); } } if(source==resetbtn) { nametxt.setText(""); teltxt.setText(""); /* n1.setSelected(false); n2.setSelected(false); */ n0.setSelected(true); scrollingList.clearSelection(); } if (source==closebtn) { dispose(); } } } 5

  6. ผลการทำงานของโปรแกรม (กรณีเลือก DragonBall, 1 day, discount 5%)

  7. ผลการทำงานของโปรแกรม (กรณีเลือก BirdLand, 3 day, discount 2%)

More Related