• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to call Action Listener Of a JButton

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends;
I am developing the java application by using netbeans. I hava a panel1 class with extends by JPanel and it have a Jbutton name check.
I have another class Main_Frame extends with JFRame.
I write a code in Main_Frame like this
panel1 p1= New panel1();

Now i want to add  an action listener of JButton check in Main_Frame how i do it?

Regards
 
Marshal
Posts: 67939
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope you mean you have an application which you are adding a GUI to. Don't write a GUI and make into an application. Make sure you have an application working, and then write a GUI to make it visible.

You add an action listener to a button like this. You can only use a λ because ActionListener is a functional interface.Make sure to write that method too.

Hamad Sultan wrote:. . . I hava a panel1 class with extends by JPanel . . .

What a dreadful name for a class. The class name should let you know what it means, and should start with a CapitalLetter.
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell and others Friends;
I just give an example for expert advise. I always Use the meaningful name of the class. Campbell thanks for your reply but i am still confused. Here i again explain my ambiguity.

I Have one class in which main interface develop like JFrame and JDesktopPane. This class name is Main_Frame.java and it have no main method to run. I have another class Master.java which have public static void main method and create of instance of Main_Frame.java to run the application.

Now i want to make a new class which extends with JPanel and it have JToolBar. The JButton adds in the JToolBar and tool bar add to JPanel. Its class name is MyPanle.Java

Now i want to add JButton ActionListner in the Main_Frame.java. I create an instance of MyPanel.java in Main_Frame.java and add panel in the JFrame. Button on the toolbar is also accessible but I am not successful to write a action Listeners in Main_Frame.java.

First is this approach is good?
Please give me an example to understand this?

Regards
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hamad Sultan wrote:. . . a new class which extends with JPanel and it have JToolBar.

The toolbar should be separate from the panel.

The JButton adds in the JToolBar . . .

You mean it opens the toolbar. Create a method to open that toolbar and call it from actionPerformed().

. . . add JButton ActionListner . . .

Add action listeners to buttons and text boxes, not to frames or panels.in the Main_Frame.java. I create an instance of MyPanel.java in Main_Frame.java and add panel in the JFrame. Button on the toolbar is also accessible but I am not successful to write a action Listeners in Main_Frame.java.

First is this approach is good?
Please give me an example to understand this?

Regards
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell;

Good day. I understand your meaning that you want to say that make a class which extends with JToolbar instead of JPanel. Then add buttons in that toolbar. Suppose JButton1 add in the toolbar.

So open the toolbar in the Main_Frame class like

MyToolBar tool=New MyToolBar();
too.add(this);
tool.setBounds(5,5,24,300);

Now action performed
tool.JButton1.addActionListener(what i write here?); am i right?

Please guide me with code. I am very thankful to you.

Regards
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hamad Sultan wrote:. . . you want to say that make a class which extends with JToolbar instead of JPanel. . . .

No, I didn't. There are only a few instances where you should extend a display component class. Use plain simple JToolbar and add the required buttons to it. Why do you want it to appear and disappear? The main difference between a toolbar and the menu bar is that you can usually drag the toolbar elsewhere. Have you read its documentation, or the Java™ Tutorials page?
 
Rancher
Posts: 4492
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't be extending JFrame and should only rarely extend JPanel.

Why does the top level GUI manager (in your case it's your Main_Frame) need to know when the button is pushed?
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends;

According to Campbell i insert JToolBar directly to the JFrame instaed of JPanel.
Now we have Three Toolbars. We have three Users when User1 Login The other Two Users Tool Bars must be Disable.
For Example
Optoolbar.setEnabled(false);

But its not working.......


How i fix it

Regards
 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at our FAQs; please explain more than saying it doesn't work, and please produce a small example of code showing the problem.
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell;

Good day. Here is the code


Regards

 
Campbell Ritchie
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid that code doesn't help us. It is too short and we can't compile it.
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell;

Here is the complete code


[/code]

Now I want to add a Action Listener of  LoginButton from LoginWindow.java into the Main_Frame.java. I mean I want to add Action Listener in the Main_Frame.java, The JButton name LoginButton in the LoginWindow.java

Regards
 
Hamad Sultan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello experts;

Please help me about add action listener.

Regards
 
Marshal
Posts: 25143
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hamad Sultan wrote:Now I want to add a Action Listener of  LoginButton from LoginWindow.java into the Main_Frame.java. I mean I want to add Action Listener in the Main_Frame.java, The JButton name LoginButton in the LoginWindow.java



It would be far better if you didn't do that. If you do, then you'll have logic spread all over the place. So instead, why not explain why you think you want to do it?
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!