Example usage for javax.swing JButton setText

List of usage examples for javax.swing JButton setText

Introduction

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

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The button's text.")
public void setText(String text) 

Source Link

Document

Sets the button's text.

Usage

From source file:Main.java

/**
 * Determine the maximum size for a 2-state button with the specified text and icons.
 * This can be used to make sure that a button doesn't resize during state change.
 *
 * @param button  the UI of this JButton is used for size determination
 * @param string1 text for 1st mode//from  w w  w. j a  v a 2  s  .c o  m
 * @param icon1   icon for 1st mode
 * @param string2 text for 2nd mode
 * @param icon2   icon for 2nd mode
 * @return the Dimension that contains both modes for the button.
 */
public static Dimension getMaxDimension(JButton button, String string1, ImageIcon icon1, String string2,
        ImageIcon icon2) {

    String originalText = button.getText();
    Icon originalIcon = button.getIcon();

    // Get dimensions for "Play" state
    button.setText(string1);
    button.setIcon(icon1);
    Dimension playSize = button.getUI().getPreferredSize(button);

    // Get dimensions for "Pause" state
    button.setText(string2);
    button.setIcon(icon2);
    Dimension pauseSize = button.getUI().getPreferredSize(button);

    // Restore original text and icon
    button.setText(originalText);
    button.setIcon(originalIcon);

    // Return max dimensions
    int maxWidth = (int) Math.max(playSize.getWidth(), pauseSize.getWidth());
    int maxHeight = (int) Math.max(playSize.getHeight(), pauseSize.getHeight());
    return new Dimension(maxWidth, maxHeight);
}

From source file:Main.java

/**
 * Configures a button as if it was an hyperlink.
 * //from   www  .  jav a  2  s . c  o m
 * @param button
 *            the button to configure.
 */
public static void configureButtonAsHyperlink(JButton button) {
    if (button == null) {
        return;
    }

    StringBuffer html = new StringBuffer();
    html.append("<html><font color=\"blue\"><u>");
    html.append(button.getText());
    html.append("</u></font></html>");

    button.setText(html.toString());
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    button.setFocusPainted(false);
    button.setBorderPainted(false);
    button.setContentAreaFilled(false);
}

From source file:Main.java

/**
 * Returns a styled JButton.// w ww.j av a 2s . c  o  m
 *
 * @param style button type
 * @return a styled button
 */
public static JButton creaStyledButton(int style) {
    JButton jb = new JButton();
    jb.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    jb.setMargin(new Insets(0, 5, 1, 5));
    switch (style) {
    case STYLE_EDIT: {
        jb.setText("edit");
        jb.setToolTipText("edit");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_OK: {
        jb.setText("ok");
        jb.setToolTipText("confirm changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_CANCEL: {
        jb.setText("cancel");
        jb.setToolTipText("discard changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_INSBEFOREROW: {
        jb.setIcon(ICON_INSBEFOREROW);
        jb.setToolTipText("insert row before");
        jb.setName("INSBEFOREROW");
        return jb;
    }
    case STYLE_INSAFTERROW: {
        jb.setIcon(ICON_INSAFTERROW);
        jb.setToolTipText("insert row after");
        jb.setName("INSAFTERROW");
        return jb;
    }
    case STYLE_DELETEROW: {
        jb.setIcon(ICON_DELETEROW);
        jb.setToolTipText("delete row");
        jb.setName("DELETEROW");
        return jb;
    }
    case STYLE_CLONEBEFOREROW: {
        jb.setIcon(ICON_CLONEBEFOREROW);
        jb.setToolTipText("clone row before");
        jb.setName("CLONEBEFOREROW");
        return jb;
    }
    case STYLE_CLONEAFTERROW: {
        jb.setIcon(ICON_CLONEAFTERROW);
        jb.setToolTipText("clone row after");
        jb.setName("CLONEAFTERROW");
        return jb;
    }
    case STYLE_DEFAULTROWS: {
        jb.setIcon(ICON_DEFAULTROWS);
        jb.setToolTipText("set default values");
        jb.setName("DEFAULTROWS");
        return jb;
    }
    default: {
        return null;
    }
    }
}

From source file:com.joey.software.memoryToolkit.MemoryUsagePanel.java

public static JFrame getMemoryUsagePanel(int dataNum, int delay) {
    final MemoryUsagePanel u = new MemoryUsagePanel(dataNum, delay);
    JFrame f = new JFrame("Memory Usage");
    f.setLayout(new BorderLayout());
    f.getContentPane().add(u, BorderLayout.CENTER);
    f.setSize(800, 400);//from   w  w w. j  av a2 s .  c o m
    f.setVisible(true);

    final JButton runButton = new JButton("Start");
    runButton.addActionListener(new ActionListener() {

        boolean running = false;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (running) {
                running = false;
                u.stopUpdating();
                runButton.setText("Start");
            } else {
                running = true;
                u.startUpdating();
                runButton.setText("Stop");
            }
        }
    });

    JButton garbage = new JButton("Run GC");
    garbage.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.gc();
        }
    });

    JPanel temp = new JPanel(new GridLayout(1, 1));

    temp.add(runButton);
    temp.add(garbage);

    f.getContentPane().add(temp, BorderLayout.SOUTH);

    return f;
}

From source file:MyLookAndFeel.java

public Main() throws Exception {
    UIManager.setLookAndFeel("MyLookAndFeel");
    setLayout(new FlowLayout());

    JButton b = new JButton();
    b.setText("<html>A<br> B</html>");
    b.setToolTipText("<html>C<br>D<br>E</html>");

    add(b);//ww w .j  ava  2 s  . com

    JLabel l = new JLabel("Z");
    l.setToolTipText("zzzzz...");

    add(l);
}

From source file:Main.java

public Main() {
    this.setLayout(new GridLayout(0, 1));
    this.add(label);
    this.add(new JButton(new AbstractAction("OK") {

        @Override/*from   w  w w . jav  a2  s  . co m*/
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton) e.getSource();
            b.setText("123");
        }
    }));
    new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText(String.valueOf(System.nanoTime()));
        }
    }).start();
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolBar = new JToolBar();
    Action a = new AbstractAction("Demo") {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action taken.");
        }//from  w w  w.ja  va 2 s  .c o  m
    };

    JButton b = toolBar.add(a);
    b.setText("Demo Button");
    b.setToolTipText("Press me to take action.");

    JMenu mainMenu = new JMenu("Menu");
    JMenuItem mi = mainMenu.add(a);
    mi.getAccessibleContext().setAccessibleName("Menu item");

    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    setJMenuBar(mb);

    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.setPreferredSize(new Dimension(200, 100));
    pane.add(toolBar, BorderLayout.NORTH);
    setContentPane(pane);

    pack();
    setVisible(true);
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    JButton button = (JButton) evt.getSource();
    if (animate) {
        button.setText("Start Animation");
        animate = false;/*from w  ww . j a  v  a2s.  co m*/
    } else {
        animate = true;
        button.setText("Stop Animation");
        new Thread(this).start();
    }
}

From source file:com.github.kennycyb.uifactory.core.factory.impl.components.swing.JButtonFactory.java

@UiAnnotationHandler(JButtonProperties.class)
protected void handleJFrameProperties(final ComponentContext context, final JButton component,
        final JButtonProperties annotate) {

    component.setText(annotate.text());
}

From source file:net.sf.jabref.gui.help.HelpAction.java

public JButton getHelpButton() {
    JButton button = new JButton(this);
    button.setText(null);
    button.setPreferredSize(new Dimension(24, 24));
    button.setToolTipText(getValue(Action.SHORT_DESCRIPTION).toString());
    return button;
}