Created
November 17, 2021 07:52
-
-
Save tinwritescode/94bfb6666baf564e631ef1ded5d08f00 to your computer and use it in GitHub Desktop.
Giữa kì
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.*; | |
| import java.awt.*; | |
| import java.sql.Connection; | |
| import java.sql.DriverManager; | |
| import java.sql.ResultSet; | |
| import java.sql.Statement; | |
| import java.util.ArrayList; | |
| /** | |
| * untitled | |
| * Created by IntelliJ IDEA. | |
| * User: Trung Tin Nguyen | |
| * Date: 16/11/2021 | |
| * Time: 8:16 pm | |
| */ | |
| class Ward { | |
| public int id; | |
| public String name; | |
| public String getName() { | |
| return name; | |
| } | |
| } | |
| class District { | |
| public int id; | |
| private String name; | |
| public Ward[] wards; | |
| public District(int id, String name) { | |
| this.id = id; | |
| this.name = name; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| } | |
| public class GiuaKi extends JFrame { | |
| private static JPanel rootPanel = new JPanel(); | |
| private ArrayList<District> districts = new ArrayList<>(); | |
| GiuaKi(JPanel rootPanel) { | |
| getData(); | |
| setTitle("District-Ward Chooser"); | |
| setSize(800, 600); | |
| // set border layout | |
| rootPanel.setLayout(new BorderLayout()); | |
| // create JPanel with flowlayout | |
| JPanel panel1 = new JPanel(); | |
| panel1.setLayout(new FlowLayout()); | |
| // create label and combo box | |
| JLabel label = new JLabel("Your district"); | |
| JComboBox<String> comboBox = new JComboBox<>(); | |
| // add items to combo box | |
| for (District district : districts) { | |
| comboBox.addItem(district.getName()); | |
| } | |
| // add label and combo box to panel1 | |
| panel1.add(label); | |
| panel1.add(comboBox); | |
| //add panel1 to top | |
| rootPanel.add(panel1, BorderLayout.NORTH); | |
| // create list select and add to panel2 | |
| JPanel panel2 = new JPanel(); | |
| panel2.setLayout(new FlowLayout()); | |
| JList<String> list = new JList<>(); | |
| JScrollPane scrollPane = new JScrollPane(); | |
| scrollPane.setViewportView(list); | |
| list.setLayoutOrientation(JList.VERTICAL); | |
| // set list to sample | |
| String[] arr = {}; | |
| // add wards to arr | |
| for (District district : districts) { | |
| if (district.getName().equals(comboBox.getSelectedItem())) { | |
| for (Ward ward : district.wards) { | |
| arr[arr.length] = ward.getName(); | |
| } | |
| } | |
| } | |
| // add arr to list | |
| list.setListData(arr); | |
| panel2.add(list); | |
| rootPanel.add(panel2, BorderLayout.CENTER); | |
| // create button and add to panel3 | |
| JPanel panel3 = new JPanel(); | |
| panel3.setLayout(new FlowLayout()); | |
| JButton button = new JButton("Ok"); | |
| JButton button2 = new JButton("Cancel"); | |
| // add handle onclick for button to print out selected item and comboBox | |
| button.addActionListener(e -> { | |
| String selectedItem = (String) list.getSelectedValue(); | |
| String comboBoxValue = (String) comboBox.getSelectedItem(); | |
| // alert | |
| JOptionPane.showMessageDialog(null, selectedItem + "--" + comboBoxValue); | |
| }); | |
| panel3.add(button); | |
| panel3.add(button2); | |
| rootPanel.add(panel3, BorderLayout.SOUTH); | |
| } | |
| public void getData() { | |
| Connection con = null; | |
| try { | |
| Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | |
| con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=hcmchooser;user=sa;password=password"); | |
| if (con != null) { | |
| System.out.println("Connect successfully"); | |
| String s = "select * from Districts"; | |
| Statement st = con.createStatement(); | |
| // load Districts into array | |
| ResultSet rs = st.executeQuery(s); | |
| while (rs.next()) { | |
| District district = new District(rs.getInt("DistrictID"), rs.getString("DistrictName")); | |
| // get ward from table Wards | |
| String s1 = "select * from Wards where DistrictID = " + district.id; | |
| Statement st1 = con.createStatement(); | |
| ResultSet rs1 = st1.executeQuery(s1); | |
| int j = 0; | |
| while (rs1.next()) { | |
| Ward ward = new Ward(); | |
| ward.id = rs1.getInt("WardID"); | |
| ward.name = rs1.getString("WardName"); | |
| district.wards[j] = ward; | |
| j++; | |
| } | |
| districts.add(district); | |
| } | |
| } | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| GiuaKi frame = new GiuaKi(rootPanel); | |
| frame.setContentPane(frame.rootPanel); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.pack(); | |
| frame.setVisible(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment