private static Component setLabelColors( final Component label, final JTable table, final boolean isSelected, final int row) { if (label instanceof JComponent) { ((JComponent) label).setOpaque(true); } label.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground()); label.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); return label; }
@NotNull @Override public Component getTableCellRendererComponent( @NotNull JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { myTable = table; myRow = row; myColumn = column; boolean isEnabled = true; final DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((TreeTable) table).getTree().getPathForRow(row).getLastPathComponent(); Option key = null; if (node instanceof MyTreeNode) { isEnabled = ((MyTreeNode) node).isEnabled(); key = ((MyTreeNode) node).getKey(); } if (!table.isEnabled()) { isEnabled = false; } Color background = table.getBackground(); if (value instanceof Boolean) { myCheckBox.setSelected(((Boolean) value).booleanValue()); myCheckBox.setBackground(background); myCheckBox.setEnabled(isEnabled); return myCheckBox; } else if (value instanceof String) { /* myComboBox.removeAllItems(); myComboBox.addItem(value); */ myComboBox.setText((String) value); myComboBox.setBackground(background); myComboBox.setEnabled(isEnabled); return myComboBox; } else if (value instanceof Integer) { if (key instanceof IntOption && ((IntOption) key).isDefaultValue(value)) { myIntLabel.setText(((IntOption) key).getDefaultValueText()); } else { myIntLabel.setText(value.toString()); } return myIntLabel; } myCheckBox.putClientProperty("JComponent.sizeVariant", "small"); myComboBox.putClientProperty("JComponent.sizeVariant", "small"); myEmptyLabel.setBackground(background); return myEmptyLabel; }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (!table.isCellEditable(row, column)) { myBlankPanel.setBackground( isSelected ? table.getSelectionBackground() : table.getBackground()); return myBlankPanel; } return myDelegate.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); }
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { final RegistryValue v = ((MyTableModel) table.getModel()).getRegistryValue(row); myLabel.setIcon(null); myLabel.setText(null); myLabel.setHorizontalAlignment(JLabel.LEFT); if (v != null) { switch (column) { case 0: myLabel.setIcon(v.isRestartRequired() ? RESTART_ICON : null); myLabel.setHorizontalAlignment(JLabel.CENTER); break; case 1: myLabel.setText(v.getKey()); break; case 2: if (v.asColor(null) != null) { myLabel.setIcon(createColoredIcon(v.asColor(null))); } else if (v.isBoolean()) { final JCheckBox box = new JCheckBox(); box.setSelected(v.asBoolean()); box.setBackground(table.getBackground()); return box; } else { myLabel.setText(v.asString()); } } myLabel.setOpaque(true); myLabel.setFont( myLabel.getFont().deriveFont(v.isChangedFromDefault() ? Font.BOLD : Font.PLAIN)); myLabel.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground()); myLabel.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); } return myLabel; }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setFont(table.getFont()); setText(Objects.toString(value, "")); return this; }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } // Select the current value setSelectedItem(value); return this; }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (!isSelected) { MyTableModel tableModel = (MyTableModel) table.getModel(); Color color = table.getBackground(); switch (tableModel.getKind(row)) { case ERROR: color = new Color(255, 175, 175); break; case CONFLICT: color = new Color(255, 240, 240); break; } result.setBackground(color); } return result; }
@Nullable public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { myValue = ((MyTableModel) table.getModel()).getRegistryValue(row); if (myValue.asColor(null) != null) { final Color color = ColorChooser.chooseColor( table, "Choose color", ((RegistryValue) value).asColor(Color.WHITE)); if (color != null) { myValue.setValue(color.getRed() + "," + color.getGreen() + "," + color.getBlue()); } return null; } else if (myValue.isBoolean()) { myCheckBox.setSelected(myValue.asBoolean()); myCheckBox.setBackground(table.getBackground()); return myCheckBox; } else { myField.setText(myValue.asString()); myField.setBorder(null); myField.selectAll(); return myField; } }
@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { final DefaultMutableTreeNode defaultNode = (DefaultMutableTreeNode) ((TreeTable) table).getTree().getPathForRow(row).getLastPathComponent(); myCurrentEditor = null; myCurrentNode = null; if (defaultNode instanceof MyTreeNode) { MyTreeNode node = (MyTreeNode) defaultNode; myCurrentNode = node; if (node.getKey() instanceof BooleanOption) { myCurrentEditor = myBooleanEditor; myBooleanEditor.setSelected(node.getValue() == Boolean.TRUE); myBooleanEditor.setEnabled(node.isEnabled()); } else if (node.getKey() instanceof IntOption) { IntOption intOption = (IntOption) node.getKey(); myCurrentEditor = myIntOptionsEditor; myIntOptionsEditor.setText( intOption.isDefaultValue(node.getValue()) ? "" : node.getValue().toString()); myIntOptionsEditor.setMinValue(intOption.getMinValue()); myIntOptionsEditor.setMaxValue(intOption.getMaxValue()); myIntOptionsEditor.setDefaultValue(intOption.getDefaultValue()); } else { myCurrentEditor = myOptionsEditor; myOptionsEditor.setCell(table, row, column); myOptionsEditor.setText(String.valueOf(node.getValue())); myOptionsEditor.setOptions(((SelectionOption) node.getKey()).options); myOptionsEditor.setDefaultValue(node.getValue()); } } if (myCurrentEditor != null) { myCurrentEditor.setBackground(table.getBackground()); } return myCurrentEditor; }
// {{{ InstallPanel constructor InstallPanel(PluginManager window, boolean updates) { super(new BorderLayout(12, 12)); this.window = window; this.updates = updates; setBorder(new EmptyBorder(12, 12, 12, 12)); final JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); split.setResizeWeight(0.75); /* Setup the table */ table = new JTable(pluginModel = new PluginTableModel()); table.setShowGrid(false); table.setIntercellSpacing(new Dimension(0, 0)); table.setRowHeight(table.getRowHeight() + 2); table.setPreferredScrollableViewportSize(new Dimension(500, 200)); table.setDefaultRenderer( Object.class, new TextRenderer((DefaultTableCellRenderer) table.getDefaultRenderer(Object.class))); table.addFocusListener(new TableFocusHandler()); InputMap tableInputMap = table.getInputMap(JComponent.WHEN_FOCUSED); ActionMap tableActionMap = table.getActionMap(); tableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "tabOutForward"); tableActionMap.put("tabOutForward", new KeyboardAction(KeyboardCommand.TAB_OUT_FORWARD)); tableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK), "tabOutBack"); tableActionMap.put("tabOutBack", new KeyboardAction(KeyboardCommand.TAB_OUT_BACK)); tableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "editPlugin"); tableActionMap.put("editPlugin", new KeyboardAction(KeyboardCommand.EDIT_PLUGIN)); tableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "closePluginManager"); tableActionMap.put( "closePluginManager", new KeyboardAction(KeyboardCommand.CLOSE_PLUGIN_MANAGER)); TableColumn col1 = table.getColumnModel().getColumn(0); TableColumn col2 = table.getColumnModel().getColumn(1); TableColumn col3 = table.getColumnModel().getColumn(2); TableColumn col4 = table.getColumnModel().getColumn(3); TableColumn col5 = table.getColumnModel().getColumn(4); col1.setPreferredWidth(30); col1.setMinWidth(30); col1.setMaxWidth(30); col1.setResizable(false); col2.setPreferredWidth(180); col3.setPreferredWidth(130); col4.setPreferredWidth(70); col5.setPreferredWidth(70); JTableHeader header = table.getTableHeader(); header.setReorderingAllowed(false); header.addMouseListener(new HeaderMouseHandler()); header.setDefaultRenderer( new HeaderRenderer((DefaultTableCellRenderer) header.getDefaultRenderer())); scrollpane = new JScrollPane(table); scrollpane.getViewport().setBackground(table.getBackground()); split.setTopComponent(scrollpane); /* Create description */ JScrollPane infoPane = new JScrollPane(infoBox = new PluginInfoBox()); infoPane.setPreferredSize(new Dimension(500, 100)); split.setBottomComponent(infoPane); EventQueue.invokeLater( new Runnable() { @Override public void run() { split.setDividerLocation(0.75); } }); final JTextField searchField = new JTextField(); searchField.addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) { table.dispatchEvent(e); table.requestFocus(); } } }); searchField .getDocument() .addDocumentListener( new DocumentListener() { void update() { pluginModel.setFilterString(searchField.getText()); } @Override public void changedUpdate(DocumentEvent e) { update(); } @Override public void insertUpdate(DocumentEvent e) { update(); } @Override public void removeUpdate(DocumentEvent e) { update(); } }); table.addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int i = table.getSelectedRow(), n = table.getModel().getRowCount(); if (e.getKeyCode() == KeyEvent.VK_DOWN && i == (n - 1) || e.getKeyCode() == KeyEvent.VK_UP && i == 0) { searchField.requestFocus(); searchField.selectAll(); } } }); Box filterBox = Box.createHorizontalBox(); filterBox.add(new JLabel("Filter : ")); filterBox.add(searchField); add(BorderLayout.NORTH, filterBox); add(BorderLayout.CENTER, split); /* Create buttons */ Box buttons = new Box(BoxLayout.X_AXIS); buttons.add(new InstallButton()); buttons.add(Box.createHorizontalStrut(12)); buttons.add(new SelectallButton()); buttons.add(chooseButton = new ChoosePluginSet()); buttons.add(new ClearPluginSet()); buttons.add(Box.createGlue()); buttons.add(new SizeLabel()); add(BorderLayout.SOUTH, buttons); String path = jEdit.getProperty(PluginManager.PROPERTY_PLUGINSET, ""); if (!path.isEmpty()) { loadPluginSet(path); } } // }}}
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); return this; }
public static void main(String args[]) { // style that is necessary try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (UnsupportedLookAndFeelException e) { } // Standard preparation for a frame fmain = new JFrame("Schedule Appointments"); // Create and name frame fmain.setSize(330, 375); // Set size to 400x400 pixels pane = fmain.getContentPane(); pane.setLayout(null); // Apply null layout fmain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when X is clicked // controls and portions of Calendar lmonth = new JLabel("January"); lyear = new JLabel("Change year:"); cyear = new JComboBox(); prev = new JButton("<<"); next = new JButton(">>"); canc = new JButton("Cancel"); mcal = new DefaultTableModel() { public boolean isCellEditable(int rowIndex, int mColIndex) { return false; } }; Cal = new JTable(mcal); scal = new JScrollPane(Cal); pcal = new JPanel(null); canc.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); // action listeners for buttons and the like prev.addActionListener(new btnPrev_Action()); next.addActionListener(new btnNext_Action()); cyear.addActionListener(new cmbYear_Action()); Cal.addMouseListener(new mouseCont()); // Adding the elements to the pane pane.add(pcal); pcal.add(lmonth); pcal.add(cyear); pcal.add(prev); pcal.add(next); pcal.add(canc); pcal.add(scal); // Setting where the elements are on the pane pcal.setBounds(0, 0, 320, 335); lmonth.setBounds(160 - lmonth.getPreferredSize().width / 2, 25, 100, 25); canc.setBounds(10, 305, 80, 20); cyear.setBounds(215, 305, 100, 20); prev.setBounds(10, 25, 50, 25); next.setBounds(260, 25, 50, 25); scal.setBounds(10, 50, 300, 250); // Make frame visible fmain.setResizable(false); fmain.setVisible(true); // Inner workings for the day mechanism GregorianCalendar cal = new GregorianCalendar(); // Create calendar rday = cal.get(GregorianCalendar.DAY_OF_MONTH); // Get day rmonth = cal.get(GregorianCalendar.MONTH); // Get month ryear = cal.get(GregorianCalendar.YEAR); // Get year currentMonth = rmonth; // Match month and year currentYear = ryear; // Add days String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // All of the days for (int i = 0; i < 7; i++) { mcal.addColumn(days[i]); } Cal.getParent().setBackground(Cal.getBackground()); // Set background // No resize/reorder Cal.getTableHeader().setResizingAllowed(false); Cal.getTableHeader().setReorderingAllowed(false); // Single cell selection Cal.setColumnSelectionAllowed(true); Cal.setRowSelectionAllowed(true); Cal.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Set row/column count Cal.setRowHeight(38); mcal.setColumnCount(7); mcal.setRowCount(6); // Placing the dates in the cells for (int i = ryear - 100; i <= ryear + 100; i++) { cyear.addItem(String.valueOf(i)); } // Refresh calendar refreshCalendar(rmonth, ryear); // Refresh calendar }
public Component getTableCellRendererComponent( JTable table, Setting owner, Object value, boolean isSelected, boolean hasFocus, boolean isEnabled, int row, int column) { // renderer.setMargin(new Insets(0, 2, 4, 2)); if (isSelected) { renderer.setForeground(table.getSelectionForeground()); panel.setBackground(table.getSelectionBackground()); blank1.setBackground(table.getSelectionBackground()); blank2.setBackground(table.getSelectionBackground()); } else { renderer.setForeground(table.getForeground()); panel.setBackground(table.getBackground()); blank1.setBackground(table.getBackground()); blank2.setBackground(table.getBackground()); } if (hasFocus) { panel.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); } else { panel.setBorder(new EmptyBorder(1, 2, 2, 1)); } if (value instanceof Color) { Color col = (Color) value; // System.out.println("setting background to "+col.toString()); renderer.setText("[" + col.getRed() + "," + col.getGreen() + "," + col.getBlue() + "]"); renderer.setEnabled(isEnabled); renderer.setFont(font); colourPanel.setBackground(col); } else if (value instanceof ArrayList) { ArrayList values = (ArrayList) value; if (values.size() > 0) { // if we have multiple properties selected. Color last = null; boolean allSame = true; for (int i = 0; i < values.size(); i++) { if (values.get(i) instanceof Color) { Color str = (Color) values.get(i); if (last != null) { if (!str.equals(last)) { allSame = false; break; } last = str; } else { last = str; } } } if (allSame) { renderer.setText( "[" + last.getRed() + "," + last.getGreen() + "," + last.getBlue() + "]"); renderer.setEnabled(isEnabled); renderer.setFont(font); colourPanel.setBackground(last); } else { renderer.setText("(Different values)"); renderer.setEnabled(isEnabled); renderer.setFont(font); colourPanel.setBackground(Color.lightGray); panel.setBackground(Color.lightGray); blank1.setBackground(Color.lightGray); blank2.setBackground(Color.lightGray); } } } return panel; }