A
A
Andrey Lubchuk2016-01-13 14:20:02
Java
Andrey Lubchuk, 2016-01-13 14:20:02

How to add an application launch handler in Java Swing?

Hello everyone) I have a program that has a JFrame, elements in it and a button click handler. How can I make it so that when running in JCombobox, pass an array of String[] items from windowOpen? I apologize in advance if this is shitty code.

package com.angelx.builder;

import GuiForms.Forms;
import com.mysql.fabric.jdbc.FabricMySQLDriver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

public class User extends JFrame {
    public static class TestWindowListener implements WindowListener {
        public void windowActivated(WindowEvent e) {
            user.l1.setText("1111111");
            System.out.println("windowActivated   ");
        }

        public void windowClosed(WindowEvent e) {
            System.out.println("windowClosed  ");
        }

        public void windowClosing(WindowEvent e) {
            System.out.println("windowClosing  ");

        }

        public void windowDeactivated(WindowEvent e) {
            System.out.println("windowDeactivated  ");
        }

        public void windowDeiconified(WindowEvent e) {
            System.out.println("windowDeiconified    ");
        }

        public void windowIconified(WindowEvent e) {
            System.out.println("windowIconified      ");
        }

        public void windowOpened(WindowEvent e) {

            System.out.println("windowOpened");
            String[] items = {"1"};
            user.l1.setText("111");
         }
    }
    WindowListener winListener = new TestWindowListener();
    static User user = new User("");

    public JButton menu, order, back;
    public TextArea jt;
    public JTextArea build;
    public JTextField t1;
    public JLabel l1;
    public static JComboBox cb;
    public eHandler handler = new eHandler();

    Connection connection = null;
    ArrayList<String> arrayString = new ArrayList<String>();
    ArrayList<String> arrayString1 = new ArrayList<String>();
    String query = "SELECT id, name, price FROM menu";
    static Forms fr = new Forms();

    public User(String s) {
        super(s);
        setLayout(null);
        menu = new JButton("Menu");
        order = new JButton("Order");
        back = new JButton("Back");
        jt = new TextArea(6, 18);
        t1 = new JTextField(5);
        l1 = new JLabel("Enter cocktails number: ");
        build = new JTextArea();
        add(menu);
        add(order);
        add(back);
        add(jt);
        add(l1);
        add(t1);
        add(build);

        menu.addActionListener(handler);
        order.addActionListener(handler);
        back.addActionListener(handler);
    }

    public static void main(String[] args0) {
        String[] items = {
                "1"
        };
        cb = new JComboBox(items);
        user.add(cb);
        user.l1.setText("11111");
    }

    public void ComboBX() {
        try {
            String query = "SELECT id, name, price FROM menu";
            Connection connection = null;
            connection = DriverManager.getConnection(ConnectToDatabase.URL, ConnectToDatabase.USERNAME, ConnectToDatabase.PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            Driver driver = new FabricMySQLDriver();
            DriverManager.registerDriver(driver);

            String id = resultSet.getString("id");
            String nameb = resultSet.getString("name");
            Double price = resultSet.getDouble("price");

            String a = "1";
            System.out.println(a);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public class eHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == menu) {
                try {
                    connection = DriverManager.getConnection(ConnectToDatabase.URL, ConnectToDatabase.USERNAME, ConnectToDatabase.PASSWORD);
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery(query);
                    Driver driver = new FabricMySQLDriver();
                    DriverManager.registerDriver(driver);
                    String nc = t1.getText();
                    String nameb = "";

                    while (resultSet.next()) {
                        String id = resultSet.getString("id");
                        nameb = resultSet.getString("name");
                        Double price = resultSet.getDouble("price");
                        arrayString.add(resultSet.getString("name"));
                        arrayString1.add(resultSet.getString("price"));
                        System.out.print(t1.getText());
                    }

                    Iterator iterator = arrayString.iterator();
                    Iterator iterator1 = arrayString1.iterator();

                    while (iterator.hasNext() && iterator1.hasNext()) {
                        for (int i = 1; i < 10; i++) {
                            jt.append(i + ". " + iterator.next().toString() + "     " + iterator1.next().toString() + '\n');
                        }
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

            if (e.getSource() == order) {
                try {
                    connection = DriverManager.getConnection(ConnectToDatabase.URL, ConnectToDatabase.USERNAME, ConnectToDatabase.PASSWORD);
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery(query);
                    Driver driver = new FabricMySQLDriver();
                    DriverManager.registerDriver(driver);
                    String nc = t1.getText();

                    while (resultSet.next()) {
                        String id = resultSet.getString("id");
                        String nameb = resultSet.getString("name");
                        Double price = resultSet.getDouble("price");
                        arrayString.add(resultSet.getString("name"));
                        System.out.print(t1.getText());
                        if (id.equals(nc)) {
                            Coctails coctails = new CoctailsBuilder()
                                    .buildName(nameb)
                                    .buildIngridiens(Ingridients.Martiny)
                                    .buildPrice(price)
                                    .build();

                            String builder = coctails.toString();
                            build.append(builder);
                        }
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

            if (e.getSource() == back) {
                setVisible(false);
                fr.Authorization();
            }
        }
    }


}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question