Jtable does not save information when focusing on the cell

Asked

Viewed 292 times

4

My JTable does not save information when there is focus on the cell.

To effectively save the information I need to focus manually and then click on the "Register".

How can I make this process automatic, every time I click the "Sign Up" button it already removes the focus from the table and saves information?

Remembering that I’ve tried:

jtable.setFocusable(false);
jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

I’m working with a Model that I remodeled from a AbstractJTableModel, I’ll be leaving below:

public class JTreeTableModelMembro extends AbstractTreeTableModel {
   static protected String[] cNames;

   static protected Class[] cTypes = { TreeTableModel.class, String.class,
          String.class };

   private ArrayList<Membro> linhas = new ArrayList<Membro>();

   public JTreeTableModelMembro(Membro root, String[] cNames) {
       super(root);
       this.cNames = cNames;
       this.setMembro(root);
   }

   private void setMembro(Membro root) {
       for (Object grupoObjeto : root.getChildren()) {
           Membro cl = (Membro) grupoObjeto;
           this.linhas.add(null);
           for (Object objeto : cl.getChildren()) {
               this.linhas.add((Membro) objeto);
           }
       }
   }

   public ArrayList<Membro> getListaDeMembro() {
       ArrayList<Membro> lista = new ArrayList<Membro>();
       for(Membro objeto : this.linhas) {
           if(objeto != null) {
              lista.add(objeto);
           }
       }
       return lista;
   }

   public Membro getMembro(int indiceLinha) {
       return linhas.get(indiceLinha);
   }

   protected Membro getMembro(Object node) {
        Membro objeto = ((Membro) node);
        return objeto;
   }

   protected Object[] getChildren(Object node) {
       Membro fileNode = ((Membro) node);
       return fileNode.getChildren();
   }

   public void setRoot(Membro root) {
       if (this.root != root) {
           this.root = root;
           this.setMembro(root);
           fireTreeStructureChanged(this, new Object[] { root }, null, null);
       }
   }

   public void limpar() {
       this.root = new Membro();
   }

   @Override
   public Class getColumnClass(int column) {
       return cTypes[column];
   }

   @Override
   public Object getChild(Object node, int i) {
       return getChildren(node)[i];
   }

   @Override
   public int getChildCount(Object node) {
       Object[] children = getChildren(node);
       return (children == null) ? 0 : children.length;
   }

   @Override
   public int getColumnCount() {
       return cNames.length;
   }

   @Override
   public String getColumnName(int column) {
       return cNames[column];
   }

   @Override
   public Object getValueAt(Object node, int column) {
       Membro objeto = getMembro(node);

       switch (getColumnName(column)) {
          case "Descrição":
              return objeto.getTituloMembro();
          case "Medida":
              return objeto.getMembroMedida();
          case "Tipo Medida":
              return objeto.getTipoMedida();
          default:
              throw new IndexOutOfBoundsException("columnIndex out of bounds");
       }
   }

   @Override
   public boolean isCellEditable(int rowIndex, int column) {
       Membro objeto = getMembro(rowIndex);

       switch (getColumnName(column)) {
          case "Descrição":
            return true;
          case "Medida":
            return objeto == null ? false : true;
          case "Tipo Medida":
            return false;
          default:
            return false;
       }
   }

   @Override
   public void setValueAt(Object aValue, Object node, int column) {
       Membro objeto = this.getMembro(node);
       switch(getColumnName(column)) {
          case "Medida":
            if(aValue != null && !aValue.toString().isEmpty()) {
                objeto.setMembroMedida(new BigDecimal(aValue.toString()));
            } else {
                objeto.setMembroMedida(new BigDecimal(0));
            }
       }
   }
}
  • The problem is only the focus? If you set focus on another component solves? For example: JButton btn = new JButton(); btn.requestFocus();

1 answer

1

Try to use repaint()

jtable.repaint(); 
  • 3

    Please explain the answer better. Enrich it by describing the purpose of the method and where to use in the author’s code snippet, for example.

Browser other questions tagged

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