lockers.FrameCobro.java Source code

Java tutorial

Introduction

Here is the source code for lockers.FrameCobro.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lockers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import javax.swing.*;

/**
 *
 * @author luishoracio
 */
public class FrameCobro extends javax.swing.JFrame {

    public FrameCobro() {
        initComponents();

        getAvailableLockers();
        getRates();
    }

    private void getAvailableLockers() {
        JSONArray array = Utils.getJSONArrayFromURL("http://127.0.0.1:8000/Lockers_Search/?status=0&format=json");

        System.out.println(array.size());
        if (array.size() == 0) {
            this.jLblAvailableLockers.setText("No hay disponibilidad");
        } else {
            this.jLblAvailableLockers.setText("La cantidad de lockers disponibles es :" + array.size());
            for (Object array1 : array) {
                JButton nuevo = new JButton();
                JSONObject obj2 = (JSONObject) array1;
                nuevo.setText(obj2.get("locker_name").toString());
                nuevo.setVisible(true);
                this.jPanel2.add(nuevo);
            }
        }
    }

    private void getRates() {
        JSONArray array = Utils.getJSONArrayFromURL("http://127.0.0.1:8000/Rates/?format=json");

        if (array.size() == 0) {
            this.jLblAvailableLockers.setText("No hay disponibilidad");
        } else {
            for (Object array1 : array) {
                JButton nuevo = new JButton();
                JSONObject obj2 = (JSONObject) array1;
                System.out.println(obj2.size());
                nuevo.setText(obj2.get("rate_name") + ": " + obj2.get("rate_rate"));
                nuevo.setVisible(true);
                nuevo.setSize(200, 50);
                nuevo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Object source = e.getSource();

                        if (source instanceof JButton) {
                            JButton btn = (JButton) source;
                            System.out.println("You clicked the button " + btn.getText());
                        }
                    }
                });
                this.jPanel1.add(nuevo);
            }

        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLblAvailableLockers = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Selecciona tu locker");
        setPreferredSize(new java.awt.Dimension(640, 480));

        jLabel1.setText("Disponibilidad de Lockers");

        jLblAvailableLockers.setText("Sin disponibilidad");

        jLabel2.setText("Tarifas Vigentes");

        jPanel1.setLayout(new java.awt.GridLayout(0, 1));

        jPanel2.setLayout(new java.awt.GridLayout(4, 3));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLblAvailableLockers)
                                        .addGroup(layout.createSequentialGroup().addGap(31, 31, 31)
                                                .addComponent(jLabel2))
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 195,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(6, 6, 6)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(14, 14, 14).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLblAvailableLockers).addGap(10, 10, 10).addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)))
                        .addContainerGap()));

        jLblAvailableLockers.getAccessibleContext().setAccessibleName("jLblAvailableLockers");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameCobro.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameCobro().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLblAvailableLockers;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}