Package cl.rivendel.views

Source Code of cl.rivendel.views.GestionadorLiga

package cl.rivendel.views;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import cl.rivendel.modelo.Jugador;

public class GestionadorLiga extends JFrame {

  /**
   *
   */
  private static final long serialVersionUID = 1L;
  private JPanel contentPane;
  private JMenu mnFile;
  private JMenuItem mntmSalir;
  private JTextField txfJugador;
  private JButton btnNuevo;
  private JTable table;
  private LinkedList<Jugador> listado;
  private static Vector<String> columnNames;
  private static DefaultTableModel model;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          GestionadorLiga frame = new GestionadorLiga();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public GestionadorLiga() {
    setTitle("Gestionador de Rondas ");
    setIconImage(Toolkit
        .getDefaultToolkit()
        .getImage(
            GestionadorLiga.class
                .getResource("/resources/images/104822-Magic_The_Gathering_Mana.png")));

    initComponents();
    initEvents();
    initTableProperties();
    loadTable();

  }

  private void initTableProperties() {
    table.getTableHeader().setReorderingAllowed(false); // we do not allow
                              // to move columns
    table.setRowHeight(25);

    // column names
    Vector<String> nombresCols = new Vector<String>();

    nombresCols.add("Nombre");
    nombresCols.add("Puntos");
    nombresCols.add("Logros");

    columnNames = nombresCols;

  }

  private void initEvents() {

    // ========================================
    // click on table rows
    // ========================================
    table.addMouseListener(new MouseAdapter() {

      public void mouseClicked(MouseEvent me) {
        int row = table.rowAtPoint(me.getPoint());
        int col = table.columnAtPoint(me.getPoint());

        if (row > -1 && col < 2) { // control of bounds
          System.out.println("se ha presionado la fila: " + row
              + "mas la columna " + col);
        }
      }
    });

    // ========================================
    // click on buttons agregar
    // ========================================

    btnNuevo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        if (!txfJugador.getText().isEmpty()) {

          Jugador jugador = new Jugador();

          jugador.setNombre(txfJugador.getText());

          addJugador(jugador);

        }
      }

    });

    // ========================================
    // click on menu item Salir
    // ========================================
    mntmSalir.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0); // close all application
      }
    });

  }

  private void initComponents() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 600);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    mnFile = new JMenu("Archivo");
    menuBar.add(mnFile);

    mntmSalir = new JMenuItem("Salir");

    mnFile.add(mntmSalir);
    contentPane = new JPanel();
    setContentPane(contentPane);

    JPanel panel = new JPanel();

    JScrollPane scrollPane = new JScrollPane();
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane
        .setHorizontalGroup(gl_contentPane
            .createParallelGroup(Alignment.LEADING)
            .addGroup(
                gl_contentPane
                    .createSequentialGroup()
                    .addGroup(
                        gl_contentPane
                            .createParallelGroup(
                                Alignment.LEADING)
                            .addGroup(
                                gl_contentPane
                                    .createSequentialGroup()
                                    .addGap(10)
                                    .addComponent(
                                        scrollPane,
                                        GroupLayout.DEFAULT_SIZE,
                                        764,
                                        Short.MAX_VALUE))
                            .addGroup(
                                gl_contentPane
                                    .createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(
                                        panel,
                                        GroupLayout.DEFAULT_SIZE,
                                        764,
                                        Short.MAX_VALUE)))
                    .addContainerGap()));
    gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(
        Alignment.LEADING).addGroup(
        gl_contentPane
            .createSequentialGroup()
            .addComponent(panel, GroupLayout.PREFERRED_SIZE, 69,
                GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(ComponentPlacement.UNRELATED)
            .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
                450, Short.MAX_VALUE).addContainerGap()));

    table = new JTable();
    scrollPane.setViewportView(table);

    JLabel lblJugador = new JLabel("Jugador");

    txfJugador = new JTextField();
    txfJugador.setColumns(10);

    btnNuevo = new JButton("Agregar");

    GroupLayout gl_panel = new GroupLayout(panel);
    gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
        Alignment.LEADING).addGroup(
        gl_panel.createSequentialGroup()
            .addContainerGap()
            .addComponent(lblJugador)
            .addPreferredGap(ComponentPlacement.UNRELATED)
            .addComponent(txfJugador, GroupLayout.PREFERRED_SIZE,
                360, GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(ComponentPlacement.UNRELATED)
            .addComponent(btnNuevo)
            .addContainerGap(264, Short.MAX_VALUE)));
    gl_panel.setVerticalGroup(gl_panel
        .createParallelGroup(Alignment.LEADING)
        .addGroup(
            gl_panel.createSequentialGroup()
                .addGap(24)
                .addGroup(
                    gl_panel.createParallelGroup(
                        Alignment.BASELINE)
                        .addComponent(lblJugador)
                        .addComponent(
                            txfJugador,
                            GroupLayout.PREFERRED_SIZE,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnNuevo))
                .addContainerGap(29, Short.MAX_VALUE)));
    panel.setLayout(gl_panel);
    contentPane.setLayout(gl_contentPane);

    // se inicializa la lista de jugadores
    listado = new LinkedList<Jugador>();

  }

  private void loadTable() {

    Vector<Object> jugadorData;

    model = new DefaultTableModel(null, columnNames); // first we add the
                              // column names

    table.setModel(model); // update the model
    table.setRowSorter(null);

    // we add each row of the table
    for (Jugador c : listado) {
      jugadorData = new Vector<Object>(); // we load a vector with the
                        // data of each row

      jugadorData.add(c.getNombre()); // data for the column "Nombre"
      jugadorData.add(c.getPuntos()); // data for the column "Punto"
      jugadorData.add(c.getLogros()); // data for the column "Logro"

      model.addRow(jugadorData); // we add all data to the table model
    }
    table.setModel(model); // update the model

    // Now, we have the model loaded, so we can modify its column properties
    table.getColumnModel().getColumn(0).setResizable(false);
    table.getColumnModel().getColumn(0).setPreferredWidth(200);

    table.getColumnModel().getColumn(1).setResizable(false);
    table.getColumnModel().getColumn(1).setPreferredWidth(200);

    table.getColumnModel().getColumn(2).setResizable(false);
    table.getColumnModel().getColumn(2).setPreferredWidth(200);

  }

  public void addJugador(Jugador p_jugador) {

    if (model == null) {
      model = new DefaultTableModel(null, columnNames); // first we add
                                // the

    }

    Vector<Object> jugadorData;
    jugadorData = new Vector<Object>();

    jugadorData.add(p_jugador.getNombre()); // data for the column "Nombre"
    jugadorData.add(p_jugador.getPuntos()); // data for the column "Punto"
    jugadorData.add(p_jugador.getLogros()); // data for the column "Logro"

    model.addRow(jugadorData); // we add all data to the table model
    table.setModel(model); // update the model

  }
}
TOP

Related Classes of cl.rivendel.views.GestionadorLiga

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.