public MainPanel() { super(new BorderLayout()); InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); KeyStroke stab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK); KeyStroke senter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK); im.put(tab, im.get(enter)); im.put(stab, im.get(senter)); final Color orgColor = table.getSelectionBackground(); final Color tflColor = this.getBackground(); table.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { table.setSelectionForeground(Color.WHITE); table.setSelectionBackground(orgColor); } @Override public void focusLost(FocusEvent e) { table.setSelectionForeground(Color.BLACK); table.setSelectionBackground(tflColor); } }); table.setComponentPopupMenu(new TablePopupMenu()); add(new JScrollPane(table)); setPreferredSize(new Dimension(320, 240)); }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { textLabel.setFont(table.getFont()); textLabel.setText(Objects.toString(value, "")); textLabel.setBorder(hasFocus ? focusCellHighlightBorder : noFocusBorder); FontMetrics fm = table.getFontMetrics(table.getFont()); Insets i = textLabel.getInsets(); int swidth = iconLabel.getPreferredSize().width + fm.stringWidth(textLabel.getText()) + i.left + i.right; int cwidth = table.getColumnModel().getColumn(column).getWidth(); dim.width = swidth > cwidth ? cwidth : swidth; if (isSelected) { textLabel.setOpaque(true); textLabel.setForeground(table.getSelectionForeground()); textLabel.setBackground(table.getSelectionBackground()); iconLabel.setIcon(sicon); } else { textLabel.setOpaque(false); textLabel.setForeground(table.getForeground()); textLabel.setBackground(table.getBackground()); iconLabel.setIcon(nicon); } return panel; }
public void fixSelection(int rows[]) { if (rows.length == 0) return; table.getSelectionModel().setValueIsAdjusting(true); int rowcount = table.getModel().getRowCount(); for (int x = 0; x < rows.length; x++) { if (rows[x] < rowcount) { table.getSelectionModel().addSelectionInterval(rows[x], rows[x]); } } table.getSelectionModel().setValueIsAdjusting(false); }
public FileNameRenderer(JTable table) { Border b = UIManager.getBorder("Table.noFocusBorder"); if (Objects.isNull(b)) { // Nimbus??? Insets i = focusCellHighlightBorder.getBorderInsets(textLabel); b = BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right); } noFocusBorder = b; p.setOpaque(false); panel.setOpaque(false); // http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons nicon = new ImageIcon(getClass().getResource("wi0063-16.png")); sicon = new ImageIcon( p.createImage( new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter()))); iconLabel = new JLabel(nicon); iconLabel.setBorder(BorderFactory.createEmptyBorder()); p.add(iconLabel, BorderLayout.WEST); p.add(textLabel); panel.add(p, BorderLayout.WEST); Dimension d = iconLabel.getPreferredSize(); dim.setSize(d); table.setRowHeight(d.height); }
@Override public void updateUI() { // Bug ID: 6788475 Changing to Nimbus LAF and back doesn't reset look and feel of JTable // completely // http://bugs.java.com/view_bug.do?bug_id=6788475 // XXX: set dummy ColorUIResource setSelectionForeground(new ColorUIResource(Color.RED)); setSelectionBackground(new ColorUIResource(Color.RED)); super.updateUI(); putClientProperty("Table.isFileList", Boolean.TRUE); setCellSelectionEnabled(true); setIntercellSpacing(new Dimension()); setShowGrid(false); setAutoCreateRowSorter(true); setFillsViewportHeight(true); setDefaultRenderer( Object.class, new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { return super.getTableCellRendererComponent(table, value, false, false, row, column); } }); TableColumn col = getColumnModel().getColumn(0); col.setCellRenderer(new FileNameRenderer(this)); col.setPreferredWidth(200); col = getColumnModel().getColumn(1); col.setPreferredWidth(300); }
public void end() { final int[] selected = table.getSelectedRows(); model.clear(rows.size()); Iterator i = rows.iterator(); while (i.hasNext()) { model.addEntry((Map) i.next()); } rows.clear(); if (SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater( new Runnable() { public void run() { model.fireListeners(); fixSelection(selected); } }); } else { model.fireListeners(); fixSelection(selected); } }
public NetworkTable(Properties display) { this.display = display; model = new GenericTableModel( new String[] {" ", "Address", "Label", "Description", "Pivot"}, "Address", 256); table = new ATable(model); TableRowSorter sorter = new TableRowSorter(model); sorter.toggleSortOrder(1); Comparator hostCompare = new Comparator() { public int compare(Object a, Object b) { long aa = Route.ipToLong(a + ""); long bb = Route.ipToLong(b + ""); if (aa > bb) { return 1; } else if (aa < bb) { return -1; } else { return 0; } } public boolean equals(Object a, Object b) { return (a + "").equals(b + ""); } }; sorter.setComparator(1, hostCompare); sorter.setComparator(4, hostCompare); table.setRowSorter(sorter); table.setColumnSelectionAllowed(false); table.getColumn("Address").setPreferredWidth(125); table.getColumn("Label").setPreferredWidth(125); table.getColumn("Pivot").setPreferredWidth(125); table.getColumn(" ").setPreferredWidth(32); table.getColumn(" ").setMaxWidth(32); table.getColumn("Description").setPreferredWidth(500); final TableCellRenderer parent = table.getDefaultRenderer(Object.class); table.setDefaultRenderer( Object.class, new TableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { JLabel component = (JLabel) parent.getTableCellRendererComponent(table, value, isSelected, false, row, col); if (col == 4 && Boolean.TRUE.equals(model.getValueAt(table, row, "Active"))) { component.setFont(component.getFont().deriveFont(Font.BOLD)); } else if (col == 1 && !"".equals(model.getValueAt(table, row, "Description"))) { component.setFont(component.getFont().deriveFont(Font.BOLD)); } else { component.setFont(component.getFont().deriveFont(Font.PLAIN)); } String tip = model.getValueAt(table, row, "Tooltip") + ""; if (tip.length() > 0) { component.setToolTipText(tip); } return component; } }); table .getColumn(" ") .setCellRenderer( new TableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { JLabel component = (JLabel) parent.getTableCellRendererComponent( table, value, isSelected, false, row, col); component.setIcon(new ImageIcon((Image) model.getValueAt(table, row, "Image"))); component.setText(""); String tip = model.getValueAt(table, row, "Tooltip") + ""; if (tip.length() > 0) { component.setToolTipText(tip); } return component; } }); table.addMouseListener( new MouseAdapter() { public void all(MouseEvent ev) { if (ev.isPopupTrigger()) { popup.showGraphPopup(getSelectedHosts(), ev); ev.consume(); } } public void mouseClicked(MouseEvent ev) { all(ev); } public void mousePressed(MouseEvent ev) { all(ev); } public void mouseReleased(MouseEvent ev) { all(ev); } }); setLayout(new BorderLayout()); scroller = new JScrollPane(table); add(scroller, BorderLayout.CENTER); }
public void setTransferHandler(TransferHandler t) { table.setTransferHandler(t); }