public void alteraComportamentoEnterTabela() { /* * Aqui eu sobrescrevo o comportamento do ENTER na tabela, por default * ele vai para a proxima linha, mas neste caso quero que pegue o valor * da atual para exibir o dados. */ tabela.setSelectionMode(0); // somente uma linha pode ser selecionada InputMap im = tabela.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); im.put(enter, im.get(KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, 0))); Action enterAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JTable tabela = (JTable) e.getSource(); } }; tabela.getActionMap().put(im.get(enter), enterAction); }
private Container createReturnPane() { // Create return button returnButton = new JButton(returnAction); // Create text fields retISBN = new JTextField(15); retCustID = new JTextField(15); // Create panel and layout JPanel pane = new JPanel(); pane.setOpaque(false); GridBagLayout gb = new GridBagLayout(); pane.setLayout(gb); GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(1, 5, 1, 5); // Fill panel c.anchor = GridBagConstraints.EAST; addToGridBag(gb, c, pane, new JLabel("ISBN:"), 0, 0, 1, 1); addToGridBag(gb, c, pane, new JLabel("Customer ID:"), 0, 1, 1, 1); c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.HORIZONTAL; addToGridBag(gb, c, pane, retISBN, 1, 0, 3, 1); addToGridBag(gb, c, pane, retCustID, 1, 1, 3, 1); c.fill = GridBagConstraints.NONE; addToGridBag(gb, c, pane, returnButton, 4, 0, 1, 3); // Set up VK_ENTER triggering the return button in this panel InputMap input = pane.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); input.put(getKeyStroke("ENTER"), "returnAction"); pane.getActionMap().put("returnAction", returnAction); return pane; }
public void teclasAtalhos() { // BOTAO EXCLUIR LOTE Action actionExcluir = new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { // simula o click no botão jBExcluirLote.grabFocus(); jBExcluirLote.doClick(); } }; // Associa o listener com a tecla f2 para que seja disparado toda vez, mesmo quando o foco não // está no botão KeyStroke keyStrokeExcluir = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0); String actionNameExcluir = "TECLA_F1"; InputMap inputMapExcluir = jBExcluirLote.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMapExcluir.put(keyStrokeExcluir, actionNameExcluir); ActionMap actionMapExcluir = jBExcluirLote.getActionMap(); actionMapExcluir.put(actionNameExcluir, actionExcluir); // BOTAO SAIR TELA // Action para o botao fechar Action actionTeclaFechar = new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { // simula o click no botão jBFechar.grabFocus(); jBFechar.doClick(); } }; // Associa o listener com a tecla esc para que seja disparado toda vez, mesmo quando o foco não // está no botão KeyStroke keyStrokeFechar = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); String actionNameFechar = "TECLA_ESC"; InputMap inputMapFechar = jBFechar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMapFechar.put(keyStrokeFechar, actionNameFechar); ActionMap actionMapFechar = jBFechar.getActionMap(); actionMapFechar.put(actionNameFechar, actionTeclaFechar); // BOTAO ATUALIZAR // Action para o botao Atualizar Action actionTeclaAtualizar = new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { // simula o click no botão jBAtualizar.grabFocus(); jBAtualizar.doClick(); } }; // Associa o listener com a tecla f6 para que seja disparado toda vez, mesmo quando o foco não // está no botão KeyStroke keyStrokeAtualizar = KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0); String actionNameAtualizar = "TECLA_F6"; InputMap inputMapAtualizar = jBAtualizar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMapAtualizar.put(keyStrokeAtualizar, actionNameAtualizar); ActionMap actionMapAtualizar = jBAtualizar.getActionMap(); actionMapAtualizar.put(actionNameAtualizar, actionTeclaAtualizar); // BOTAO MOSTRAR TODOS // Action para o botao exibir todos Action actionTeclaExibirTodos = new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { // simula o click no botão jBMostrarTodos.grabFocus(); jBMostrarTodos.doClick(); } }; // Associa o listener com a tecla f4 para que seja disparado toda vez, mesmo quando o foco não // está no botão KeyStroke keyStrokeExibirTodos = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0); String actionNameExibirTodos = "TECLA_F5"; InputMap inputMapExibirTodos = jBMostrarTodos.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMapExibirTodos.put(keyStrokeExibirTodos, actionNameExibirTodos); ActionMap actionMapExibirTodos = jBMostrarTodos.getActionMap(); actionMapExibirTodos.put(actionNameExibirTodos, actionTeclaExibirTodos); }
public AuthDialog(final JFrame parent, String title, boolean modal) { super(parent, title, modal); // Set up close behaviour setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { if (!okButtonClicked) System.exit(0); } }); // Set up OK button behaviour JButton okButton = new JButton("OK"); okButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (getUserName().length() == 0) { showMessageDialog( AuthDialog.this, "Please enter a username", "Format Error", ERROR_MESSAGE); return; } if (getDatabasePassword().length() == 0) { showMessageDialog( AuthDialog.this, "Please enter a password", "Format Error", ERROR_MESSAGE); return; } okButtonClicked = true; setVisible(false); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Set up dialog contents labelPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 5, 5)); inputPanel.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 20)); labelPanel.setLayout(new GridLayout(2, 1)); labelPanel.add(new JLabel("User Name: ")); labelPanel.add(new JLabel("Password:"******"ESCAPE"), "exitAction"); actionMap.put( "exitAction", new AbstractAction() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Pack it all pack(); // Center on the screen setLocationRelativeTo(null); }
private Container createBorrowPane() { // Initialise date combo boxes borDay = new JComboBox(); borMonth = new JComboBox(); borYear = new JComboBox(); String[] days = new String[31]; for (int i = 0; i < 31; i++) days[i] = String.valueOf(i + 1); String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; String[] years = { "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }; borDay.setModel(new DefaultComboBoxModel(days)); borMonth.setModel(new DefaultComboBoxModel(months)); borYear.setModel(new DefaultComboBoxModel(years)); Calendar today = Calendar.getInstance(); borDay.setSelectedIndex(today.get(DAY_OF_MONTH) - 1); borMonth.setSelectedIndex(today.get(MONTH)); borYear.setSelectedIndex(today.get(YEAR) - 2005); // Create borrow button borrowButton = new JButton(borrowAction); // Create text fields borISBN = new JTextField(15); borCustID = new JTextField(15); // Create panel and layout JPanel pane = new JPanel(); pane.setOpaque(false); GridBagLayout gb = new GridBagLayout(); pane.setLayout(gb); GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(1, 5, 1, 5); // Fill panel c.anchor = GridBagConstraints.EAST; addToGridBag(gb, c, pane, new JLabel("ISBN:"), 0, 0, 1, 1); addToGridBag(gb, c, pane, new JLabel("Customer ID:"), 0, 1, 1, 1); addToGridBag(gb, c, pane, new JLabel("Due Date:"), 0, 2, 1, 1); c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.HORIZONTAL; addToGridBag(gb, c, pane, borISBN, 1, 0, 3, 1); addToGridBag(gb, c, pane, borCustID, 1, 1, 3, 1); c.fill = GridBagConstraints.NONE; addToGridBag(gb, c, pane, borDay, 1, 2, 1, 1); addToGridBag(gb, c, pane, borMonth, 2, 2, 1, 1); addToGridBag(gb, c, pane, borYear, 3, 2, 1, 1); addToGridBag(gb, c, pane, borrowButton, 4, 0, 1, 3); // Set up VK_ENTER triggering the borrow button in this panel InputMap input = pane.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); input.put(getKeyStroke("ENTER"), "borrowAction"); pane.getActionMap().put("borrowAction", borrowAction); return pane; }