Example usage for javax.swing JComponent setBackground

List of usage examples for javax.swing JComponent setBackground

Introduction

In this page you can find the example usage for javax.swing JComponent setBackground.

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The background color of the component.")
public void setBackground(Color bg) 

Source Link

Document

Sets the background color of this component.

Usage

From source file:MyButtonUI.java

public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_SPACE) {
        JComponent c = (JComponent) e.getComponent();
        c.setBorder(m_borderLowered);/*from   w w  w .  jav a2  s  . co m*/
        c.setBackground(m_backgroundPressed);
    }
}

From source file:lu.lippmann.cdb.common.gui.MultiPanel.java

/**
 * Constructor./*ww w. ja  va 2  s. co m*/
 */
public MultiPanel(final ListOrderedMap<JComponent, Integer> mapPanels, final int w, final int h,
        final boolean withWeight) {

    final int total = mapPanels.keySet().size();

    if (!withWeight)
        setLayout(new GridLayout(0, 1));

    final int w2 = w - 10 * total;
    final int h2 = h - 75;

    final Map<JComponent, Color> choosedColor = new HashMap<JComponent, Color>();

    int i = 0;
    for (final JComponent p : mapPanels.keySet()) {
        final Dimension size = new Dimension((int) (w2 * (mapPanels.get(p) / 100.0)), h2);
        p.setPreferredSize(size);
        choosedColor.put(p, COLORS[i]);
        p.setBackground(COLORS[i]);
        p.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add(p);
        i++;
    }

    if (withWeight) {
        /** add percents **/
        for (final JComponent p : mapPanels.keySet()) {
            final int perc = mapPanels.get(p);
            final int percent = (int) (w2 * (mapPanels.get(p) / 100.0));
            final Dimension size = new Dimension(percent, 20);
            final JPanel arrow = new JPanel();
            arrow.setPreferredSize(size);
            final JLabel label = new JLabel(perc + "%");
            label.setForeground(choosedColor.get(p).darker());
            arrow.add(label);
            add(arrow);
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractField.java

protected void decorateMissingValue(JComponent jComponent, boolean missingValueState) {
    jComponent.setBackground(missingValueState ? requiredBgColor : defaultBgColor);
}

From source file:net.lmxm.ute.gui.validation.AbstractInputValidator.java

@Override
public final boolean verify(final JComponent component) {
    final List<String> messages = validate(component);

    if (CollectionUtils.isEmpty(messages)) {
        component.setBackground(getDefaultBackgroundColor());

        return true;
    } else {//from   ww  w.j  a  va 2  s .  c  om
        component.setBackground(Color.PINK);
        displayMessagesDialog(component, messages);

        return false;
    }
}

From source file:com.rapidminer.template.gui.RoleRequirementSelector.java

public RoleRequirementSelector(final TemplateController controller) {
    super(new BorderLayout());
    setBorder(border);/*  w  ww .  ja  v  a  2  s  .c  om*/
    this.controller = controller;

    setBackground(Color.WHITE);
    updateRequirement();
    controller.getModel().addObserver(new Observer() {

        @Override
        public void update(Observable o, Object arg) {
            if (TemplateState.OBSERVER_EVENT_TEMPLATE.equals(arg)) {
                updateRequirement();
                updateComponents();
            } else if (TemplateState.OBSERVER_EVENT_ROLES.equals(arg)) {
                assignSelectionToCombo();
                updateComponents();
                attributeCombo.repaint();
            } else if (TemplateState.OBSERVER_EVENT_INPUT.equals(arg)) {
                updateAttributes();
            }
        }
    });

    AutoCompleteDecorator.decorate(attributeCombo);
    AutoCompleteDecorator.decorate(positiveClassCombo);

    attributeCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (areValuesAdjusting) {
                return;
            }
            assignRoles();
        }
    });
    positiveClassCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (areValuesAdjusting) {
                return;
            }
            assignRoles();
        }
    });

    helpLabel.setHorizontalAlignment(SwingConstants.CENTER);
    add(helpLabel, BorderLayout.PAGE_START);

    chartPanel = new ChartPanel(null, 250, 250, 100, 100, 360, 360, true, false, false, false, false, false);
    chartPanel.setMinimumDrawWidth(0);
    chartPanel.setMaximumDrawWidth(Integer.MAX_VALUE);
    chartPanel.setMinimumDrawHeight(0);
    chartPanel.setMaximumDrawHeight(Integer.MAX_VALUE);
    attributeCombo.setPreferredSize(new Dimension(100, 30));
    attributeCombo.setMaximumSize(new Dimension(150, 30));
    add(chartPanel, BorderLayout.CENTER);

    JComponent comboPanel = new JPanel();
    comboPanel.setLayout(new BoxLayout(comboPanel, BoxLayout.PAGE_AXIS));
    JComponent comboPanelAtt = new JPanel();
    comboPanelAtt.setBackground(Color.WHITE);
    comboPanelAtt.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    JComponent comboPanelClass = new JPanel();
    comboPanelClass.setBackground(Color.WHITE);
    comboPanelClass.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    positiveClassCombo.setPreferredSize(new Dimension(100, 30));
    positiveClassCombo.setMaximumSize(new Dimension(150, 30));
    positiveClassLabel.setLabelFor(positiveClassCombo);
    positiveClassLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
    positiveClassLabel.setToolTipText(positiveClassLabel.getText());
    positiveClassLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    columnLabel.setLabelFor(attributeCombo);
    columnLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
    columnLabel.setToolTipText(columnLabel.getText());
    columnLabel.setHorizontalAlignment(SwingConstants.RIGHT);

    comboPanelAtt.add(columnLabel);
    comboPanelAtt.add(attributeCombo);
    comboPanelClass.add(positiveClassLabel);
    comboPanelClass.add(positiveClassCombo);
    comboPanel.add(comboPanelAtt);
    comboPanel.add(comboPanelClass);

    add(comboPanel, BorderLayout.SOUTH);

    updateComponents();
}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

public static void setNumberInputVerifier(JTextField field) {
    // Add the input verifier
    field.setInputVerifier(new InputVerifier() {
        @Override/*from   w  w  w.j  a  va  2  s  . c  om*/
        public boolean verify(JComponent input) {
            JTextField field = (JTextField) input;
            String txt = field.getText();
            if (txt.isEmpty()) {
                field.setToolTipText(UILabels.STL50084_CANT_BE_BLANK.getDescription());
                input.setBackground(UIConstants.INTEL_LIGHT_RED);
                return false;
            }
            try {
                Utils.toLong(txt);
                input.setBackground(UIConstants.INTEL_WHITE);
                return true;
            } catch (Exception e) {
                field.setToolTipText(UILabels.STL50085_MUST_BE_NUMERIC.getDescription());
                input.setBackground(UIConstants.INTEL_LIGHT_RED);
            }
            return false;
        }

    });
}

From source file:edu.ku.brc.ui.skin.SkinItem.java

/**
 * @param comp/*from  w ww .j a va2  s. co m*/
 */
public void setupPanel(final JComponent comp) {
    boolean isOpaque = bgColor != null || StringUtils.isNotEmpty(imagePath);
    if (isOpaque) {
        comp.setOpaque(isOpaque);
    }

    if (bgColor != null) {
        comp.setBackground(bgColor);
    }

    if (comp instanceof JTiledPanel && (bgImage != null || StringUtils.isNotEmpty(imagePath))) {
        ((JTiledPanel) comp).setTileImage(getBGImage());
    }
}

From source file:DragColorTextFieldDemo.java

/**
 * Overridden to import a Color if it is available.
 * getChangesForegroundColor is used to determine whether the foreground or
 * the background color is changed.//from  w w w. java 2 s.co  m
 */
public boolean importData(JComponent c, Transferable t) {
    if (hasColorFlavor(t.getTransferDataFlavors())) {
        try {
            Color col = (Color) t.getTransferData(colorFlavor);
            if (getChangesForegroundColor()) {
                c.setForeground(col);
            } else {
                c.setBackground(col);
            }
            return true;
        } catch (UnsupportedFlavorException ufe) {
            System.out.println("importData: unsupported data flavor");
        } catch (IOException ioe) {
            System.out.println("importData: I/O exception");
        }
    }
    return false;
}

From source file:de.unentscheidbar.validation.swing.trigger.ChangeTriggerTest.java

@Override
protected void doNotProvokeTrigger(Iterable<JComponent> components) {

    for (final JComponent c : components) {
        robot.focusAndWaitForFocusGain(c);
        robot.focusAndWaitForFocusGain(gui.get(dummyKey));
        runInEdt(new Runnable() {

            @Override//from  ww w .  jav a 2s  .  c  o m
            public void run() {

                Color col;
                while ((col = new Color(rnd.nextInt())).equals(c.getForeground()))
                    ;
                c.setForeground(col);
                c.setBackground(col);
            }
        });
    }
}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

public static JPasswordField createPasswordField(DocumentListener... docListeners) {
    final JPasswordField field = new JPasswordField();
    final Border oldBorder = field.getBorder();
    if (docListeners != null) {
        for (DocumentListener docListener : docListeners) {
            field.getDocument().addDocumentListener(docListener);
            field.getDocument().putProperty("owner", field);
        }/*from w ww .j  a  va  2s  .com*/
    }
    final InputVerifier verifier = new InputVerifier() {
        @Override
        public boolean verify(final JComponent input) {
            final JPasswordField field = (JPasswordField) input;
            char[] txt = field.getPassword();
            if (txt.length == 0) {
                // Run in EDT to avoid deadlock in case this gets called
                // from not swing thread
                Util.runInEDT(new Runnable() {
                    @Override
                    public void run() {
                        field.setToolTipText(UILabels.STL50084_CANT_BE_BLANK.getDescription());
                        input.setBackground(UIConstants.INTEL_LIGHT_RED);
                        input.setBorder(BorderFactory.createLineBorder(UIConstants.INTEL_RED, 2));
                        // show tooltip immediately
                        ToolTipManager.sharedInstance()
                                .mouseMoved(new MouseEvent(field, 0, 0, 0, 0, 0, 0, false));
                    }
                });
                return false;
            } else {
                Util.runInEDT(new Runnable() {
                    @Override
                    public void run() {
                        input.setBackground(UIConstants.INTEL_WHITE);
                        input.setBorder(oldBorder);
                    }
                });
                return true;
            }
        }

    };

    DocumentListener dynamicChecker = new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            verifier.verify(field);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            verifier.verify(field);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            verifier.verify(field);
        }

    };
    field.getDocument().addDocumentListener(dynamicChecker);

    // Add the input verifier
    field.setInputVerifier(verifier);

    return field;
}