How to add unimplemented methods in BlueJ - java

Like the title suggests I wanna know how I can add unimplemented methods in BlueJ.
I was working with WindowListener and it kept on saying about the unimplemented methods.
How I can add the unimplemented methods like WindowListener, KeyListener, etc.??

Related

What method in JButton actually calls its ActionListener?

So I know how to program a GUI to get a JButton to react to a mouse press: Implement an ActionListener, and add that to the button...
What I want to know is what method in JButton (or something it inherits from ) take care of passing the mouse press event to the actionPeformed() method which is waiting for it. I don't need the details of how it works...but I would like to know what methods are involved.
I have tried to find it in the JavaDoc, but there is such a long inheritance chain, when you don't know where to look, you could be looking forever.
Can anyone help?

Firing a method in my Controller class from a button in my JFrame class

So I'm doing a project for school where I'm supposed to simulate a supermarket. I'm pretty far into it, but I came across a problem I haven't had before. Long story short, (among others) I have a controller class and a JFrame class.
In the main class and method, I have an object of the Controller class. In the controller cass I have an object of the JFrame class. When I run the project, the main makes a controller, which makes a JFrame.
Now, I want to add a button into the JFrame that signals the controller I want to 'move on' the simulation. I'm kind of stumped as to how to accomplish this. Basically, I want to add a button into the JFrame that will call a method in the controller class when clicked.
I'm happy to post code if necessary, but it's more a problem of 'how do I do this?' than fixing something that is broken.
Extend the JFrame with your own JFrame, which has an overloaded constructor that takes an instance of the Controller. Then when you create an instance of the JFrame from the Controller you pass it a reference to itself using the "this" keyword. When the button inside the JFrame needs to notify the controller it should be able to call some getController() method. Of course, this is all conjecture without seeing some actual code :)

Determining the source of a window activation event in Java

I am using the java WindowAdapter class to determine when a window is activated and then do some appropriate work. This is accomplished by implementing the windowActivated method of the WindowAdapter. Now, I am having some strange behavior and when I open one type of window it then activates another type afterwards. I tried to get a stack trace to see why the other window is being activated but the stack trace is not very helpful because all entries in the stacktrace are AWT methods until it gets to the call to the windowActivated method. Can anyone think of a good way to determine what triggered this listener?
Each of the WindowAdapter methods receives a WindowEvent. The getWindow() method should tell you which window generated the event. A short example might clarify the problem.

Swing - get main window class instance from button class

I need to run a method (lets call it SampleMethod) in a panel class upon the click on specific button. As panels are added to some sort of list in the main window class instance, I just made it public, and want to access it in mouseClicked event of the button.
So, how do I get the main window instance, so I could get that panel list, get the specific panel and run SampleMethod with this instance?
As panels are added to some sort of list in the main window class
instance...
The main window class instance? It seems you have only one instance of your main window which may extend JFrame or JPanel or JDialog etc. Anyway, you can use Singleton design pattern in your application to get and use the same instance (in your case the same main instance) anywhere in your code. In Singleton pattern, the constructor of the class that we apply the pattern is private, so you cannot make instances of it. However this class has also its single static instance which can be referenced via getter method and used anywere in your code. Take a look at Singleton examples on the web and you will be able to make a call like
YourJFrame.getInstance().getPanels(10).sampleMethod();
in your nested classes and methods, in your case mouseClicked().
You might look at Action as a way to expose the desired functionality. The common use is to let buttons and menus share the same handler, but one Action can forward an event to another, as shown here for buttons and keystrokes.
Window w = SwingUtilities.windowForComponent(Component c);
What trashgod said. Use the ActionListener class and the buttons addActionListener() instead since you only need to know if the button was pressed or not.

JRadioButton itemlistener not firing

Hello my Japplet is using a JComboBox and 5 JRadioButtons to draw and paint on the applet. Currently everything works except for my JRadioButtons which doesn't call the itemStateChanged() when a button is selected. So on the applet I can click on a button, but it won't fire. My combobox is also using the itemlistener interface and it works, but no matter what I've tried I can't get the buttons to send information/fire.
I noticed that it requires two clicks to select a button, and hope that a problem lies within that.
This is for a homework problem and if I could use an actionperformed and actionlistener I would :(. I need to use itemlistener. Below are examples of how I'm calling my radiobuttons, adding them to the shapes buttongroup, and adidng the buttons to the container c.
Thanks for any help!
Sorry to anyone reading this but because it was homework I'm not 100% sure I can keep the code up, PM me if you need help understanding it!
The code you posted is NOT a SSCCE!
Your question is about an ItemListener, so why did you post code related to a MouseListener and a MouseMotionListener? What does the custom painting code have to do with your problem?
How do you know you the ItemListener code isn't being invoked? Did you add a System.out.println(...) statement to the listener code? Test your code first using the "appletviewer". Its easier than using the browser. From the command line all you do is:
appletviewer P6.html
Or I find it easer to test the applet without even creating an HTML file. YOu can add the following line of code to the top of your source file:
// <applet code="P6.class" width="800" height="600"></applet>
Now from the command you you can test the applet just by using:
appletviewer P6.java
The problem with you code is that your radio buttons are defined as both class and local variables. The ItemListener generates a NullPointerException, because the class variables are null.
JRadioButton jrbOval = new JRadioButton("Oval");
should be:
jrbOval = new JRadioButton("Oval");
Also, you should not be overriding the paint() method of JApplet. Custom painting is done by overriding the paintComponent() method of a JPanel. Then you add the panel to the applet.
In general it's a bad idea to use your Applet class for so many Listeners. It just adds to the confusion and you now have a God object that handles too many events. See this discussion for more info:
Advantages to Nested Classes For Listeners in GUIs
The second issue is that you are heavily mixing java.awt and javax.swing objects, which are known to cause problems when they are put in the same container. You should definitely try splitting your Applet into 2 JPanels, one for awt stuff (paint, shapes, etc) and one for swing stuff (buttons, boxes, etc).
You seem to be using the ItemListener class properly, but when I saw that it takes two clicks to select a button, that was an obvious sign of awt/swing mixing/painting problems.

Resources