Feedback form using swing,awt,jdbc
Rohit143
java
3 years ago
8.5 kB
7
Indexable
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.sql.*; class MyFrame extends JFrame implements ActionListener { JLabel label2, label3; // Components of the Form JPanel firstTab; JLabel title; JLabel name; JTextField tname; JLabel mno; JTextField tmno; JLabel gender; JRadioButton male; JRadioButton female; JRadioButton other; JRadioButton prefer_not_to_say; ButtonGroup gengp; JLabel feedback; JTextArea tfeedback; JButton sub; JButton reset; JTextArea tout; JLabel res; JTextArea resadd; MyFrame() { setSize(400, 400); setDefaultCloseOperation(3); JTabbedPane tabbedPane = new JTabbedPane(); // constructor, to initialize the components // with default values. setTitle("Covid 19 vaccination Feedback form"); setBounds(300, 90, 900, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); firstTab = new JPanel(); firstTab.setLayout(null); title = new JLabel("Covid 19 vaccination Feedback form"); title.setFont(new Font("Arial", Font.PLAIN, 30)); title.setSize(400, 30); title.setLocation(250, 30); firstTab.add(title); name = new JLabel("Name"); name.setFont(new Font("Arial", Font.PLAIN, 20)); name.setSize(100, 20); name.setLocation(100, 100); firstTab.add(name); tname = new JTextField(); tname.setFont(new Font("Arial", Font.PLAIN, 15)); tname.setSize(190, 20); tname.setLocation(200, 100); firstTab.add(tname); mno = new JLabel("Mobile"); mno.setFont(new Font("Arial", Font.PLAIN, 20)); mno.setSize(100, 20); mno.setLocation(100, 150); firstTab.add(mno); tmno = new JTextField(); tmno.setFont(new Font("Arial", Font.PLAIN, 15)); tmno.setSize(150, 20); tmno.setLocation(200, 150); firstTab.add(tmno); gender = new JLabel("Gender"); gender.setFont(new Font("Arial", Font.PLAIN, 20)); gender.setSize(100, 20); gender.setLocation(100, 200); firstTab.add(gender); male = new JRadioButton("Male"); male.setFont(new Font("Arial", Font.PLAIN, 15)); male.setSelected(true); male.setSize(75, 20); male.setLocation(200, 200); firstTab.add(male); female = new JRadioButton("Female"); female.setFont(new Font("Arial", Font.PLAIN, 15)); female.setSelected(false); female.setSize(80, 20); female.setLocation(275, 200); firstTab.add(female); other = new JRadioButton("Other"); other.setFont(new Font("Arial", Font.PLAIN, 15)); other.setSelected(false); other.setSize(80, 20); other.setLocation(355, 200); firstTab.add(other); prefer_not_to_say = new JRadioButton("Prefer not to say"); prefer_not_to_say.setFont(new Font("Arial", Font.PLAIN, 15)); prefer_not_to_say.setSelected(false); prefer_not_to_say.setSize(200, 20); prefer_not_to_say.setLocation(435, 200); firstTab.add(prefer_not_to_say); gengp = new ButtonGroup(); gengp.add(male); gengp.add(female); gengp.add(other); gengp.add(prefer_not_to_say); feedback = new JLabel("Feedback"); feedback.setFont(new Font("Arial", Font.PLAIN, 20)); feedback.setSize(100, 20); feedback.setLocation(100, 250); firstTab.add(feedback); tfeedback = new JTextArea(); tfeedback.setFont(new Font("Arial", Font.PLAIN, 15)); tfeedback.setSize(500, 180); tfeedback.setLocation(200, 250); tfeedback.setLineWrap(true); firstTab.add(tfeedback); sub = new JButton("Submit"); sub.setFont(new Font("Arial", Font.PLAIN, 15)); sub.setSize(100, 20); sub.setLocation(150, 450); sub.addActionListener(this); firstTab.add(sub); reset = new JButton("Reset"); reset.setFont(new Font("Arial", Font.PLAIN, 15)); reset.setSize(100, 20); reset.setLocation(270, 450); reset.addActionListener(this); firstTab.add(reset); res = new JLabel(""); res.setFont(new Font("Arial", Font.PLAIN, 20)); res.setSize(500, 25); res.setLocation(100, 500); firstTab.add(res); // Second Tab Item final JPanel secondPanel = new JPanel(); final JLabel receivedFeedbacks=new JLabel("Feedback received"); secondPanel.add(receivedFeedbacks); final JScrollPane scrollPane = displayTable(); secondPanel.add(scrollPane); validate(); JButton refresh =new JButton("Refresh"); refresh.setSize(100, 20); refresh.setLocation(20,50); refresh.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { secondPanel.remove(scrollPane); secondPanel.add(displayTable()); receivedFeedbacks.setText("Feedback received and Refreshed."); validate(); } }); secondPanel.add(refresh); secondPanel.add(scrollPane); tabbedPane.add("Share Feedback", firstTab); tabbedPane.add("Received Feedbacks", secondPanel); add(tabbedPane); setVisible(true); } public JScrollPane displayTable() { JTable table ; DefaultTableModel defaultTableModel = new DefaultTableModel(); table = new JTable(defaultTableModel); defaultTableModel.addColumn("Name"); defaultTableModel.addColumn("Feedback"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection("jdbc:odbc:feedbacks"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from feedback"); while (resultSet.next()) { String name = resultSet.getString(1); String feedback = resultSet.getString(3); defaultTableModel.addRow(new Object[]{name,feedback}); } } catch (Exception e) { System.out.println(e); } JScrollPane scrollPane = new JScrollPane(table); return scrollPane; } public void reset() { String def = ""; tname.setText(def); tfeedback.setText(def); tmno.setText(def); res.setText(def); tout.setText(def); resadd.setText(def); } public void actionPerformed(ActionEvent e) { String nameData = tname.getText(); String numberData = tmno.getText(); String feedbackData = tfeedback.getText(); if (e.getSource() == sub) { if (nameData.isEmpty() || (numberData.isEmpty()) || (feedbackData.isEmpty())) { JOptionPane.showMessageDialog(null, "Data missing"); } else { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection("jdbc:odbc:feedbacks"); Statement statement = connection.createStatement(); statement.executeUpdate("INSERT INTO feedback VALUES('" + nameData + "','" + numberData + "','" + feedbackData + "')"); JOptionPane.showMessageDialog(null, "Thanks for your feedback"); connection.close(); reset(); validate(); } catch (Exception exception) { System.out.println(exception); } } } else if (e.getSource() == reset) { reset(); } } } public class FeedbackForm { public static void main(String[] args) { new MyFrame(); } }
Editor is loading...