1
I have a celleditor
in the format of textarea
, and when you’re focused, I squeeze some word to type in celula
. It opens the edit mode, but does not write anything, which may be wrong?
working class
package javaapplication10;
/**
*
* @author Gabriel
*/
public class Funcionario {
private String nome;
public Funcionario(String nome, int idade, int matricula) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
class Funciotablemodel
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class FuncionarioTableModel extends AbstractTableModel {
private String colunas[] = {"nome"};
private ArrayList<Funcionario> funcionarios;
private final int COLUNA_NOME = 0;
public FuncionarioTableModel(ArrayList<Funcionario> funcionarios) {
this.funcionarios = funcionarios;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public int getRowCount() {
return funcionarios.size();
}
@Override
public int getColumnCount() {
return colunas.length;
}
@Override
public String getColumnName(int indice) {
return colunas[indice];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case COLUNA_NOME:
return String.class;
default:
return String.class;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Funcionario funcionario = this.funcionarios.get(rowIndex);
switch (columnIndex) {
case COLUNA_NOME:
return funcionario.getNome();
}
return null;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Funcionario funcionario = this.funcionarios.get(rowIndex);
switch (columnIndex) {
case COLUNA_NOME:
funcionario.setNome(String.valueOf(aValue));
break;
}
fireTableDataChanged();
}
}
class Jtableexemple
package javaapplication10;
/**
*
* @author Gabriel
*/
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
public class JTableExample extends JFrame {
private JTable tabela;
private JScrollPane scrollPainel;
public JTableExample() {
renderizarTela();
}
private void renderizarTela() {
Funcionario f1 = new Funcionario("",0,0);
Funcionario f2 = new Funcionario("", 0, 0);
Funcionario f3 = new Funcionario("", 0, 0);
Funcionario f4 = new Funcionario("", 0, 0);
ArrayList<Funcionario> funcionarios = new ArrayList<>();
funcionarios.add(f1);
funcionarios.add(f2);
funcionarios.add(f3);
funcionarios.add(f4);
FuncionarioTableModel model = new FuncionarioTableModel(funcionarios);
this.tabela = new JTable(model);
this.scrollPainel = new JScrollPane(tabela);
tabela.getColumnModel().getColumn(0).setCellEditor(new TextAreaEditor());
tabela.setRowHeight(50);
this.add(scrollPainel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JTableExample tb = new JTableExample();
tb.setLocationRelativeTo(null);
tb.setVisible(true);
}
});
}
}
// celleditor
class TextAreaEditor extends DefaultCellEditor {
protected JScrollPane scrollpane;
protected JTextArea textarea;
public TextAreaEditor() {
super(new JCheckBox());
scrollpane = new JScrollPane();
textarea = new JTextArea();
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
// textarea.setBorder(new TitledBorder("This is a JTextArea"));
scrollpane.getViewport().add(textarea);
// colocar para editar em 2 click
setClickCountToStart(2);
//
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
textarea.setText((String) value);
return scrollpane;
}
public Object getCellEditorValue() {
return textarea.getText();
}
}
What’s the problem? Your code worked normally here
– user28595
Excuse the delay Articuno! When he is focused on the cell and I try to type some writing or number, he opens the edition, but does not type. In case I double click it type. In that case I needed q when typing something it already activated the editing without giving both click
– GabrielBispo
Remove the line you added to enter the edit with two clicks then.
– user28595
In case I need to have her too.. You have to have both options. But the stranger is the same as he said, when you’re focused, try to type something and still not type.
– GabrielBispo
Dude you’re doing this celleditor wrong. Notice that you pass a Jcheckbox in the constructor but then return a Jscrollpane. If you are going to use components that are not a text field, a combo or a checkbox, you cannot use defaultcelleditor, you need to implement a celleditor from scratch. Your class
TextAreaEditor
needs to be reset from scratch, so it will never work properly.– user28595
Well, Articuno, I don’t understand much. Scroll I put so that when you finish typing and the writing does not fit in the cell, it automatically increases the cell. ja this checkbox I have no idea. could you give me a hand to clean this? I really appreciate
– GabrielBispo
I made a template but there is a problem, how do you intend to finish editing in the field? Enter does not work in the textarea, it breaks line.
– user28595
It was good with Tab, but tab also does not work neh?
– GabrielBispo
I managed to do, but without two clicks in the field, it opens straight for editing.
– user28595
with the 2 clicks is not possible?
– GabrielBispo
usually people always click on another cell to lose focus and can tab and in this case will already open another field
– GabrielBispo
It’s possible, but it doesn’t pay for the work. You have to choose, if it’s two clicks, I can’t help because I’d have to rewrite a lot and for something so simple I don’t think it’s worth the trouble.
– user28595
I get it, it might be the way you said
– GabrielBispo
I believe I will answer yes
– GabrielBispo