I have a JFrame that has some JLabels, and when I click any JLabel, it opens another JFrame but I want the other to stay disabled. First I did:
JFrame frame2 = new JFrame();
frame2.setVisible(true);
this.setEnabled(false);
Then I want to setEnabled(true) the 1st frame when I close the 2nd frame. What I did is giving the 2nd frame a JFrame attribute and giving a param JFrame in its Constructor, and with a WindowListener I use the method windowClosing() like this:
public class My2ndJFrame extends JFrame implements ActionListener, WindowListener {
My1stJFrame frame;
public My2ndJFrame(My1stJFrame frame) {
this.frame = frame;
//moreCodeWeDontNeedNow
}
#Override
public void windowClosing(WindowEvent e) {
frame.setEnabled(true);
}
It works but I dont know if this is good programming practice, so I would like to know if there's a better way and if this is not good, why. Thank you.
Related
I want to disable a JPanel at the start of JFrame
I know the code that I have to use but I do not know where I should put it
public class Fenetre1 extends JFrame {
//code JFrame
private class Affichage implements ActionListener {
//action
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fenetre1 f = new Fenetre1 ();
f.panel.setEnabled(false);
}
You can setEnabled(false) when you create the JPanel. If you want to toggle enabling the JPanel, you should probably use a listener.
Notes from Java documentation (https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html)
Note: Disabling a component does not disable its children.
Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.
JFrame jframe = new JFrame();
JPanel jpanel = new JPanel();
jpanel.setEnabled(false);
jframe.add(jpanel);
Maybe JPanel can't be focused but Objects like JTextField or JTextArea are focus-able.
If you have some objects like those in the JPanel , then use textArea.setFocusable(false).
And you can try jPanel.setEnabled(false).
Your answer inspired me a bit, I use a component table in the JPanel
so,I went through the entire table and disabled each component
I put the code in the class inheriting the JFrame:
for(int j= 0;j<tab_component.length;j++)
{
tab_buttonsOperateur[j].setEnabled(false);
}
thanks anyway
I have a jframe object in a class and I want to be able to close the frame from my jpanel class(which obviously I attach to the frame). Anyway, I tried making a instance field in my jpanel with the jframe object has an instance field and then made a method that I would call in the jframe class with the parameter of the jframe object I made so I could make the jpanel instance field the same object as the jframe object. I then called the instance field.dispose(); hoping it would close the frame. Any ideas would be greatly appreciated!
In case that was hard to understand here is an example:
public class example extends jFrame
{
public static void main(String[]args)
{
examplePanel ep = new examplePanel();
example e = new example(ep);
}
/**
* Constructor for objects of class example
*/
public example(examplePanel ep)
{
//code that handles my frame settings
}
}
public class examplePanel extends jPanel implements ActionListener
{
private example e;
private boolean checkWin;
public void actionPerformed(ActionEvent e)
{
if(this.checkWin())
{
setVisible(false);
e.dispose();
//^this line of code is supposed to dispose of the frame but it does not
}
}
public void getExample(example e)
{
this.e = e;
}
}
Your code and question are hard to follow as you have an ActionListener which you add to no JButton or JMenuItem, You create a JFrame object and a JPanel, but are never observed to add the panel to the frame. You give your JPanel an "example" variable, but never assign it a reference to the visualized JFrame, you don't appear to ever set the default close operation of the JFrame, and so your JFrame as written above should be non-closable. From your code it looks like your examplePanel's e variable in the JPanel should in fact be null and so calling any method on it should throw a NullPointerException, that is unless you're assigning the correct JFrame object reference to it, but are not showing us.
Myself, I'd get the top level window from Swing itself when needed, something like:
#Override
public void actionPerformed(ActionEvent e) {
// get the top-level window that is displaying this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose(); // dispose of it
}
}
For example:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
public class CloseFromJPanel extends JPanel implements ActionListener {
private static final int PREF_W = 400;
private static final int PREF_H = 300;
public CloseFromJPanel() {
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(this);
add(closeButton);
}
#Override
public void actionPerformed(ActionEvent e) {
// get the top-level window that is displaying this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose(); // dispose of it
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Close From JPanel");
// GUI will exit when the JFrame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CloseFromJPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
This code will work for JButtons within JFrames and JDialogs, but not JMenuItems or within JApplets (I don't think). Or if all you want to do is end the application, then you could simply call System.exit(0) from within the actionPerformed method. If you absolutely want to do this using a field of the JFrame, then you'll need to pass in a reference to the JFrame into the JPanel, likely using a constructor parameter, and possibly passing in this.
If this doesn't help, please create and post real code, not kind-of sort-of code, code that we can compile, run and actually test, an MCVE (please check out the link).
Other issues:
Your code does not comply with Java naming standards as class names should all start with capital letters. Please Google this and study it, since if your code follows standards, others, including both us and your future self, will better be able to understand your code.
You'll rarely want to ever extend from JFrame since you rarely need to alter it's innate behavior. Usually you'll create and use a JFrame or JDialog when and where needed.
I made a JFrame in Java (Netbeans) with a button. When pressing that button another JFrame opens and the first frame has setEnabled to false. When I close my second Frame, I want the first one to be enabled again.... how can I do that?
Assuming you have JFrame subclass objects, you can pass your first frame as an argument to your second frame, and enable the first frame again in the close event of your second frame.
MyJFrame class:
private MyJFrame alphaFrame;
public MyJFrame(MyJFrame alpha) {
alphaFrame = alpha;
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
alphaFrame.setEnabled(true);
}
});
}
You can then instantiate your second frame (from the first, I assume) with something like this:
MyJFrame secondFrame = new MyJFrame(this);
I need to set to open a different JFrame as the defaultCloseOperation of the JFrame that I'm currently working.
Can I do that in Netbeans ?
http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html
Add a WindowListener to the first frame and when it closes, make the seconds frame and close this first frame. Then set defaultCloseOperation to DO_NOTHING_ON_CLOSE.
Non-working code just so you get the idea:
public class FirstFrame extends JFrame implements WindowListener {
(other stuff)
#Override
public void windowClosed(WindowEvent e) {
//make second frame
dispose();
}
}
I have a subclass of JFrame that uses a class extended from JPanel
public class HelloWorld extends JPanel implements KeyListener
I add an object of HelloWorld to the frame - app.add(helloWorld);. Now, when I press any keyboard key non of the KeyListener methods gets called and it seems that helloWorld doesn't have window focus. I have tried also to invoke helloWorld.requestFocusInWindow(); but still doesn't respond.
How can I make it respond to key press?
Did you set that KeyListener for your HelloWorld panel would be that panel itself? Also you probably need to set that panel focusable. I tested it by this code and it seems to work as it should
class HelloWorld extends JPanel implements KeyListener{
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped: "+e);
}
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed: "+e);
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased: "+e);
}
}
class MyFrame extends JFrame {
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
HelloWorld helloWorld=new HelloWorld();
helloWorld.addKeyListener(helloWorld);
helloWorld.setFocusable(true);
add(helloWorld);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
JPanel is not Focusable by default. That is, it can not respond to focus related events, meaning that it can not respond to the keyevents.
I would suggest trying to setFocusable on the pane to true and trying again. Make sure you click the panel first to make sure it receives focus.
Understand though, you WILL get strange focus traversal issues, as the panel will now receive input focus as the user navigates through your forms, making it seem like the focus has been lost some where.
Also, KeyListeners tend to be unreliable in this kind of situation (due to the way that the focus manager works).
simple you have to add
addKeylistener(new HelloWorld());
add this in MyFrame method;
HelloWorld() helloWorld = new HelloWorld();
this.addKeyListener(helloWorld);