List of usage examples for javax.swing DefaultListSelectionModel getMinSelectionIndex
public int getMinSelectionIndex()
From source file:Main.java
public Main() { setLayout(new BorderLayout()); list = new JList(label); JScrollPane pane = new JScrollPane(list); DefaultListSelectionModel m = new DefaultListSelectionModel(); m.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { System.out.println(e.toString()); }/*from w ww. jav a 2s. c o m*/ }); list.setSelectionModel(m); System.out.println(m.getMinSelectionIndex()); add(pane, BorderLayout.NORTH); }
From source file:cz.muni.fi.javaseminar.kafa.bookregister.gui.MainWindow.java
private void initAuthorsTable() { authorsTable.getColumnModel().getColumn(2) .setCellEditor(new DatePickerCellEditor(new SimpleDateFormat("dd. MM. yyyy"))); authorsTable.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableCellRenderer() { @Override/* w w w.j ava 2s . co m*/ public Component getTableCellRendererComponent(JTable jtable, Object value, boolean selected, boolean hasFocus, int row, int column) { if (value instanceof Date) { // You could use SimpleDateFormatter instead value = new SimpleDateFormat("dd. MM. yyyy").format(value); } return super.getTableCellRendererComponent(jtable, value, selected, hasFocus, row, column); } }); authorsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ListSelectionModel selectionModel = authorsTable.getSelectionModel(); selectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { DefaultListSelectionModel source = (DefaultListSelectionModel) e.getSource(); if (source.getMinSelectionIndex() >= 0) { authorsTableModel.setCurrentSlectedIndex(source.getMinSelectionIndex()); } if (!e.getValueIsAdjusting()) { booksTableModel.setAuthorIndex(source.getMinSelectionIndex()); } } }); authorsTable.getColumnModel().getColumn(2) .setCellEditor(new DatePickerCellEditor(new SimpleDateFormat("dd. MM. yyyy"))); authorsTable.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable jtable, Object value, boolean selected, boolean hasFocus, int row, int column) { if (value instanceof Date) { // You could use SimpleDateFormatter instead value = new SimpleDateFormat("dd. MM. yyyy").format(value); } return super.getTableCellRendererComponent(jtable, value, selected, hasFocus, row, column); } }); JPopupMenu authorsPopupMenu = new JPopupMenu(); JMenuItem deleteItem = new JMenuItem("Delete"); authorsPopupMenu.addPopupMenuListener(new PopupMenuListener() { @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { int rowAtPoint = authorsTable.rowAtPoint( SwingUtilities.convertPoint(authorsPopupMenu, new Point(0, 0), authorsTable)); if (rowAtPoint > -1) { authorsTable.setRowSelectionInterval(rowAtPoint, rowAtPoint); authorsTableModel.setCurrentSlectedIndex(rowAtPoint); } } }); } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { // TODO Auto-generated method stub } @Override public void popupMenuCanceled(PopupMenuEvent e) { // TODO Auto-generated method stub } }); deleteItem.addActionListener(new ActionListener() { private Author author; @Override public void actionPerformed(ActionEvent e) { new SwingWorker<Void, Void>() { @Override protected Void doInBackground() throws Exception { author = authorsTableModel.getAuthors().get(authorsTable.getSelectedRow()); log.debug("Deleting author: " + author.getFirstname() + " " + author.getSurname() + " from database."); authorManager.deleteAuthor(author); return null; } @Override protected void done() { try { get(); } catch (InterruptedException | ExecutionException e) { if (e.getCause() instanceof DataIntegrityViolationException) { JOptionPane.showMessageDialog(MainWindow.this, "Couldn't delete author; there are still some books assigned to him.", "Error", JOptionPane.ERROR_MESSAGE); } log.error("There was an exception thrown during deletion author: " + author.getFirstname() + " " + author.getSurname(), e); return; } updateModel(); } }.execute(); } }); authorsPopupMenu.add(deleteItem); authorsTable.setComponentPopupMenu(authorsPopupMenu); }