Return jtable line information after selection

Asked

Viewed 661 times

4

I have an application with 2 Jframes..

1st Jframe: A screen with a search button and Jtextfield

2nd Jframe: A Jtable with BD information.

When I click the search button in the 1st Frame opens the JTable with the BD information all right.

I wanted to do that by clicking on a line of JTable , showed the information in the 1st Frame and closed Jtable as a result of the click.

  • Say the code of what you’ve done so far.

  • You have created an abstractTableModel or is using Default?

2 answers

1

Another alternative is to use an event that monitors selections made in the table:

suaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

    @Override
    public void valueChanged(ListSelectionEvent e) {

        ListSelectionModel lsm = (ListSelectionModel) e.getSource();

        if(!lsm.isSelectionEmpty()){

        //aqui você insere a ação que quer fazer quando
        //uma linha for selecionada ou uma seleção for
        //alterada na tabela
        }
    }
}

And don’t forget to convert the table’s Dice to the model’s, to avoid indexofboundException if you insert filters into the table.

int rowSel = suaTable.getSelectedRow();//pega o indice da linha na tabela
int indexRowModel = suaTable.getRowSorter().convertRowIndexToModel(rowSel);//converte pro indice do model

If you don’t have a method that returns an object from your model, you can use the method getValueAt(indexRow, indexColumn) to take the values of each column of the row and populate its object. The method would look a little less like this:

suaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent e) {

        ListSelectionModel lsm = (ListSelectionModel) e.getSource();

        if(!lsm.isSelectionEmpty()){

          int rowSel = suaTable.getSelectedRow();//pega o indice da linha na tabela
          int indexRowModel = suaTable.getRowSorter().convertRowIndexToModel(rowSel);//converte pro indice do model
          //aqui você adapta conforme sua tabela e seu objeto,
          //alterando o indice da coluna
          seuTable.getModel().getValueAt(indexRowModel, indiceDaColuna);
        }               
    }
});

Another tip would be to use JDialog for secondary screens of your application, and use JFrame only in the main, this way you can control the visibility and easily rescue information between these windows, even after you have given dispose in the daughter windows. At this link you get information about the use of JDialogs, its use is quite similar to the JFrames, with the addition of making it a modal window.

In this question has an example similar to his.


Other references:

Update a Jtable that is in a Jframe from a Jdialog

How to call a Jframe from another Jframe with different classes

0

If you are using the Abstracttablemodel based on the class... First, when calling the second screen, create a method on the second screen that you receive from where it came from. type, have Jframe1 that opens Jframe2, when you call Jframe2, Voce should also call a method that Voce should create in Jframe two... Example. In Jframe 2 you must have

 JFrame1 JFrame1;
/* Construtor*/
public JFrame2() {
 }
public void setTelaPai(JFrame1 jframe1) {
this.JFrame1 = jframe1 ;
}

With this when Voce is calling Jframe2, Voce should call the method

JFrame2 jframe2 = new JFrame2();
jframe2.setVisible(true);
jframe2.setTelaPai(this);

Now in the second frame, he already knows where to go back, because he already knows what his father is. Now go to Jtable’s click event... And do so

        private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
  if (evt.getClickCount() == 2) {
     int linSelect = jTable1.getSelectedRow();
  if(linSelect!=-1){

        SeUsuarios usuarios = (SeUsuarios) abstractTableUsuario.getUsuario(linSelect);

        JFrame1.usuariosTela(usuarios);
        setVisible(false); 
     } else{
     JOptionPane.showMessageDialog(null, "Selecione uma linha");
     }  
}
}

This user methodThe is a method that receives the user object, and fills the screen, it is used because a second screen can not access the fields of the other, Therefore you have to create a public method on the screen itself to be called on another. I hope I’ve helped...

Browser other questions tagged

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