![]() ![]() ActionListener removeActionListener(): Removes the objects that listens to the action events generated by the button.void addActionListener(ActionListener a): Adds the objects that listens to the action events generated by the button.To get an icon from a pressed or selected button use Icon getPressedIcon(). Icon getIcon(): Gets the icon displayed on the button when it is not pressed or selected.To set an icon on a pressed button use void setPressedIcon(Icon i) void setIcon(Icon i): Sets the icon or image displayed by the button when it is not pressed.Action getAction(): Gets the button properties with respect to action presented in it.void setAction(Action a): Sets the button properties according to the action provided.String getText(): Gets the text on the button.void setText(String s): Sets the provided text to a button created.JButton(String s, Icon i): Creates and instantiates a button with initial text as a string and an icon provided respectively.JButton(Icon i): Creates and instantiates a button with an icon i as provided.JButton(String s): Creates and instantiates a button with text with string s as provided.JButton(Event e): Creates and instantiates a button with the event supplied and properties are also taken from action supplied.JButton(): Creates and instantiates a button having no set text or icon.Here we will discuss the constructors and methods of java swing button: ConstructorsĬonstructors are used for instantiating objects for JButton class: TactButton.Web development, programming languages, Software testing & others class JButton extends AbstractButton implements Accessible Constructors and Methods of Java Swing Button tIcon(new ImageIcon(("/images/button_next.jpg"))) I looked at the question How do I add an image to a JButton and tried to do it like there.įull code of creating button and set icon: tactButton = new JButton("next tact") trying something like tIcon(new ImageIcon("/images/button_next.jpg")) īut I have this exception or if I use tIcon(new ImageIcon("/images/button_next.jpg")) I have simple button without image.įinally I wrote absolute path to my image and this works fine (but absolute path is not good idea).put all pictures in the same folder as MainWindow class.My code looks like this: tIcon(new ImageIcon(getClass().getResource("/images/button_next.jpg"))) īut when I start app I have exception: Īt .(ImageIcon.java:205)Īt by.(MainWindow.java:117)Īt by.$1.run(MainWindow.java:46)Īt .dispatch(InvocationEvent.java:251)Īt (EventQueue.java:721)Īt $200(EventQueue.java:103)Īt $3.run(EventQueue.java:682)Īt $3.run(EventQueue.java:680)Īt (Native Method)Īt $1.doIntersectionPrivilege(ProtectionDomain.java:76)Īt (EventQueue.java:691)Īt (EventDispatchThread.java:244)Īt (EventDispatchThread.java:163)Īt (EventDispatchThread.java:151)Īt (EventDispatchThread.java:147)Īt (EventDispatchThread.java:139)Īt (EventDispatchThread.java:97) It looks like this: tactButton = new JButton("next tact") Īnd I want to set image to this button using method setIcon. So I have question how to set icon to JButton.Īnd I have simple JButton in MainWindow class: ![]() I'm not very good in creating Swing application. ![]()
0 Comments
Leave a Reply. |