So I created a handling class that implements the MouseListener (this one works). As the program runs some resources are being loaded and it creates the panel to be used for the MouseListener etc... Then when everything is done it's suppose to enter() the panel and add the MouseListener. enter() below:
#Override
public void enter() {
gh = new Game_handler(gr); //gh works fine and implements mouselistener
this.addMouseListener(gh); //gr is nothing but a chunk of data
this.requestFocus();
}
Now for some reason the MouseListener doesn't listen! I 've done some system prints and I know the panel is created then the MouseListener class and then it's added to the panel. But for some reason it just wont do anything!
Related
I have a few classes that use swing GUI and one JFrame that displays all of the GUI and all of GUI have an action listener for the Jbuttons and an ItemListener for the Jcheckboxes that cause various things to happen. Some of these buttons and checkboxes are other sub-classes that extend them.
when I do this
Public class SomeClass{
SomeClass(){
// some code here
methodCalled();
}
public void methodCalled(){
//more code here
new BazarGui();
new MapGui();
}
}
How do I actually get bazar GUI to wait for the user to click some buttons or checkboxes with out having the program go right to map GUI? they both work fine on their own but when one is called after the other only the second one shows. Would make the GUIs for the bazaar and map be JDialogs as opposed to JPanels and then adding them to the JFrame solve this problem?
i've created a JPanel with loaded components, called bkg, so I want to change it to SlideShow, that extends jpanel, and show it when i click on a button. this is the code called from the button:
public void startGame(){
bkg.removeAll();
bkg.revalidate();
bkg.repaint();
bkg = new SlideShow();
contentPane.add(bkg, BorderLayout.CENTER);
}
In the SlideShow constructor I've created some test labels, but when I click the button, all of old bkg components disappear (as I want) but nothing from SlideShow components I placed in the costructor appears... How can change the bkg JPanel to another external class that extends JPanel?
Suggestions:
Why remove/revalidate/repaint the bkg variable when you immediately replace the current object held by that variable with another?
What you want to revalidate and repaint is the contentPane after replacing the bkg object after adding it to the contentPane.
It's almost always better to use a CardLayout rather than manually swapping. The tutorial can be found here: CardLayout tutorial.
Or since it looks like you're re-starting a game, perhaps then give the SlideShow class a reset() method, one that sets both its model and its visible state to its original conditions.
So
public void startGame(){
// *** no need for this ***
// bkg.removeAll();
// bkg.revalidate();
// bkg.repaint();
bkg = new SlideShow();
contentPane.add(bkg, BorderLayout.CENTER);
// *** add this ***
contentPane.revalidate();
contentPane.repaint();
}
Or if you go the reset option, then the method would simplify to:
public void startGame(){
bkg.reset();
}
However that reset method would be key, the details depending on the structure of the rest of your program.
I have 2 frames. I want from the first frame to open the second. I tried this, but it leads to an exception.
class aboutaction implements ActionListener {
public void actionPerformed(ActionEvent e) {
frame.dispose();
aboutInfo about = new aboutInfo();
about.frame.setVisible(true);
}
}
about.addActionListener(new aboutaction());
This is the full program: https://github.com/Zhelyazkov97/Fuel-calculator.git.
You're basic problem is a NullPointerException create from a misunderstanding of how Swing works and a bad design.
Basically, you define your class as...
public class aboutInfo extends JFrame {
private JPanel contentPane;
javax.swing.JFrame frame;
Now, here the confusion starts, you basically have two frames, but you only ever add components to the instance of aboutInfo.
The basic answer here is, get rid of frame, it's just confusing the issue. In fact, you shouldn't be extending from JFrame in the first place, you should really use something like JPanel and simply add instances of the class to instances of JFrame or JDialog or what ever container you want
I have a class "GUI" that extends JPanel. I have another class "Buttons" that extends JFrame. I'm trying to have the JFrame class call a method "clearScreen()" in the JPanel class when the when a JButton "clearB" is pushed on the JFrame.
The only way I could make this work was by building the object for the JPanel class "GUI" right in the actionlistener for the JButton:
clearB.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
GUI g = new GUI();
g.clearScreen();
}
}
);
But then when I called the method clearScreen(), which looks like this:
public void clearScreen(){
xs.clear();
ys.clear();
count = 0;
repaint();
}
NOTHING HAPPENED. I'm guessing it's because the repaint() method wasn't working for some reason unknown to me.
Someone PLEASE show me an easier, working way of doing what I'm trying to accomplish here.
Thanks! :D
The reason that your ActionListener isn't working is because the GUI object that you create in there is a new GUI object, one that is completely unrelated to the GUI object that is displayed, and so calling the clearScreen() method on the non-displayed GUI instance will have no effect on the displayed GUI instance.
The solution is for your Buttons class to hold a valid reference to the visualized GUI object and call methods on this reference. The reference can be passed via a setter method or constructor parameter.
i.e.,
public class Buttons {
private GUI gui;
public Buttons (GUI gui) {
this.gui = gui;
}
// in some ActionListener code...
gui.someMethod();
}
A couple of comments:
It is unusual that you should have to have a class that extends JFrame. Myself, I try to avoid doing this unless necessary, but rather usually create my JFrames from the JFrame class itself, and only when needed.
I'm a little surprised that your main window class doesn't already have a GUI variable, since it likely displays the GUI instance.
First of all... I'd like to say that I am not interested in using the card layout for this... Unless it's necessary (which means that not using the card layout would be result in unnecessary workarounds and complex code). This is for learning purposes after all and I will look into the card layout very soon enough anyway...
Okay so my question is pretty basic GUI layout I guess. My code is not working and this whole layouting confuses me quite a lot...
I'm having trouble how to make the transition between JPanels like this:
I have one window. I press a button, the old window is replaced by another window. I press a button and that window will be replaced by another window.
I'd like to add that I am skipping a lot of irrelevant code in my example below...
I start off with a JFrame:
public class StartWindow extends JFrame{
public StartWindow(){
//Add JButton & ActionListener
//When the button is pressed:
add(new NextWindow());
}
public static void main(String [] args){
new StartWindow();
}
}
then I have several JPanels...
public class NextWindow extends JPanel{
public NextWindow(){
//Add a JButton & ActionListener
//When the button is pressed:
add(new NextWindow2());
remove(this);
//This does not work. Nothing happens.
}
}
public class NextWindow2 extends JPanel{ // Stuff and so on}
So, I'd like to know a proper way to handle this situation!
You are adding a panel to itself. You need to remove the panel from the JFrame, add the new one to it, and call revalidate() on the JFrame.