import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.JTextComponent;
public class comboMulticolumn extends javax.swing.JFrame {
private String[] colunas = null;
private String[][] linhas = null;
private RowFilter<Object, Object> rf = null;
private TableRowSorter<TableModel> sorter = null;
private DefaultTableModel tm = new DefaultTableModel();
private int colFiltro = 0;
private int i = -1;
/** Creates new form comboboxMulticolumn */
public comboMulticolumn() {
colunas = new String[]{"Sigla", "Estado"};
linhas = new String[][]{
{"DF", "Distrito Federal"},
{"ES", "Espírito Santo"},
{"GO", "Goias"},
{"MA", "Maranhão"},
{"MG", "Minas Gerais"},
{"PR", "Paraná"},
{"RJ", "Rio de Janeiro"},
{"RN", "Rio Grande do Norte"},
{"RO", "Rondônia"},
{"RR", "Rorâima"},
{"RS", "Rio Grande do Sul"},
{"SC", "Santa Catarina"},
{"SE", "Sergipe"},
{"SP", "São Paulo"},
{"TO", "Tocantins"}
};
initComponents();
jComboBox1.setEditable(true);
jComboBox1.setUI(new MyComboUI());
tm = new DefaultTableModel(linhas, colunas);
jTable1.setModel(tm);
filtraDados(tm);
}
protected class MyComboUI extends MetalComboBoxUI {
//cria o menu pop-up do combobox
@Override
protected ComboPopup createPopup() {
return (ComboPopup) new TableComboPopup(this);
}
}
public class TableComboPopup extends BasicComboPopup implements ListSelectionListener {
private JScrollPane pane;
public TableComboPopup(MyComboUI ui) {
super(jComboBox1);
jTable1.setAutoCreateRowSorter(true);
pane = new JScrollPane(jTable1);
jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.getSelectionModel().addListSelectionListener(this);
}
@Override
public void show() {
super.removeAll();
Dimension dim = new Dimension(300, 200);
pane.setPreferredSize(dim);
super.add(pane);
selectRow();
super.show();
}
private void selectRow() {
jTable1.addMouseListener(new
MouseAdapter( ) {
@Override
public void mouseClicked(java.awt.event.MouseEvent me) {
jComboBox1.setPopupVisible(false);
final JTextComponent editor = (JTextComponent) jComboBox1.getEditor().getEditorComponent();
int posicao = jTable1.getSelectedRow();
editor.setText(jTable1.getValueAt(posicao, colFiltro).toString());
}
});
}
public void valueChanged(ListSelectionEvent e) {
//jComboBox1.setSelectedIndex(jTable1.getSelectedRow());
}
public void jComboBox1ItemStateChanged(ItemEvent e) {
if (e.getStateChange() == e.DESELECTED) {
return;
}
jTable1.getSelectionModel().removeListSelectionListener(this);
selectRow();
jTable1.getSelectionModel().addListSelectionListener(this);
}
}
public void filtraDados(TableModel tm) {
final JTextComponent editor = (JTextComponent) jComboBox1.getEditor().getEditorComponent();
sorter = new TableRowSorter<TableModel>(tm);
jTable1.setRowSorter(sorter);
editor.addCaretListener(new
javax.swing.event.CaretListener
() {
public void caretUpdate(javax.swing.event.CaretEvent e) {
String text = "(?i)^" + editor.getText();
rf = RowFilter.regexFilter(text, colFiltro);
sorter.setRowFilter(rf);
}
});
editor.addKeyListener(new
KeyAdapter( ) {
@Override
public void keyPressed(java.awt.event.KeyEvent e) {
jComboBox1.setPopupVisible(true);
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (i < jTable1.getRowCount() - 1) {
i++;
} else {
i = 0;
}
jTable1.addRowSelectionInterval(i, i);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (i >= 1) {
i--;
jTable1.addRowSelectionInterval(i, i);
} else {
i = jTable1.getRowCount() - 1;
}
}
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
editor.setEditable(true);
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
int posicao = jTable1.getSelectedRow();
editor.setText(jTable1.getValueAt(posicao, colFiltro).toString());
}
}
//se a jtable for filtrada e não restar nehum item, a última letra digitada é apagada
@Override
public void keyReleased(java.awt.event.KeyEvent e) {
if (jTable1.getRowCount() == 0) {
editor.setText(editor.getText().substring(0, editor.getText().length() - 1));
}
if (jTable1.getRowCount() == 0 && editor.getText().length() >= 1) {
editor.setText("");
}
}
});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jComboBox1 = new javax.swing.JComboBox();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(106, 106, 106)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(117, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(97, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new comboMulticolumn().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}