Java JButton getText

Java JButton getText

My previous post I described how to get and set JButton icons.Today am going to demonstrate how to get JButton Text.

JButton getText Example

[
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
public class JavaJButton {

    public static void main(String args[])
    {
        try        {
            UIManager.setLookAndFeel
                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {
        }
        JFrame frame=new JFrame("Jbutton Example");
        JButton button=new JButton("Save");


        JLabel field=new JLabel();
        field.setBounds(50,50, 150,20);
        frame.add(field);
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                String text=button.getText();
                field.setText("Button Name is="+text);

            }


        });


        button.setBounds(100,100,95,30);
        frame.add(button);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);



    }

}
]

JButton getText Code Explanation:

[ button.addActionListener(new ActionListener())]-used to add action to the button when clicked.
[ frame.setVisible(true)]-makes the frame visible.
[ frame.add(button)]-adds button to the frame.
[String text=button.getText();
field.setText("Button Name is="+text);]-gets String in the button and sets the text in a JLabel.

[ UIManager.setLookAndFeel();]-used to set application LookAndFeel

See also:

 JButton tooltip code example



COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JButton getText
Java JButton getText
Java JButton getText
https://4.bp.blogspot.com/-4-Sy7dh_Op8/WLhhnxTPxAI/AAAAAAAAG-Q/aCnOG-rxBhMrUvoEzmPAHlLKOQUB_JKzQCLcB/s200/JButton-getText-method-min.PNG
https://4.bp.blogspot.com/-4-Sy7dh_Op8/WLhhnxTPxAI/AAAAAAAAG-Q/aCnOG-rxBhMrUvoEzmPAHlLKOQUB_JKzQCLcB/s72-c/JButton-getText-method-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jbutton-gettext.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jbutton-gettext.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy