Olá Pessoal tudo bem!!!
Consegui fazer meu modelo de tabela usando o AbstractTableModel e consegui mostra-lo em uma JFrame, mas apenas tive sucesso em
fazer isso usando o BorderLayout.
O que estou querendo fazer é não ter que se preocurar em usar gerenciadores de Layout e usar a paleta do NetBeans, mas para apenas
desenhar o Layout, a minha JTable foi criada manualmente como foi citada acima.
import Telas.Tabelas.SpringUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
public class FiltroTabela extends JPanel {
private JTable table;
private JTextField filterText;
private JTextField statusText;
private TableRowSorter<MyTableModel> sorter;
public FiltroTabela(Object[][] data,String[] columnNames) {
super(new BorderLayout());
//setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//Create a table with a sorter.
MyTableModel model = new MyTableModel(columnNames,data);
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(800, 70));
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane,BorderLayout.PAGE_START);
JLabel l1 = new JLabel("Filter Text:");
add(l1,BorderLayout.LINE_START);
filterText = new JTextField();
//Whenever filterText changes, invoke newFilter.
filterText.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}
public void insertUpdate(DocumentEvent e) {
newFilter();
}
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
add(filterText,BorderLayout.CENTER);
private void newFilter() {
RowFilter<MyTableModel, Object> rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(filterText.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = null;
private Object[][] data = null;
public MyTableModel(String[] columnNames,Object[][] data){
this.columnNames = columnNames;
this.data = data;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
Acima é a classe que os componentes e um deles é o Meu Modelo de Tabela.
JFrame frame2 = new JFrame("Minha Tabela");
FiltroTabela filtroTabela = new FiltroTabela(getListaCarro().DadosTabela(), nomeColunas);
frame2.setContentPane(filtroTabela);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.pack();
frame2.setVisible(true);
Neste caso acima a classe funciona e exibe a minha tabela mas quando tento escrever ela usando a Paleta do NetBeans não consigo exibir a minha Tabela
O que tentei fazer é criar um Formulário JFrame no netBeans usar a paleta, adicionar um panel e coloca-lo em uma determinada posição e neste panel exibir
o meu modelo de Tabela que foi citado acima, mas sem se preocupar em escrever o layout, já que o NetBeans oferece este recurso.
Caso alguem possa me ajudar ficarei grato!!!