I can’t show custom Jtable inside the frame

Asked

Viewed 31 times

0

create a custom table model but do not click on my frame. Someone could help me?

public class PessoaJTable extends JTable{

/**
 * 
 */

private static final long serialVersionUID = 3182466937648479844L;

private PessoaTableModel tableModel;

public PessoaJTable() {
    this.tableModel = new PessoaTableModel();
    setModel(this.tableModel);
}

public void setBounds(int x, int y, int width, int height) {
    // TODO Auto-generated method stub

  }

}

Now here’s my main frame.

package br.com.treinaweb.javabasico.forms;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.treinaweb.javabasico.components.PessoaJTable;

public class FrmPrincipal extends JFrame{

public FrmPrincipal() {
    this.setSize(600,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(600,200);
    this.setTitle("Cadastro de pessoas");

    JPanel panel = new JPanel();
    panel.setLayout(null);

    //labelId

    JLabel labelId = new JLabel("id: ");
    labelId.setBounds(10,10,50,30);
    panel.add(labelId);

    //textId

    JTextField textId = new JTextField();
    textId.setBounds(80,10,500,30);
    textId.setEnabled(false);
    panel.add(textId);

    // labelNome 

    JLabel labelNome = new JLabel("Nome: ");
    labelNome.setBounds(10,60,50,30);
    panel.add(labelNome);

    // textNome

    JTextField textNome = new JTextField();
    textNome.setBounds(80,60,500,30);
    textNome.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            char tecla = e.getKeyChar();
            if(Character.isDigit(tecla)) {
                e.consume();
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub

        }
    });
    panel.add(textNome);

    // labelIdade

    JLabel labelIdade = new JLabel("Idade:");
    labelIdade.setBounds(10,110,50,30);
    panel.add(labelIdade);

    // textIdade

    JTextField textIdade = new JTextField();
    textIdade.setBounds(80,110,500,30);
    textIdade.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            char tecla = e.getKeyChar();
            if(Character.isAlphabetic(tecla)) {
                e.consume();
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub

        }
    });
    panel.add(textIdade);

    // btnAdd

    JButton btnAdd = new JButton("Add");
    btnAdd.setBounds(40, 150, 100, 30);
    panel.add(btnAdd);

    // btnUpdate

    JButton btnUpdate = new JButton("Update");
    btnUpdate.setBounds(240, 150, 100, 30);
    panel.add(btnUpdate);

    // btnDelete

    JButton btnDelete = new JButton("Delete");
    btnDelete.setBounds(440, 150, 100, 30);
    panel.add(btnDelete);

    // tablePessoas

    PessoaJTable tablePessoas = new PessoaJTable();
    tablePessoas.setBounds(300,10,500,300);
    panel.add(tablePessoas);
    this.add(panel);
    this.setVisible(true);
}

}
  • At a glance here and then here. I think it might help you. And maybe your question is duplicated.

  • See the answers to the questions I marked above yours (including the two suggested by @gato), the correct solution of how popular a table with own data is in them.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.