Return information from the jtable row after selection

4

I have an application with 2 JFrames ..

1st JFrame: A screen with a search button and JTextField

2nd JFrame: A JTable with DB information.

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

I wanted to make clicking on a line of JTable show the information in 1st Frame and close JTable as a result of the click.

    
asked by anonymous 10.07.2016 / 19:48

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 do not forget to convert the index from the table to the model, to avoid problems with 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 do not have a method that returns an object of your model, you can use the getValueAt(indexRow, indexColumn) method to get the values of each column of the line and popularize its object. The method would look more 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 your app's secondary screens, and use JFrame only on the main, so you can control visibility and easily retrieve information between those windows, even after you have given dispose to windows. In this link you get information about using JDialogs , its use is very similar to JFrames , with the addition of making it a #

Modal window .

In this question has an example similar to yours.

Other references:

Update a Jtable that is in a JFrame from a JDialog

Calling up a JFrame from another JFrame with different classes

    
10.07.2016 / 20:53
0

If you are using AbstractTableModel based on the class ... you should first call the second screen, create a method on the second screen that you receive from where it came from. type, has JFrame1 that opens JFrame2, when you call JFrame2, you must also call a method that you must 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 you call JFrame2, you 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, because he already knows what his father is. Now go to the JTable 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 method users screen 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, so you have to create a public method on the screen to be called in another. I hope I have helped ...

    
10.07.2016 / 20:19