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)); }
/** * Create a Transferable to use as the source for a data transfer. * * @param c The component holding the data to be transfered. This argument is provided to enable * sharing of TransferHandlers by multiple components. * @return The representation of the data to be transfered. */ protected Transferable createTransferable(JComponent c) { Object[] values = null; if (c instanceof JList) { values = ((JList) c).getSelectedValues(); } else if (c instanceof JTable) { JTable table = (JTable) c; int[] rows = table.getSelectedRows(); if (rows != null) { values = new Object[rows.length]; for (int i = 0; i < rows.length; i++) { values[i] = table.getValueAt(rows[i], 0); } } } if (values == null || values.length == 0) { return null; } StringBuffer plainBuf = new StringBuffer(); StringBuffer htmlBuf = new StringBuffer(); htmlBuf.append("<html>\n<body>\n<ul>\n"); for (Object obj : values) { String val = ((obj == null) ? "" : obj.toString()); plainBuf.append(val + "\n"); htmlBuf.append(" <li>" + val + "\n"); } // remove the last newline plainBuf.deleteCharAt(plainBuf.length() - 1); htmlBuf.append("</ul>\n</body>\n</html>"); return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values); }
@Override public void mouseExited(MouseEvent e) { if (row >= 0 && col >= 0) { table.repaint(table.getCellRect(row, col, false)); } row = -1; col = -1; }
@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 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 Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { getModel().setRollover(highlighter.isHighlightableCell(row, column)); if (isSelected) { setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); // setBackground(row % 2 == 0 ? table.getBackground() : Color.WHITE); //Nimbus } setSelected(Objects.equals(value, Boolean.TRUE)); return this; }
public MainPanel() { super(new BorderLayout()); table.setAutoCreateRowSorter(true); JSplitPane sp = new JSplitPane( JSplitPane.VERTICAL_SPLIT, new JScrollPane(new JTable(model)), new JScrollPane(table)); sp.setResizeWeight(.5); add(sp); setPreferredSize(new Dimension(320, 240)); }
@Override public void mouseMoved(MouseEvent e) { Point pt = e.getPoint(); int prevRow = row; int prevCol = col; row = table.rowAtPoint(pt); col = table.columnAtPoint(pt); if (row < 0 || col < 0) { row = -1; col = -1; } // >>>> HyperlinkCellRenderer.java // @see // http://java.net/projects/swingset3/sources/svn/content/trunk/SwingSet3/src/com/sun/swingset3/demos/table/HyperlinkCellRenderer.java if (row == prevRow && col == prevCol) { return; } Rectangle repaintRect; if (row >= 0 && col >= 0) { Rectangle r = table.getCellRect(row, col, false); if (prevRow >= 0 && prevCol >= 0) { repaintRect = r.union(table.getCellRect(prevRow, prevCol, false)); } else { repaintRect = r; } } else { repaintRect = table.getCellRect(prevRow, prevCol, false); } table.repaint(repaintRect); // <<<< // table.repaint(); }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); String str = Objects.toString(value, ""); if (highlighter.isHighlightableCell(row, column)) { setText("<html><u>" + str); setForeground(isSelected ? table.getSelectionForeground() : HIGHLIGHT); setBackground(isSelected ? table.getSelectionBackground().darker() : table.getBackground()); } else { setText(str); setForeground(isSelected ? table.getSelectionForeground() : table.getForeground()); setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); } return this; }
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (table != null) { JTableHeader header = table.getTableHeader(); if (header != null) { setFont(header.getFont()); } } this.isSelected = isSelected; this.hasFocus = hasFocus; this.column = column; this.hasRollover = (column == rolloverColumn); if (skin == null || skin.getContentMargin() == null) { skin = XPStyle.getXP().getSkin(header, "header.headeritem"); } setText((value == null) ? "" : value.toString()); setBorder(new EmptyBorder(skin.getContentMargin())); return this; }
@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); }