Why/When ComponentListener.componentShown() get invoked? - java

Why this code never prints "Hello2" ?
public class Test4 {
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel();
f.getContentPane().add(p);
JLabel x = new JLabel("Hello");
p.add(x);
p.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent arg0) {
System.err.println("Hello1");
}
public void componentMoved(ComponentEvent arg0) {
}
public void componentShown(ComponentEvent arg0) {
System.err.println("Hello2");
}
public void componentHidden(ComponentEvent arg0) {
}
});
f.setVisible(true);
f.pack();
}
}

I would guess that it's called when the visibility state of the actual object changes.
in this case, you change the visibility of the Frame, not of the Panel.
(by default, Frame starts hidden, but panels are visible)
try to add the listener to the frame.

AWT's definition of "visible" may be a bit counter-intuitive. From the Javadoc of java.awt.Component#isVisible:
"Components are initially visible, with the exception of top level components such as
Frame objects."
According to this description, p is already visible before you add the ComponentListener. In fact, you can verify this if you insert a
System.out.println(p.getVisible());
anywhere before you call f.setVisible(true). In that sense, the visibility is not changed when displaying the frame and hence componentShown(..) is not called.

From Java Tutorials
The component-hidden and
component-shown events occur only as
the result of calls to a Component 's
setVisible method. For example, a
window might be miniaturized into an
icon (iconified) without a
component-hidden event being fired.

Related

AddMouseListener into another AddMouseListener

I had a problem while calling two mouse events, one into the other. I wanted to show a second frame (frame2) when the user clicks on a component (component1) from the first frame (frame1), then returns to the previous frame (frame1) if the component2 is clicked on. All this using one file.
This is what I wrote:
component1.addMouseListener(this on);
#Override
public void mouseClicked(MouseEvent e)
{
if(e.getSource() == component1)
{
frame1.dispose();
frame2.setVisible(true);
component2.addMouseListener(new MouseAdapter() {
public void mouseClicked() {
frame2.dispose();
frame1.setVisible(true);
}
});
}
}
The first event works, but not the second.
Thank you for answering.
Here is a fully functional example where there are 2 frames, each with a label that, when clicked, hides one frame and shows the other, done in Java 10. See if this works for you and explain how your code differs from this. Note that I only created 2 MouseListeners, one for each frame. I did not recreate the MouseListener in the other MouseListener's code. Also, I did not dispose the frame, which will likely cause problems. If I had disposed frame1, I would most likely have to create a new JFrame and assign it to the frame1 instance member.
Please note you have to click on the label itself, not somewhere else on the frame.
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TwoFrames {
public static void main(String[] args) {
TwoFrames twoFrames = new TwoFrames();
twoFrames.start();
}
private void start() {
setupFrames();
}
JFrame frame1 = new JFrame("Frame 1"),
frame2 = new JFrame("Frame 2");
JLabel component1 = new JLabel("Click me 1"),
component2 = new JLabel("Click me 2");
private void setupFrames() {
frame1.getContentPane().add(component1);
frame2.getContentPane().add(component2);
component1.setOpaque(true);
component2.setOpaque(true);
component1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
frame1.setVisible(false);
frame2.setVisible(true);
}
});
component2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
frame2.setVisible(false);
frame1.setVisible(true);
}
});
frame1.setSize(300, 300);
frame2.setSize(400, 400);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(() -> frame1.setVisible(true));
}
}
The dispose() method actually destroys the window and so, frame1 should become null and you are most likely getting a null pointer exception.
Consider calling frame1.setVisible(false) and frame2.setVisible(false) instead of the dispose() method.
Also, you could consider using to separate mouse listener objects instead of adding a new mouse listener to component2 when component1 is clicked.

JPanel wont appear until being resized [duplicate]

I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe.
But my image hasn't shown up until I make a change to jframe's size.
This is my code:
public class ImagePanel extends JPanel{
public void setImage(BufferedImage bi)
{
image = bi;
revalidate();
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null)
{
g.drawImage(image, 0, 0, this);
}
}
}
Verify that you invoke setVisible() after adding components and calling pack(), as discussed in this related example. You may also need to adopt an appropriate layout. Invoking repaint(), as suggested here, may fix the symptom but not the underlying cause.
Take a look at the docs for JPanel.add(), which it inherits from java.awt.Container:
Appends the specified component to the end of this container. This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int).
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.
Emphasis added.
Therefore, if you modify a Container after it's already been displayed, you must call validate() in order for it to show up. Just invoking repaint() is not enough. You may have noticed that calling setVisible(true) also works; this is because it calls validate() internally.
If you want to "refresh" the JPanel then you should call repaint(), which will call your paintComponent(). This should fix your problem:
public void setImage(BufferedImage bi)
{
image = bi;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
repaint();
}
});
}
Its good practice to update and change the GUI using the EDT. Heres more info on the EDT if you're interested:
How does the event dispatch thread work?
repaint doesn't need to be called from the EDT. If you're changing the GUI, such as setting text to a JLabel, it should be inside of the EDT. Heres more information on what can be called outside of the EDT (courtesy of nIcE cOw):
Safe to use Component.repaint() outside EDT?
I had the same problem and fixed it by call setVisible(true); the JFrame I was using.
Example : if your JFrame does not update after using :
jframe.setContentPane(new MyContentPane());
fix it with :
jframe.setContentPane(new MyContentPane());
jframe.setVisible(true);
I know that it sounds silly to do this even though your JFrame is already visible, but that's the only way I've found so far to fix this problem (the solution proposed above didn't work for me).
Here is a complete example. Run it and then uncomment the "f.setVisible(true);" instructions in classes Panel1 and Panel2 and you'll see the difference. Don't forget the imports (Ctrl + Shift + O for automatic imports).
Main class :
public class Main {
private static JFrame f;
public static void main(String[] args) {
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Panel1(f));
f.pack();
f.setVisible(true);
}
}
Panel1 class :
public class Panel1 extends JPanel{
private JFrame f;
public Panel1(JFrame frame) {
f = frame;
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
JButton b = new JButton("Panel 1");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setContentPane(new Panel2(f));
// Uncomment the instruction below to fix GUI "update-on-resize-only" problem
//f.setVisible(true);
}
});
add(b);
}
}
Panel2 class :
public class Panel2 extends JPanel{
private JFrame f;
public Panel2(JFrame frame) {
f = frame;
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
JButton b = new JButton("Panel 2");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setContentPane(new Panel1(f));
// Uncomment the instruction below to fix GUI "update-on-resize-only" problem
//f.setVisible(true);
}
});
add(b);
}
}
Hope that helps.
Regards.
I also had same problem but I found a solution. Just create a jframe object on top and call jframe methods at the bottom like jf.pack(), jf.setVisible(), jf.setSize(), jf.setDefaultCloseOpetion() should be at the bottom of the all UIs added in that frame you will find it work great.

What exactly a constructor does and why this code is not working in constructor?

I read about the constructer that a constructor is something that is used to set up the components of the container. So i wrote the following program which works well but only problem is that the label is not at the specified location i.e. 125,300. When I wrote the line label.setLocation(125,300) in the public run() then the code works well. Why it is not working when I write the line in the constructor? Basically it should work as the constructor is used to set up the components of the JFrame.
If not then What exactly is a constructor?
The Code:
public class RealGame extends JFrame implements Runnable, KeyListener{
JLabel label = new JLabel("I am a JLabel");
RealGame(){
setVisible(true);
requestFocus();
setContentPane(new JPanel());
getContentPane().setSize(640, 480);
setSize(640, 480);
label.setSize(50,50);
label.setOpaque(true);
label.setVisible(true);
label.setFont(new Font("Arial", Font.PLAIN, 18));
getContentPane().add(label);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RealGame());
}
#Override
public void run() {
label.setLocation(125,300); //THIS IS IMPORTANT
JOptionPane.showMessageDialog(null, "Hello");
System.out.println("Done");
System.exit(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
And also is the structure of the program correct? i.e. in many programs I saw that people use
SwingUtilities.invokeLater(new Runnable(){
public run(){
}
});
But instead I instantiated the main class. Will it give any problems in future?
but only problem is that the label is not at the specified location i.e. 125,300.
What is special about (125, 300). Don't use magic numbers in your program.
Swing uses layout managers to position components in a panel. The layout manager will determine the size and location of your component.
Read the section from the Swingtutorial on Layout Managers for more information and examples.
in many programs I saw that people use
Yes all GUI components should be updated on the Event Dispatch Thread(EDT). All the tutorial examples show you how to do this. You can also read the section in the tutorial on Concurrency in Swing for reasons why this is important.
When I wrote the line label.setLocation(125,300) in the public run() then the code works well
The code is added to the end of the EDT, which means it executes AFTER the layout manager. However this is only temporary. Try resizing the frame and the label will go back to the position determined by the layout manager. Don't try to set the location manually!

repaint of main panel not working properly when using an internal frame

I'm having the following bug/problem and I havent been able to found solution yet on web or example of someone with similar issue.Basicaly I have a main frame that contains a panel of the same size(acting as the main panel) and When you press "Enter" an internal frame pop up(on top of where the playersprite is) acting as the inventory and then the control is passed to it and if you press "Enter" again the inventory is destroyed and control is passed back to the main panel.
the repaint function is called and the character and the map is then redrawn and this work about 90% of the time.The other 10% or less of time whenever the inventory is destroyed it seems the repaint is called(and work) except nothing is drawn its as if it draw on the destroyed panel because if I add a debug keypress that call repaint on the mainpanel(thescreen) everything is back to normal.
of course I could just repaint the character every loop in the run() method but thats terrible since I will only repaint if something changed(ie I moved)
I removed all the move and other code since they arent useful and still get the problem with the below code.You can think of the Character class as a plain drawn square.Anyone as any insight on why this is happening?
public class main extends JFrame implements Runnable{
private boolean gameRunning=true;
private Character Link;
private MainScreen theScreen;
public final int ScreenHeight=500;
public final int ScreenWidth=500;
public boolean inMenu=false;
Block ablock=new Block(200,200);
public class Inventory extends JInternalFrame{
public Inventory(){
setBounds(25,25,300,300);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setVisible(true);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_ENTER){
try{
setClosed(true);
theScreen.requestFocusInWindow();
theScreen.repaint();
inMenu=false;
}
catch(Exception ex){}
}
}});
}
}
class MainScreen extends JPanel{
MainScreen(){
super();
setIgnoreRepaint(true);
setFocusable(true);
setBounds(0,0,ScreenWidth,ScreenHeight);
setVisible(true);
setBackground(Color.white);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Link.draw(g);
g.drawImage(ablock.getImg(),ablock.getX(), ablock.getY(),null);
}
}
main(){
super();
final JDesktopPane desk = new JDesktopPane();
theScreen=new MainScreen();
add(theScreen);
theScreen.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_ENTER){
inMenu=true;
Inventory myInventory=new Inventory();
desk.add(myInventory);
myInventory.requestFocusInWindow();
}
}
});
add(desk);
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {}
setTitle("Project X");
setResizable(false);
Link=new Character();
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
main Game=new main();
new Thread(Game).start();
}
public void run(){
//omitted/irrelevant only contains a FPS count
}
}
}
Don't use KeyListeners. Use Key Bindings.
Don't use a Thread. Use a Swing Timer for animation so updates will be done on the EDT.
Don't use an Internal Frame for a popup window. Use a JDialog.
Do custom painting on a JPanel, not a JDesktopPane.
Don't use setIgnoreRepaints(). That is used for active rendering.
Don't use empty catch clauses.
Use standard Java naming conventions. Classes start with upper cases characters, variable names do not.
Don't use setBounds(). Use a Layout Manager.

JPanel doesn't update until resize Jframe

I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe.
But my image hasn't shown up until I make a change to jframe's size.
This is my code:
public class ImagePanel extends JPanel{
public void setImage(BufferedImage bi)
{
image = bi;
revalidate();
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null)
{
g.drawImage(image, 0, 0, this);
}
}
}
Verify that you invoke setVisible() after adding components and calling pack(), as discussed in this related example. You may also need to adopt an appropriate layout. Invoking repaint(), as suggested here, may fix the symptom but not the underlying cause.
Take a look at the docs for JPanel.add(), which it inherits from java.awt.Container:
Appends the specified component to the end of this container. This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int).
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.
Emphasis added.
Therefore, if you modify a Container after it's already been displayed, you must call validate() in order for it to show up. Just invoking repaint() is not enough. You may have noticed that calling setVisible(true) also works; this is because it calls validate() internally.
If you want to "refresh" the JPanel then you should call repaint(), which will call your paintComponent(). This should fix your problem:
public void setImage(BufferedImage bi)
{
image = bi;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
repaint();
}
});
}
Its good practice to update and change the GUI using the EDT. Heres more info on the EDT if you're interested:
How does the event dispatch thread work?
repaint doesn't need to be called from the EDT. If you're changing the GUI, such as setting text to a JLabel, it should be inside of the EDT. Heres more information on what can be called outside of the EDT (courtesy of nIcE cOw):
Safe to use Component.repaint() outside EDT?
I had the same problem and fixed it by call setVisible(true); the JFrame I was using.
Example : if your JFrame does not update after using :
jframe.setContentPane(new MyContentPane());
fix it with :
jframe.setContentPane(new MyContentPane());
jframe.setVisible(true);
I know that it sounds silly to do this even though your JFrame is already visible, but that's the only way I've found so far to fix this problem (the solution proposed above didn't work for me).
Here is a complete example. Run it and then uncomment the "f.setVisible(true);" instructions in classes Panel1 and Panel2 and you'll see the difference. Don't forget the imports (Ctrl + Shift + O for automatic imports).
Main class :
public class Main {
private static JFrame f;
public static void main(String[] args) {
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Panel1(f));
f.pack();
f.setVisible(true);
}
}
Panel1 class :
public class Panel1 extends JPanel{
private JFrame f;
public Panel1(JFrame frame) {
f = frame;
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
JButton b = new JButton("Panel 1");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setContentPane(new Panel2(f));
// Uncomment the instruction below to fix GUI "update-on-resize-only" problem
//f.setVisible(true);
}
});
add(b);
}
}
Panel2 class :
public class Panel2 extends JPanel{
private JFrame f;
public Panel2(JFrame frame) {
f = frame;
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
JButton b = new JButton("Panel 2");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setContentPane(new Panel1(f));
// Uncomment the instruction below to fix GUI "update-on-resize-only" problem
//f.setVisible(true);
}
});
add(b);
}
}
Hope that helps.
Regards.
I also had same problem but I found a solution. Just create a jframe object on top and call jframe methods at the bottom like jf.pack(), jf.setVisible(), jf.setSize(), jf.setDefaultCloseOpetion() should be at the bottom of the all UIs added in that frame you will find it work great.

Categories

Resources