Example usage for javax.swing JLabel getBackground

List of usage examples for javax.swing JLabel getBackground

Introduction

In this page you can find the example usage for javax.swing JLabel getBackground.

Prototype

@Transient
public Color getBackground() 

Source Link

Document

Gets the background color of this component.

Usage

From source file:JColorChooserSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());
    colorChooser.setBorder(BorderFactory.createTitledBorder("Pick Foreground Color"));

    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();//from ww  w  . j a v a2s . c o m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());
    colorChooser.setBorder(BorderFactory.createTitledBorder("Pick Color for java2s.com"));

    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            Color newForegroundColor = colorChooser.getColor();
            label.setForeground(newForegroundColor);
        }//from w  ww .ja va  2 s  .c o m
    };
    model.addChangeListener(changeListener);
    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
}

From source file:ListeningJColorChooserSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));

    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());

    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            Color newForegroundColor = colorChooser.getColor();
            label.setForeground(newForegroundColor);
        }//w  w  w .j a  v a  2  s  . c om
    };
    model.addChangeListener(changeListener);

    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
}

From source file:com.clank.launcher.swing.SwingHelper.java

/**
 * Show a message dialog using/*from w  w w.j  av a2  s  . co  m*/
 * {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int)}.
 *
 * <p>The dialog will be shown from the Event Dispatch Thread, regardless of the
 * thread it is called from. In either case, the method will block until the
 * user has closed the dialog (or dialog creation fails for whatever reason).</p>
 *
 * @param parentComponent the frame from which the dialog is displayed, otherwise
 *                        null to use the default frame
 * @param message the message to display
 * @param title the title string for the dialog
 * @param messageType see {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int)}
 *                    for available message types
 */
public static void showMessageDialog(final Component parentComponent, @NonNull final String message,
        @NonNull final String title, final String detailsText, final int messageType) {

    if (SwingUtilities.isEventDispatchThread()) {
        // To force the label to wrap, convert the message to broken HTML
        String htmlMessage = "<html><div style=\"width: 250px\">" + htmlEscape(message);

        JPanel panel = new JPanel(new BorderLayout(0, detailsText != null ? 20 : 0));

        // Add the main message
        panel.add(new JLabel(htmlMessage), BorderLayout.NORTH);

        // Add the extra details
        if (detailsText != null) {
            JTextArea textArea = new JTextArea(_("errors.reportErrorPreface") + detailsText);
            JLabel tempLabel = new JLabel();
            textArea.setFont(tempLabel.getFont());
            textArea.setBackground(tempLabel.getBackground());
            textArea.setTabSize(2);
            textArea.setEditable(false);
            textArea.setComponentPopupMenu(TextFieldPopupMenu.INSTANCE);

            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(350, 120));
            panel.add(scrollPane, BorderLayout.CENTER);
        }

        JOptionPane.showMessageDialog(parentComponent, panel, title, messageType);
    } else {
        // Call method again from the Event Dispatch Thread
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    showMessageDialog(parentComponent, message, title, detailsText, messageType);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.sshtools.common.ui.SshToolsApplicationPanel.java

/**
 * Show an error message with toggable detail
 *
 * @param parent/*from w  w w.j ava2s  . co  m*/
 * @param mesg
 * @param title
 * @param exception
 */

public static void showErrorMessage(Component parent, String mesg,

        String title, Throwable exception) {

    boolean details = false;

    while (true) {

        String[] opts = new String[] {

                details ? "Hide Details" : "Details", "Ok"

        };

        StringBuffer buf = new StringBuffer();

        if (mesg != null) {

            buf.append(mesg);

        }

        appendException(exception, 0, buf, details);

        //MultilineLabel message = new MultilineLabel(buf.toString());

        javax.swing.JTextArea message = new javax.swing.JTextArea(buf.toString());
        message.setEditable(false);
        message.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
        javax.swing.JLabel jl = new javax.swing.JLabel();
        message.setFont(jl.getFont());
        message.setBackground(jl.getBackground());

        int opt = JOptionPane.showOptionDialog(parent, message, title,

                JOptionPane.OK_CANCEL_OPTION,

                JOptionPane.ERROR_MESSAGE,

                null, opts, opts[1]);

        if (opt == 0) {

            details = !details;

        }

        else {

            break;

        }

    }

}

From source file:Main.java

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    if (c instanceof JLabel) {
        JLabel label = (JLabel) c;
        editorPane.setText(label.getText());
        editorPane.setToolTipText(label.getToolTipText());
        editorPane.setOpaque(label.isOpaque());
        editorPane.setBackground(label.getBackground());
        editorPane.setBorder(label.getBorder());
    }//from  w  w w  .j a  va2 s .c  om
    return editorPane;
}

From source file:com.limegroup.gnutella.gui.LicenseWindow.java

/**
 * Builds a new JTextArea with the appropriate values set.
 *//*ww  w. j a  v a2  s.  co  m*/
private JTextArea newTextArea(String msg) {
    JTextArea text = new JTextArea();
    text.setLineWrap(true);
    text.setWrapStyleWord(true);
    text.setEditable(false);
    text.setFont(UIManager.getFont("Table.font"));
    JLabel label = new JLabel();
    text.setForeground(label.getForeground());
    text.setBackground(label.getBackground());
    text.setText(msg);
    return text;
}

From source file:com.sshtools.common.ui.SshToolsApplication.java

public void openChangelog(Component parent) {

    JPanel p = new JPanel(new GridBagLayout());
    p.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

    GridBagConstraints gBC = new GridBagConstraints();
    gBC.anchor = GridBagConstraints.CENTER;
    gBC.fill = GridBagConstraints.HORIZONTAL;
    gBC.insets = new Insets(1, 1, 1, 1);

    JLabel a = new JLabel(getApplicationName());
    a.setFont(a.getFont().deriveFont(24f));
    UIUtil.jGridBagAdd(p, a, gBC, GridBagConstraints.REMAINDER);

    String changelog = "";
    try {/*from  www  . j a v a2 s.  co m*/
        BufferedReader br = new BufferedReader(
                new InputStreamReader(getClass().getResourceAsStream("/changelog")));
        String line = br.readLine();
        while (line != null) {
            changelog += line + "\n";
            line = br.readLine();
        }
        br.close();
    } catch (Exception e) {
        changelog = "<Error opening changelog>\n";
    }

    javax.swing.JTextArea message = new javax.swing.JTextArea(changelog);
    message.setEditable(false);
    message.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
    javax.swing.JLabel jl = new javax.swing.JLabel();
    message.setFont(jl.getFont());
    message.setBackground(jl.getBackground());

    //   MultilineLabel x = new MultilineLabel(changelog);
    //   x.setBorder(BorderFactory.createEmptyBorder(8, 0, 8, 0));
    //   x.setFont(x.getFont().deriveFont(12f));   
    javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane();
    scrollPane.getViewport().add(message);
    scrollPane.setSize(400, 200);
    scrollPane.setPreferredSize(new java.awt.Dimension(400, 200));
    UIUtil.jGridBagAdd(p, scrollPane, gBC, GridBagConstraints.REMAINDER);

    JOptionPane.showMessageDialog(parent, p, "Change log", JOptionPane.PLAIN_MESSAGE,
            getApplicationLargeIcon());
}

From source file:org.cds06.speleograph.graph.GraphEditor.java

/**
 * Creates a modal dialog by specifying the attached {@link GraphPanel}.
 * <p>Please look at {@link javax.swing.JDialog#JDialog()} to see defaults params applied to this Dialog.</p>
 *//*from   w  ww.j a v a 2  s.c  om*/
public GraphEditor(GraphPanel panel) {
    super((Frame) SwingUtilities.windowForComponent(panel), true);
    Validate.notNull(panel);
    this.graphPanel = panel;

    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    mainPanel.registerKeyboardAction(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    setContentPane(mainPanel);
    mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.setTitle(I18nSupport.translate("menus.graph.graphEditor"));

    {
        // This section use FormLayout which is an external layout for Java, consult the doc before edit the
        // following lines !
        final FormLayout layout = new FormLayout(
                "right:max(40dlu;p), 4dlu, p:grow, 4dlu, p, 4dlu, p:grow, 4dlu, p", //NON-NLS
                "p,4dlu,p,4dlu,p,4dlu,p,4dlu,p" //NON-NLS
        );

        PanelBuilder builder = new PanelBuilder(layout);

        final JLabel colorLabel = new JLabel();
        final JTextField titleForGraph = new JTextField(
                graphPanel.getChart().getTitle() != null ? graphPanel.getChart().getTitle().getText() : "");
        final JLabel colorXYPlotLabel = new JLabel();
        final JLabel colorGridLabel = new JLabel();
        final JCheckBox showLegendCheckBox = new JCheckBox("Afficher la lgende",
                graphPanel.getChart().getLegend().isVisible());

        {
            builder.addLabel("Titre :", "1,1");
            builder.add(titleForGraph, "3,1,7,1");
        }

        {
            builder.addLabel(I18nSupport.translate("menus.graph.graphEditor.backgroundColor"), "1,3");
            colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            colorLabel.setText(" ");
            colorLabel.setBackground((Color) graphPanel.getChart().getBackgroundPaint());
            colorLabel.setOpaque(true);
            colorLabel.setEnabled(false);
            final JButton edit = new JButton(I18nSupport.translate("menus.graph.graphEditor.edit"));
            edit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color c = JColorChooser.showDialog(GraphEditor.this,
                            I18nSupport.translate("menus.graph.graphEditor.selectColor"),
                            colorLabel.getBackground());
                    if (c != null) {
                        colorLabel.setBackground(c);
                    }
                }
            });
            builder.add(colorLabel, "3,3,5,1");
            builder.add(edit, "9,3");
        }

        final XYPlot xyPlot = graphPanel.getChart().getXYPlot();
        {
            builder.addLabel(I18nSupport.translate("menus.graph.graphEditor.graphColor"), "1,5");
            colorXYPlotLabel.setText(" ");
            colorXYPlotLabel.setOpaque(true);
            colorXYPlotLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            colorXYPlotLabel.setBackground((Color) xyPlot.getBackgroundPaint());
            colorXYPlotLabel.setEnabled(false);
            final JButton edit = new JButton(I18nSupport.translate("menus.graph.graphEditor.edit"));
            edit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color c = JColorChooser.showDialog(GraphEditor.this,
                            I18nSupport.translate("menus.graph.graphEditor.selectColor"),
                            colorXYPlotLabel.getBackground());
                    if (c != null) {
                        colorXYPlotLabel.setBackground(c);
                    }
                }
            });
            builder.add(colorXYPlotLabel, "3,5,5,1");
            builder.add(edit, "9,5");
        }

        {
            builder.addLabel(I18nSupport.translate("menus.graph.graphEditor.gridColor"), "1,7");
            colorGridLabel.setOpaque(true);
            colorGridLabel.setText(" ");
            colorGridLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            colorGridLabel.setBackground((Color) xyPlot.getRangeGridlinePaint());
            colorGridLabel.setEnabled(false);
            final JButton edit = new JButton(I18nSupport.translate("menus.graph.graphEditor.edit"));
            edit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color c = JColorChooser.showDialog(GraphEditor.this,
                            I18nSupport.translate("menus.graph.graphEditor.selectColor"),
                            colorGridLabel.getBackground());
                    if (c != null) {
                        colorGridLabel.setBackground(c);
                    }
                }
            });
            builder.add(colorGridLabel, "3,7,5,1");
            builder.add(edit, "9,7");
        }

        {
            builder.add(showLegendCheckBox, "1,9,9,1");
        }

        mainPanel.add(builder.build(), BorderLayout.CENTER);

        ButtonBarBuilder buttonBarBuilder = new ButtonBarBuilder();

        buttonBarBuilder.addGlue();

        buttonBarBuilder.addButton(new AbstractAction() {
            {
                putValue(NAME, I18nSupport.translate("cancel"));
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                GraphEditor.this.setVisible(false);
            }
        });

        buttonBarBuilder.addButton(new AbstractAction() {

            {
                putValue(NAME, I18nSupport.translate("ok"));
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                if (titleForGraph.getText().isEmpty())
                    graphPanel.getChart().setTitle((String) null);
                else
                    graphPanel.getChart().setTitle(titleForGraph.getText());
                {
                    Color c = colorGridLabel.getBackground();
                    xyPlot.setRangeGridlinePaint(c);
                    xyPlot.setRangeMinorGridlinePaint(c);
                    xyPlot.setDomainGridlinePaint(c);
                    xyPlot.setDomainMinorGridlinePaint(c);
                }
                graphPanel.getChart().setBackgroundPaint(colorLabel.getBackground());
                xyPlot.setBackgroundPaint(colorXYPlotLabel.getBackground());
                graphPanel.getChart().getLegend().setVisible(showLegendCheckBox.isSelected());
                GraphEditor.this.setVisible(false);
            }
        });

        mainPanel.add(buttonBarBuilder.build(), BorderLayout.SOUTH);
    }

    pack();
    Dimension d = this.getPreferredSize();
    this.setSize(new Dimension(d.width + 20, d.height));
    setResizable(false);
}

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

/**
 * //from w  ww  .j  a v  a2  s  .  c  o m
 * <i>Description:</i> simple method that creates single/multi line label
 * with specified label width based on a source label. For more advanced
 * label attributes, it's the developer's responsibility to set them back.
 * 
 * @param source
 * @param wrap
 * @param maxWidth
 * @return
 */
public static JLabel deriveLabel(JLabel source, final boolean wrap, final int maxWidth) {
    JXLabel label = new JXLabel(source.getText(), source.getIcon(), source.getHorizontalAlignment()) {
        private static final long serialVersionUID = -4816144910055350011L;

        private Font cachedFont;

        private String chahedRawText, chahedText;

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.JLabel#getText()
         */
        @Override
        public String getText() {
            String text = super.getText();
            if (wrap || maxWidth <= 0 || text == null || text.isEmpty()) {
                return text;
            }

            if (getFont().equals(cachedFont) && text.equals(chahedRawText)) {
                return chahedText;
            }

            chahedRawText = text;
            cachedFont = getFont();
            FontMetrics fm = getFontMetrics(cachedFont);
            char[] chars = text.toCharArray();
            int width = fm.charsWidth(chars, 0, chars.length);
            if (width < maxWidth) {
                chahedText = text;
            } else {
                width += fm.charWidth('.') * 3;
                int pos = chars.length - 1;
                for (; pos >= 0 && width > maxWidth; pos--) {
                    width -= fm.charWidth(chars[pos]);
                }
                chahedText = new String(chars, 0, pos) + "...";
                if (getToolTipText() == null) {
                    setToolTipText(text);
                }
            }

            return chahedText;
        }

    };
    if (wrap) {
        label.setLineWrap(true);
    }
    if (maxWidth > 0) {
        label.setMaxLineSpan(maxWidth);
    }
    label.setEnabled(source.isEnabled());
    label.setForeground(source.getForeground());
    label.setOpaque(source.isOpaque());
    label.setBackground(source.getBackground());
    label.setFont(source.getFont());
    label.setBorder(source.getBorder());
    label.setToolTipText(source.getToolTipText());
    return label;
}