Update a Jtable that is in a JFrame from a JDialog

3

What I'm doing is a little longer, so I'll shorten the problem with a more objective example:

I have a registration form and created a modal Jdialog with the fields that I want to search. When I search in JDialog I would like to filter the information in the table that is in the JFrame. The problem is that it is not working.

VISAO (Search Screen):

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    FrmCadUsuarios frm = new FrmCadUsuarios();
    frm.tbVisivel(true);
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    FrmCadUsuarios frm = new FrmCadUsuarios();
    frm.tbVisivel(false);
}                                        

VISAO (registration form):

public class FrmCadUsuarios extends javax.swing.JFrame {

    private JTable tbUsuarios;
    DefaultTableModel modelo = new DefaultTableModel();

    public FrmCadUsuarios() {
        initComponents();
        this.setLocation(550,250);
        criaTabela();
        jScrollPane1.setViewportView(tbUsuarios);

    }

    public void tbVisivel (boolean aa) {
        tbUsuarios.setVisible(aa);
    }

}

I call JDialog like this:

private void tb_btn_pesquisarActionPerformed(java.awt.event.ActionEvent evt) {
  frmPesquisa pesq = new frmPesquisa(this,false);
  pesq.setVisible(true);
}

And I try to update a JLabel on the screen of main so it has 2 buttons, 1 should make the label visible and the other invisible:

private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
  FrmCadUsuarios frm = new FrmCadUsuarios();
  frm.tbVisivel(this, true);
}

private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {
  FrmCadUsuarios frm = new FrmCadUsuarios();
  frm.tbVisivel(this, false); 
}

The form search builder ( JDialog ) looks like this:

public frmPesquisa(java.awt.Frame parent, boolean modal) {
  super(parent, modal);
  initComponents();
  this.setLocation(1300,100);
}
    
asked by anonymous 02.02.2016 / 12:28

1 answer

3
  

As you were not informed whether the search should be only one   column, or in all of JTable , this example looks up   on all rows and in any column.

The RowSorter class is responsible for handling filters and sorts, but for components such as tables, there is an implementation of it called TableRowSorter , made to work with TableModel , which makes it much easier to add search to tables. Add in the same class where you construct your JTable an attribute like the one below:

private TableRowSorter tableRowSorter;

After building your table, assign the model you passed to it to tableRowSorter , so that it filters based on the same table model:

 this.tableRowSorter = new TableRowSorter(seuModel);

Or if you're using DefaultTableModel and anonymous methods and did not create your own model (though highly recommended you create your own TableModel ):

 this.tableRowSorter = new TableRowSorter(suaTable.getModel());

Once assigned to RowSorter on top of which model it will work, now assign this RowSorter to your table:

suajTable.setRowSorter(this.tableRowSorter);

Now, I recommend that you change the way you call JDialog , the second parameter of this class is a boolean that Informs if the window is modal or not , if you want a modal, you need to pass true , this way, the Frame is locked while the modal is open:

frmPesquisa pesq = new frmPesquisa(this, true);

In your modal (if you have not already done so), add an attribute of type String to store what is typed for the search and create a method that returns this variable. This method will be returned later for the main Frame.

private String textSearch = "";//vazio pra evitar problemas com nullPointerException

...

   public String getTextSearch() {
    return this.textSearch;
}

In%% of% of modal%, you will capture what was typed in listerner and assign% to% variable created previously, so when closing modal, just call method JButton in Frame to receive the search to be done in the table:

this.textSearch = this.seuTextField.getText();
this.dispose();// isso fecha o modal ao clicar no botao,
               // logo após capturar o texto digitado e retorna para o Frame

Now, in your%% of main%, just call the method created in the modal and pass JTextField as a filter:

String busca = modal.getTextSearch().trim();
tableRowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + busca));
  

The expression textSearch is a getTextSearch() of the language that indicates that the search will be case insensitive , meaning you will search for the filter, regardless of whether it is uppercase or lowercase. It can be removed if you do not want this behavior.

JFrame removes spaces in white around the string returned in the search, and RowSorter applies the filter as (?i) and displays the lines that contain the search you entered in your content.

  

If the search text is found even if it is a partial form of the content of the line (eg search 'eng', and the line has choked, or shelved), it will be returned the same way.

See a demonstration print I made to show how the search would work:

InthelinksbelowitispossibletofindmoredetailsaboutfiltersandordinancesinPattern,inadditiontofullandfunctionalexamples.

how to search an element in a JTable java?

JTable Row filtering by JTextField value

How to Make Dialogs - Oracle Documentation

Sorting and Filtering Tables with Java SE 6.0 (DevMedia)

Implementing Your Own TableModel (DevMedia)

Table Sorting and Filtering

How to Use Tables - Sorting and Filtering (Oracle)

    
03.02.2016 / 01:34