Update a Jtable that is in a Jframe from a Jdialog

Asked

Viewed 1,484 times

4

What I’m doing is a little longer, so I’m going to shorten the problem with a more objective example:

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

Follow code (in this case the example is with the setvisible that tb is not working, I believe it is the same problem):

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 to JDialog thus:

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

and try to update a JLabel on the screen of main thus 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 Search Form Builder (JDialog) is as follows:

public frmPesquisa(java.awt.Frame parent, boolean modal) {
  super(parent, modal);
  initComponents();
  this.setLocation(1300,100);
}
  • To make filters in JTable you need to use the class TableRowSorter passing his TableModel. Add your table model to the question to make it easy to answer.

  • Hi Diego! The problem is a little before you get into it. I’m not able to implement Listener, for example, if I create a Jdialog with a textbox and a boot ok, I wanted to update the main form with the contents of this one, but it won’t. I’ve tried everything. I’ve seen examples of how it would work if I did the form only in code, but I’m using the Netbeans interface and it’s not coming off. If you can have a simple example of how it would work it would be great. Thank you :)

  • Oh yes, add to the question how you call the jdialog in the jframe, and how you return the search to it back. With this information (beyond what you already have) can help you.

  • No problem! I call Dialog like this: private void tb_btn_searchActionPerformed(java.awt.Event.Actionevent evt) ' frmPesquisa pesq = new frmPesquisa(this,false); pesq.setVisible(true); }

  • and I try to update a Label on the main screen 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 Search form constructor (Jdialog) is as follows: public frmPesquisa(java.awt.Frame Parent, Boolean modal) { super(Parent, modal); initComponents(); this.setLocation(1300,100); }

Show 2 more comments

1 answer

3

As it was not informed if the research should be only one given column or in all of the JTable, this example searches in all rows and in any column.

The class RowSorter is responsible for handling filters and sorts, but for components like tables, there is an implementation called TableRowSorter, made to work with the TableModel, which makes it easy enough to add search into tables. Add in the same class where you build your JTable an attribute like the below:

private TableRowSorter tableRowSorter;

After constructing your table, assign the model you passed to her to tableRowSorter, so that it Filter based on the same model of the table:

 this.tableRowSorter = new TableRowSorter(seuModel);

Or if you’re using DefaultTableModel and anonymous methods and did not create a model of its own(although highly recommended that you create your own Tablemodel):

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

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

suajTable.setRowSorter(this.tableRowSorter);

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

frmPesquisa pesq = new frmPesquisa(this, true);

In your modal (if you haven’t already done so), add a type attribute String to store what is typed for the query and create a method that returns this variable. This method will be returned later to the main frame.

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

...

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

In the listerner of JButton modal, you will capture what was typed in JTextField and assign the variable textSearch previously created, thus, when closing the modal, just call the method getTextSearch() in the Frame to receive the query to be made 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 JFrame main, just call the method created in the modal and pass to the RowSorter as a filter:

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

The expression (?i) is a Pattern language indicating that the search will be case insensitive, i.e., it will search for the filter, regardless of case or lower case. It can be removed if you do not want this behavior.

The trim() removes whitespace around the string returned in the query, and the setRowFilter apply the filter in the form of regex and displays the lines that contain in your typed search content.

If the search text is found even as a partial form of the line content (e.g., search 'Eng', and the line has choked, or choked), it will be returned in the same way.

See a demo print I did to show how the survey would work:

Tabela com todas as linhas

inserir a descrição da imagem aqui


In the links below you can find more details about filters and JTable, in addition to complete and functional examples.

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)

  • @kirito the answer helped him to solve the problem? It would be interesting to mark it as chosen by clicking on v on the side, to serve as reference for others with similar problem.

Browser other questions tagged

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