Olá pessoal!
Estou com um problema, já li tópicos similares aqui no GUJ e ainda não consegui resolver!
Tenho uma JTable e quero que um JFrame com os dados da linha selecionada na tabela seja exibido quando eu pressionar ENTER. Normalmente o que o ENTER faz em uma JTable é selecionar a próxima linha…
Tentei fazer o código abaixo, porém o evento normal do ENTER não foi totalmente sobrescrevido, pois quando eu pressiono ENTER até abre o JFrame, mas da próxima linha mesmo, isto é, agora tenho os dois eventos ao mesmo tempo…
Como resolver?!?
public JTable getTable() {
table = new JTable();
table.setSelectionMode(0); //somente uma linha pode ser selecionada
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
im.put(enter, im.get(KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, 0)));
final Action oldTabAction = table.getActionMap().get(im.get(enter));
Action enterAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
oldTabAction.actionPerformed( e );
JTable table = (JTable)e.getSource();
showItemDetails();
}
};
table.getActionMap().put(im.get(enter), enterAction);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2){
showItemDetails();
}
}
} );
return table;
}
Muito obrigado e um abraço a todos!