![]() Setting a JButton as the default buttonA window can have a default button whose action will be invoked when the user hits Enter key. Adding an event listener using anonymous class (shortcut way):īutton.addActionListener(new ActionListener() void actionPerformed(ActionEvent evt) ) 5.Adding a JButton to a container with a specific layout manager:.A JButton is usually added to a JPanel, a JFrame, a JDialog or a JApplet:.JButton button = new JButton("Start", new ImageIcon("images/start.gif")) Create a button with a caption and an icon:.JButton button = new JButton(icon) Here the icon file stop.jpg is placed under a specific package in the classpath. Icon icon = new ImageIcon(getClass().getResource(iconPath)) String iconPath = "/net/codejava/swing/jbutton/stop.jpg" Create a button with only icon inside a jar file or in classpath:.JButton button = new JButton(new ImageIcon("images/start.gif")) Here the icon file start.gif is placed under images directory which is relative to the program. Create a button with only an icon in the file system:.Create a default button with a caption:.Setting a JButton as the default button.Setting mnemonic and hotkey for JButton.This article summarizes common programming practices for using JButton in Swing. In this Java Swing tutorial, you will learn how to use button that allows the user to perform action in a desktop application.You know, JButton is a fundamental Swing component that renders a button on screen and responds to user’s clicking event for performing a specific task. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |