JButton can not be resolved to a type

0

This JFrame has a JTextField that once entered and clicking "Accept" shows a table with the entered data. The problem is that I get this error in getParent ():

  

The method getParent () is undefined for the type new ActionListener () {}

This is the code I have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.List;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;

import javax.swing.table.DefaultTableModel;  
public class Ventana extends JFrame {



    private JTextField txtidCliente;

    private JTextField txtnombreCliente;
    private JTextField txtnumeroCuenta;
    private JTextField txttelefono;
    private JButton btnAceptar;
    private JLabel lblidCliente, lblnombreCliente, lblnumeroCuenta, lbltelefono;

    private Container contenedor;
    private GridLayout cuadricula1;
    public Ventana () {
        super ("CREAR UNA CUENTA EN UN BANCO");
        cuadricula1 = new GridLayout (3, 3, 10, 10);

        lblidCliente = new JLabel("ID CLIENTE:");
        lblnombreCliente = new JLabel("NOMBRE DEL CLIENTE:");
        lblnumeroCuenta = new JLabel("NUMERO DE CUENTA:");
        lbltelefono = new JLabel("NÚMERO DE TELEFONO:");


        Object [] array = {"ID CLIENTE", "NOMBRE DEL CLIENTE", "NUMERO DE CUENTA", "TELEFONO"};
        JTable tabla = new JTable (new Object[][]{} , array);
        JScrollPane panel_tabla = new JScrollPane ();
        DefaultTableModel modelo_tabla = new DefaultTableModel (null, array);

        txtidCliente = new  JTextField(15); 
        txtnombreCliente = new  JTextField(45);
        txtnumeroCuenta = new  JTextField(15);
        txttelefono = new  JTextField(15);

        JButton btnAceptar = new JButton("ACEPTAR");

        contenedor = getContentPane();
        contenedor.setLayout(cuadricula1);
        contenedor.add(lblidCliente);
        contenedor.add(txtidCliente);
        contenedor.add(lblnombreCliente);
        contenedor.add(txtnombreCliente);
        contenedor.add(lblnumeroCuenta);
        contenedor.add(txtnumeroCuenta);
        contenedor.add(lbltelefono);
        contenedor.add(txttelefono);
        contenedor.add( btnAceptar);    

        btnAceptar.addActionListener(new ActionListener (){
          public void actionPerformed(ActionEvent e){
            JTable tblDatos = new JTable();
            DefaultTableModel modelo=(DefaultTableModel) tblDatos.getModel(); 

            //Sección 2
            Object [] fila=new Object[4]; 

            //Sección 3
            fila[0]=txtidCliente.getText(); 
            fila[1]=txtnombreCliente.getText(); 
            fila[2]=txtnumeroCuenta.getText(); 
            fila[3]=txttelefono.getText(); 

            //Sección 4
            modelo.addRow(fila); 

            //Sección 5
            tblDatos.setModel( modelo); 


            tabla.setModel(modelo_tabla);
            panel_tabla.setViewportView(tabla);
            getContentPane().add(panel_tabla);
            panel_tabla.setBounds(12, 350, 513, 250);

            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setSize(700,150);
            setVisible(true);
            setLocationRelativeTo(this.getParent());
          }
        });
      }
    
asked by Mady Reyes 21.11.2016 в 09:18
source

1 answer

0

If your problem is the last line of the code that you have put:

setLocationRelativeTo(this.getParent());

It is clear that it must give you an error because if you look you are within the definition of your anonymous ActionListener . To do what you want you must do one of these two options:

  • Remove that line of code before and before that definition and then pass it on for you to use inside
  • Take the element you want to act on (just like you did with the TableModel and modify it inside, but keep in mind that in that case you're going to be asked to have the final modifier or effectively final .
  • answered by 21.11.2016 / 09:37
    source