Example usage for javax.swing JButton setIcon

List of usage examples for javax.swing JButton setIcon

Introduction

In this page you can find the example usage for javax.swing JButton setIcon.

Prototype

@BeanProperty(visualUpdate = true, description = "The button's default icon")
public void setIcon(Icon defaultIcon) 

Source Link

Document

Sets the button's default icon.

Usage

From source file:storybook.ui.dialog.ExceptionDialog.java

@Override
public void initUi() {
    setLayout(new MigLayout("wrap,fill", "", ""));
    setTitle("Exception");
    setIconImage(I18N.getIconImage("icon.small.error"));
    setPreferredSize(new Dimension(700, 500));

    ta = new JTextArea();
    ta.setEditable(false);/*w  w w. j  a  v  a  2s.  c  o  m*/
    StringBuilder buf = new StringBuilder();
    buf.append("Exception Message:\n");
    buf.append(e.getLocalizedMessage());
    buf.append("\n\nStack Trace:\n");
    buf.append(ExceptionUtils.getStackTrace(e));
    ta.setText(buf.toString());
    JScrollPane scroller = new JScrollPane(ta);
    SwingUtil.setMaxPreferredSize(scroller);

    // copy text
    JButton btCopyText = new JButton();
    btCopyText.setAction(getCopyTextAction());
    btCopyText.setText(I18N.getMsg("msg.file.info.copy.text"));
    btCopyText.setIcon(I18N.getIcon("icon.small.copy"));

    // layout
    add(scroller, "grow");
    add(btCopyText, "split 2,sg");
    add(getCloseButton(), "sg");

    ViewUtil.scrollToTop(scroller);
}

From source file:thesaurusEditor.gui.graph.MainGraph.java

/**
 * create an instance of a simple graph with basic controls
 *//*from  ww  w.j a  v a  2  s .c o  m*/
public MainGraph(List<Konzept> konzepte, Main main) {

    // create a simple graph for the demo
    this.konzepte = konzepte;
    this.main = main;

    main.getController().getThesaurus().addObserver(this);

    graph = new DirectedSparseGraph<Konzept, EdgeClass>();

    for (Konzept k : konzepte) {
        graph.addVertex(k);
    }
    createEdges(konzepte);

    layout = new PersistentLayoutImpl<Konzept, EdgeClass>(new FRLayout<Konzept, EdgeClass>(graph));
    //layout = new FRLayout<Konzept,EdgeClass>(graph);
    Dimension preferredSize = new Dimension(1300, 900);
    final VisualizationModel<Konzept, EdgeClass> visualizationModel = new DefaultVisualizationModel<Konzept, EdgeClass>(
            layout, preferredSize);
    vv = new VisualizationViewer<Konzept, EdgeClass>(visualizationModel, preferredSize);

    // this class will provide both label drawing and vertex shapes
    VertexLabelAsShapeRenderer<Konzept, EdgeClass> vlasr = new VertexLabelAsShapeRenderer<Konzept, EdgeClass>(
            vv.getRenderContext());

    // customize the render context
    vv.getRenderContext().setVertexLabelTransformer(new Transformer<Konzept, String>() {
        public String transform(Konzept k) {
            return "";
        }
    });
    // this chains together Transformers so that the html tags
    // are prepended to the toString method output
    /*new ChainedTransformer<Konzept,String>(new Transformer[]{
    new ToStringLabeller<Konzept>(),
    new Transformer<Konzept,String>() {
     public String transform(Konzept input) {
        return input.toString();
     }}}));*/
    vv.getRenderContext().setVertexShapeTransformer(new Transformer<Konzept, Shape>() {
        public Shape transform(Konzept k) {
            return new Rectangle(-((k.toString().length() * 8 + 10) / 2), -(10), k.toString().length() * 7 + 18,
                    21);
        }
    });
    vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.red));
    vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.black));
    vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));

    vv.getRenderContext().setVertexFillPaintTransformer(
            new PickableVertexPaintTransformer<Konzept>(vv.getPickedVertexState(), Color.white, Color.yellow));
    vv.getRenderContext().setEdgeDrawPaintTransformer(
            new PickableEdgePaintTransformer<EdgeClass>(vv.getPickedEdgeState(), Color.black, Color.red));

    vv.getRenderContext().setVertexIconTransformer(new Transformer<Konzept, Icon>() {

        /*
         * Implements the Icon interface to draw an Icon with background color and
         * a text label
         */
        public Icon transform(final Konzept v) {
            return new Icon() {
                private Konzept k;

                public int getIconHeight() {
                    return 20;
                }

                public int getIconWidth() {
                    if (k != null) {
                        return k.toString().length() * 7 + 10;
                    }
                    return v.toString().length() * 7 + 10;
                }

                public void paintIcon(Component c, Graphics g, int x, int y) {
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.yellow);
                    } else {
                        g.setColor(Color.lightGray);
                    }
                    g.fillRect(x, y, v.toString().length() * 8 + 10, 20);

                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.black);
                    } else {
                        g.setColor(Color.black);
                    }
                    g.drawRect(x, y, v.toString().length() * 8 + 10, 20);
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.black);
                    } else {
                        g.setColor(Color.black);
                    }
                    this.k = v;

                    if (vv.getPickedVertexState().isPicked(v)) {
                        Font font = new Font("DejaVu Sans Mono", Font.BOLD, 14);
                        g.setFont(font);
                        g.drawString("" + v, x + 6, y + 15);
                    } else {
                        Font font = new Font("DejaVu Sans Mono", Font.PLAIN, 14);
                        g.setFont(font);
                        g.drawString("" + v, x + 6, y + 15);
                    }
                    this.k = v;

                }
            };
        }
    });

    // customize the renderer
    /*GradientVertexRenderer<Konzept,EdgeClass> vertex = new GradientVertexRenderer<Konzept,EdgeClass>(Color.white, Color.white, true);
    vv.getRenderer().setVertexRenderer(vertex);
    vv.getRenderer().setVertexLabelRenderer(vlasr); */

    vv.setBackground(Color.white);

    // add a listener for ToolTips
    KonzeptParser<Konzept> parser = new KonzeptParser<Konzept>();
    vv.setVertexToolTipTransformer(parser);

    /*final DefaultModalGraphMouse<Konzept,Edge> graphMouse = 
    new DefaultModalGraphMouse<Konzept,Edge>();
    */

    Factory<Konzept> vertexFactory = new VertexFactory();
    Factory<EdgeClass> edgeFactory = new EdgeFactory();

    graphMouse = new MyModalGraphMouse<Konzept, EdgeClass>(vv.getRenderContext(), vertexFactory, edgeFactory,
            main, vv);

    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());
    // the EditingGraphMouse will pass mouse event coordinates to the
    // vertexLocations function to set the locations of the vertices as
    // they are created
    //        graphMouse.setVertexLocations(vertexLocations);
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    graphMouse.setMode(ModalGraphMouse.Mode.EDITING);

    GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);

    JComboBox modeBox = graphMouse.getModeComboBox();
    modeBox.addItemListener(graphMouse.getModeListener());
    //graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton();
    plus.setIcon(new ImageIcon(getClass().getResource("/thesaurusEditor/img/zoom_in.png")));
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton();
    minus.setIcon(new ImageIcon(getClass().getResource("/thesaurusEditor/img/zoom_out.png")));
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    JPanel zoomControls = new JPanel(new GridLayout(1, 2));
    //zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
    zoomControls.add(plus);
    zoomControls.add(minus);
    controls.add(zoomControls);
    //controls.add(modeBox);
    /*
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(gzsp);
    gzsp.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 374, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 106, Short.MAX_VALUE)
    );
            
            
    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(controls);
    controls.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 374, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 164, Short.MAX_VALUE)
    );*/

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(gzsp, javax.swing.GroupLayout.Alignment.TRAILING,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                    Short.MAX_VALUE)
                            .addComponent(controls, javax.swing.GroupLayout.Alignment.TRAILING,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                    Short.MAX_VALUE))
                    .addContainerGap()));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup().addContainerGap()
                    .addComponent(gzsp, javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(controls, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()));

}

From source file:uk.ac.soton.mib104.t2.workbench.ui.ActivityConfigurationPanelWithInputPortAndOutputPortComponents.java

/**
 * Initializes the graphical user interface (GUI).
 * <p>/*  w w w  .ja  v  a2s  . c  o m*/
 * Subclasses may refine the implementation of this method. 
 */
protected void initGui() {
    this.setDoubleBuffered(false);
    this.setLayout(new BorderLayout());

    tabbedPane.setBorder(BorderFactory.createEmptyBorder());

    tabbedPane.addTab(defaultInputPortsTabText, defaultInputPortsTabIcon, inputPortsPane,
            defaultInputPortsTabTip);
    tabbedPane.addTab(defaultOutputPortsTabText, defaultOutputPortsTabIcon, outputPortsPane,
            defaultOutputPortsTabTip);

    this.add(tabbedPane, BorderLayout.CENTER);

    inputPortsPane.setBorder(BorderFactory.createEmptyBorder());
    inputPortsPane.setLayout(new BorderLayout());

    emptyInputPortsLabel.setHorizontalAlignment(JLabel.CENTER);
    emptyInputPortsLabel.setText(defaultEmptyInputPortsText);

    final JButton addInputPortButton = new JButton();
    addInputPortButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            final String portName = nextPortName(defaultInputPortName,
                    ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this.getInputPortComponents()
                            .keySet());

            final IN_CONFIG inputPortConfigBean = ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this
                    .newInputPortConfiguration();

            final IN_COMPONENT inputPortComponent = ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this
                    .toInputPortComponent(portName, inputPortConfigBean);

            inputPortsTabbedPane.addTab(portName, inputPortComponent);
        }

    });
    addInputPortButton.setIcon(defaultAddInputPortButtonIcon);
    addInputPortButton.setText(defaultAddInputPortButtonText);
    addInputPortButton.setToolTipText(defaultAddInputPortButtonTip);

    inputPortsPane.add(emptyInputPortsLabel, BorderLayout.CENTER);
    inputPortsPane.add(addInputPortButton, BorderLayout.SOUTH);

    outputPortsPane.setBorder(BorderFactory.createEmptyBorder());
    outputPortsPane.setLayout(new BorderLayout());

    emptyOutputPortsLabel.setHorizontalAlignment(JLabel.CENTER);
    emptyOutputPortsLabel.setText(defaultEmptyOutputPortsText);

    final JButton addOutputPortButton = new JButton();
    addOutputPortButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            final String portName = nextPortName(defaultOutputPortName,
                    ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this
                            .getOutputPortComponents().keySet());

            final OUT_CONFIG outputPortConfigBean = ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this
                    .newOutputPortConfiguration();

            final OUT_COMPONENT outputPortComponent = ActivityConfigurationPanelWithInputPortAndOutputPortComponents.this
                    .toOutputPortComponent(portName, outputPortConfigBean);

            outputPortsTabbedPane.addTab(portName, outputPortComponent);
        }

    });
    addOutputPortButton.setIcon(defaultAddOutputPortButtonIcon);
    addOutputPortButton.setText(defaultAddOutputPortButtonText);
    addOutputPortButton.setToolTipText(defaultAddOutputPortButtonTip);

    outputPortsPane.add(emptyOutputPortsLabel, BorderLayout.CENTER);
    outputPortsPane.add(addOutputPortButton, BorderLayout.SOUTH);
}

From source file:utybo.branchingstorytree.swing.OpenBSTGUI.java

public OpenBSTGUI() {
    instance = this;
    UIManager.put("OptionPane.errorIcon", new ImageIcon(Icons.getImage("Cancel", 48)));
    UIManager.put("OptionPane.informationIcon", new ImageIcon(Icons.getImage("About", 48)));
    UIManager.put("OptionPane.questionIcon", new ImageIcon(Icons.getImage("Rename", 48)));
    UIManager.put("OptionPane.warningIcon", new ImageIcon(Icons.getImage("Error", 48)));

    BorderLayout borderLayout = new BorderLayout();
    borderLayout.setVgap(4);/*from   w w  w.j ava  2 s  . co m*/
    getContentPane().setLayout(borderLayout);
    setIconImage(Icons.getImage("Logo", 48));
    setTitle("OpenBST " + OpenBST.VERSION);
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            boolean cancelled = false;
            int i = 0;
            for (Component c : container.getComponents()) {
                if (c instanceof StoryPanel) {
                    i++;
                } else if (c instanceof StoryEditor) {
                    container.setSelectedComponent(c);
                    if (((StoryEditor) c).askClose()) {
                        continue;
                    } else {
                        cancelled = true;
                        break;
                    }
                }
            }
            if (!cancelled) {
                if (i > 0) {
                    int j = Messagers.showConfirm(OpenBSTGUI.this,
                            "You are about to close " + i + " file(s). Are you sure you wish to exit OpenBST?",
                            Messagers.OPTIONS_YES_NO, Messagers.TYPE_WARNING, "Closing OpenBST");
                    if (j != Messagers.OPTION_YES)
                        cancelled = true;
                }
                if (!cancelled)
                    System.exit(0);
            }
        }

    });

    JMenuBar jmb = new JMenuBar();
    jmb.setBackground(OPENBST_BLUE);
    jmb.add(Box.createHorizontalGlue());
    jmb.add(createShortMenu());
    jmb.add(Box.createHorizontalGlue());
    this.setJMenuBar(jmb);

    addDarkModeCallback(b -> {
        jmb.setBackground(b ? OPENBST_BLUE.darker().darker() : OPENBST_BLUE);
    });

    container = new JTabbedPane();
    container.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    container.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(final MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                final int i = container.indexAtLocation(e.getX(), e.getY());
                System.out.println(i);
                if (i > -1) {
                    Component c = container.getComponentAt(i);
                    if (c instanceof StoryPanel) {
                        container.setSelectedComponent(c);
                        ((StoryPanel) c).askClose();
                    } else if (c instanceof StoryEditor) {
                        container.setSelectedComponent(c);
                        ((StoryEditor) c).askClose();
                    }
                }
            }
        }
    });
    getContentPane().add(container, BorderLayout.CENTER);

    final JBackgroundPanel welcomeContentPanel = new JBackgroundPanel(Icons.getRandomBackground(),
            Image.SCALE_FAST);
    background = welcomeContentPanel;

    welcomeContentPanel.setLayout(new MigLayout("hidemode 2", "[grow,center]", "[][grow][]"));
    container.add(welcomeContentPanel);
    container.setTitleAt(0, Lang.get("welcome"));

    bannersPanel = new JPanel(new MigLayout("hidemode 2, gap 0px, fill, wrap 1, ins 0"));
    bannersPanel.setBackground(new Color(0, 0, 0, 0));
    welcomeContentPanel.add(bannersPanel, "cell 0 0,grow");

    if (OpenBST.VERSION.endsWith("u")) {
        JButton btnReportBugs = new JButton(Lang.get("welcome.reportbugs"));
        btnReportBugs.addActionListener(e -> {
            VisualsUtils.browse("https://github.com/utybo/BST/issues");
        });
        bannersPanel.add(new JBannerPanel(new ImageIcon(Icons.getImage("Experiment", 32)), Color.YELLOW,
                Lang.get("welcome.ontheedge"), btnReportBugs, false), "grow");
    } else if (OpenBST.VERSION.contains("SNAPSHOT")) {
        bannersPanel.add(new JBannerPanel(new ImageIcon(Icons.getImage("Experiment", 32)), Color.ORANGE,
                Lang.get("welcome.snapshot"), null, false), "grow");
    }

    if (System.getProperty("java.specification.version").equals("9")) {
        bannersPanel.add(new JBannerPanel(new ImageIcon(Icons.getImage("Attention", 32)),
                new Color(255, 50, 50), Lang.get("welcome.java9warning"), null, false), "grow");
    }
    if (System.getProperty("java.specification.version").equals("10")) {
        bannersPanel.add(new JBannerPanel(new ImageIcon(Icons.getImage("Attention", 32)),
                new Color(255, 50, 50), Lang.get("welcome.java10warning"), null, false), "grow");
    }

    JButton btnJoinDiscord = new JButton(Lang.get("openbst.discordjoin"));
    btnJoinDiscord.addActionListener(e -> {
        VisualsUtils.browse("https://discord.gg/6SVDCMM");
    });
    bannersPanel.add(new JBannerPanel(new ImageIcon(Icons.getImage("Discord", 48)), DISCORD_COLOR,
            Lang.get("openbst.discord"), btnJoinDiscord, true), "grow");

    JPanel panel = new JPanel();
    panel.setBackground(new Color(0, 0, 0, 0));
    welcomeContentPanel.add(panel, "flowx,cell 0 1,growx,aligny center");
    panel.setLayout(new MigLayout("", "[40%][][][][60%,growprio 50]", "[][grow]"));

    final JLabel lblOpenbst = new JLabel(new ImageIcon(Icons.getImage("FullLogo", 48)));
    addDarkModeCallback(b -> lblOpenbst
            .setIcon(new ImageIcon(b ? Icons.getImage("FullLogoWhite", 48) : Icons.getImage("FullLogo", 48))));
    panel.add(lblOpenbst, "flowx,cell 0 0 1 2,alignx trailing,aligny center");

    JSeparator separator = new JSeparator();
    separator.setOrientation(SwingConstants.VERTICAL);
    panel.add(separator, "cell 2 0 1 2,growy");

    final JLabel lblWelcomeToOpenbst = new JLabel("<html>" + Lang.get("welcome.intro"));
    lblWelcomeToOpenbst.setMaximumSize(new Dimension(350, 999999));
    panel.add(lblWelcomeToOpenbst, "cell 4 0");

    Component horizontalStrut = Box.createHorizontalStrut(10);
    panel.add(horizontalStrut, "cell 1 1");

    Component horizontalStrut_1 = Box.createHorizontalStrut(10);
    panel.add(horizontalStrut_1, "cell 3 1");

    final JButton btnOpenAFile = new JButton(Lang.get("welcome.open"));
    panel.add(btnOpenAFile, "flowx,cell 4 1");
    btnOpenAFile.setIcon(new ImageIcon(Icons.getImage("Open", 40)));
    btnOpenAFile.addActionListener(e -> {
        openStory(VisualsUtils.askForFile(this, Lang.get("file.title")));
    });

    final JButton btnOpenEditor = new JButton(Lang.get("welcome.openeditor"));
    panel.add(btnOpenEditor, "cell 4 1");
    btnOpenEditor.setIcon(new ImageIcon(Icons.getImage("Edit Property", 40)));
    btnOpenEditor.addActionListener(e -> {
        openEditor(VisualsUtils.askForFile(this, Lang.get("file.title")));
    });

    JButton btnChangeBackground = new JButton(Lang.get("welcome.changebackground"),
            new ImageIcon(Icons.getImage("Change Theme", 16)));
    btnChangeBackground.addActionListener(e -> {
        BufferedImage prev = background.getImage();
        BufferedImage next;
        do {
            next = Icons.getRandomBackground();
        } while (prev == next);
        background.setImage(next);
    });
    welcomeContentPanel.add(btnChangeBackground, "flowx,cell 0 2,alignx left");

    JButton btnWelcomepixabay = new JButton(Lang.get("welcome.pixabay"),
            new ImageIcon(Icons.getImage("External Link", 16)));
    btnWelcomepixabay.addActionListener(e -> {
        VisualsUtils.browse("https://pixabay.com");

    });
    welcomeContentPanel.add(btnWelcomepixabay, "cell 0 2");

    JLabel creds = new JLabel(Lang.get("welcome.credits"));
    creds.setEnabled(false);
    welcomeContentPanel.add(creds, "cell 0 2, gapbefore 10px");

    setSize((int) (830 * Icons.getScale()), (int) (480 * Icons.getScale()));
    setLocationRelativeTo(null);
}