This question already has answers here:
Keyword for the outer class from an anonymous inner class [duplicate]
(2 answers)
Closed 9 years ago.
I am trying to create my own window class which extends JFrame. However, I am having a problem with the action listener for fullScreenBtn. When writing the ActionListener.actionPerformed funcion, I am unable to use the this keyword as it refers to new ActionListener. How do I refer to the instance of MyWindow instead?
public class MyWindow extends JFrame {
private static GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
private static GraphicsDevice gDev = gEnv.getDefaultScreenDevice();
private static JPanel toolbar = new JPanel();
private static JButton fullScreenBtn = new JButton("Show Full Screen");
private static boolean isFullScreen = false;
public MyWindow() {
toolbar.setLayout(new FlowLayout());
this.getContentPane().add(toolbar, BorderLayout.PAGE_START);
fullScreenBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Toggle full screen window
this.setUndecorated(!isFullScreen);
this.setResizable(isFullScreen);
gDev.setFullScreenWindow(this);
isFullScreen = !isFullScreen;
if (isFullScreen) {
fullScreenBtn.setText("Show Windowed");
} else {
fullScreenBtn.setText("Show Full Screen");
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
this.dispose();
System.exit(0);
}
});
}
}
In inner classes you will need to prepend your use of this with the class name of the outer class if you need to obtain a reference to the outer class: For example, use
MyWindow.this.setUndecorated(...)`
// etc...
As an aside, you really don't want to extend JFrame here, and in most situations.
Also, the ancestor Window that holds the JButton can be obtained in other ways such as via SwingUtilities.getWindowAncestor(theButton). i.e.,
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton button = (button) source;
Window ancestorWin = SwingUtilities.getAncestorWindow(button);
ancestorWin.setUndecorated(!isFullScreen);
ancestorWin.setResizable(isFullScreen);
// etc...
Or if you know most definitely that the ancestor window is a JFrame:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton button = (button) source;
JFrame ancestorWin = (JFrame) SwingUtilities.getAncestorWindow(button);
ancestorWin.setUndecorated(!isFullScreen);
ancestorWin.setResizable(isFullScreen);
// etc...
This is the syntax of accessing the enclosing-class's instance from an inner-class or an anonymous-class:
OuterClass.this.foo();
Because you are using an anonymous class, this will refer to that class, in this case the ActionListener. Because your ActionListener doesn't have methods such as setUndecorated, this will give you a compile error.
What you want to do is use MyWindow.this, followed by any method of MyWindow.
You will need to access this by specifying the outer class too, in your case it has to be something like follows:
MyWindow.this
Related
I have two classes and a text file database, The JLabel in the first class, let's call it class1 automatically set it self to the number in the database. Now, in class2 I have this little JFrame and a text field and of course a button, the value I put in the text field overwrites the one in the database, but here's the problem. The label in the first class wont update while running, but if I restart it it will show me the value that I want.
How do I update it while the program is running?
I've tried to change the label in the buttonActionperformed in the other class but it gives me a NullPointException every time.
How do I fix this?
THE MAIN CLASS ( JUST THE JFRAME )
package BankrollG;
import java.awt.Graphics;
import javax.swing.JFrame;
public class BGbrain {
BGbody body = new BGbody();
JFrame Frame = new JFrame();
public BGbrain() {
Frame.setSize(body.width, body.height);
Frame.setTitle(body.title);
Frame.setResizable(false);
Frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
Frame.add(body.panel);
Frame.setLocationRelativeTo(null);
Frame.setFocusable(true);
Frame.setVisible(true);
}
public static void main(String[] args ) {
new BGbrain();
}
}
Then you got the class with the components:
private JLabel bankroll_label
public BGbody(){
panel.setLayout(null);
windowComponents();
}
public void windowComponents() {
// Bankroll database access
try {
FileReader fr = new FileReader("Bankroll.txt");
BufferedReader br = new BufferedReader(fr);
set_bankroll = br.readLine();
} catch(IOException e) {
System.out.println("FEL MED LĂ„SNING AV DATABAS /// BGBODY");
}
}
}
THEN you got the JFrame class that I created with the netbeans function
private void AddcurrencyActionPerformed(java.awt.event.ActionEvent evt) {
String CBR = txt_bankroll.getText();
try {
FileWriter fw = new FileWriter("Bankroll.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println(CBR);
pw.close();
} catch(IOException e) {
System.out.println("FEL MED INSKRIVNINGEN I DATABASEN");
}
}
Now, everything goes as plan, but I can't update my JLabel "bankroll_label" from the button class because it just returns nullpointsexceptions. The data is there, because the JLabel reads from the database but it wont update when changes has been made from the button class. So a getter setter method wont work because the value IS there but it wont update the JLabel.
I hope this made it easier to understand my problem.
It's ALOT more code, that dont have to do with this, I hope I simplified it at least some.
Your question is a specific example of a basic problem in programming in Java -- how to transfer information between classes. There are several ways to do this, one of the most elegant being giving to use a "model" class that holds your program's logic code and key data, having one class change the model's state by changing a text String that it holds. Then using a listener or observer pattern, have the model notify the other class that it has been changed so the other class can extract the new information, its new String from the model. While this is likely the best solution, it may be a bit of overkill and likely is above your current level of coding, so for you, I'm not going to recommend this.
Instead, I'm going to recommend a simpler less elegant solution, that you instead have one class call a setter method of the other to push the new String into it.
One problem we have as volunteer answerers here is that your question is hard to answer because it lacks critical code, making it hard for us to guess why your code is misbehaving, why specifically you're running into a NullPointerException (or NPE) when you try to run it. So all I can do is guess, but guess I will try nevertheless.
For simplicity's sake, let's call one class the, the one that holds the JLabel, the LabelClass and the other class the ButtonTextFieldClass.
One possible reason is that you've got a NullPointerException is because your ButtonTextFieldClass may have a LabelClass variable, but never initialized the variable, something like so:
// this guy is null because it is never initialized
private LabelClass labelClass;
A simple solution could be to try to initialize it like so:
private LabelClass labelClass = new LabelClass();
But this won't work because while it does create and assign a LabelClass instance, it's likely not the LabelClass instance that is visualized in the running GUI.
A better solution is to give the ButtonTextFieldClass a setter method that allows other classes to set the ButtonTextFieldClass with the proper LabelClass instance.
e.g.,
public void setLabelClass(LabelClass labelClass) {
this.labelClass = labelClass;
}
This way the code that sets up both classes can pass the visualized LabelClass to the first class, and it can call methods on it.
A simple example of LabelClass could look like so:
class LabelClass extends JPanel {
private JLabel label = new JLabel("");
public LabelClass() {
setBorder(BorderFactory.createTitledBorder("Label Panel"));
add(label);
}
public void setLabelText(String text) {
label.setText(text);
}
}
I have it extend JPanel because this way it gives me the freedom of placing it into a JFrame or JDialog or other JPanel as I see fit. Note that I've made the JLabel private and have given the class a public setter method, setLabelText(String text), that allows outside classes the ability to set the JLabel's text.
The ButtonTextFieldClass could look something like:
class ButtonTextFieldClass extends JPanel {
private JTextField textField = new JTextField(10);
private JButton button = new JButton(new ButtonAction("Send Text"));
private LabelClass labelClass;
public ButtonTextFieldClass() {
setBorder(BorderFactory.createTitledBorder("Button TextField Panel"));
add(textField);
add(button);
}
// here we allow other classes to set instances of our LabelClass
public void setLabelClass(LabelClass labelClass) {
this.labelClass = labelClass;
}
// ....
I've also given the button an AbstractAction in place of an ActionListener since it is like a super ActionListener on steroids. Inside of it, I'd get the text from the JTextField and then call the LabelClass's setter method (if the variable is not null) to set the label's text:
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
if (labelClass != null) {
labelClass.setLabelText(text);
}
}
Then to set everything up, in another class I'd create instances of both LabelClass and ButtonTextFieldClass, and then "hook them up" by calling the setter method:
LabelClass labelClass = new LabelClass();
ButtonTextFieldClass buttonTextFieldClass = new ButtonTextFieldClass();
buttonTextFieldClass.setLabelClass(labelClass); // set our reference
The whole thing could look like so:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class TransferData {
private static void createAndShowGui() {
LabelClass labelClass = new LabelClass();
ButtonTextFieldClass buttonTextFieldClass = new ButtonTextFieldClass();
buttonTextFieldClass.setLabelClass(labelClass); // set our reference
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
mainPanel.add(buttonTextFieldClass);
mainPanel.add(labelClass);
JFrame frame = new JFrame("TransferData");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class LabelClass extends JPanel {
private JLabel label = new JLabel("");
public LabelClass() {
setBorder(BorderFactory.createTitledBorder("Label Panel"));
add(label);
}
public void setLabelText(String text) {
label.setText(text);
}
}
class ButtonTextFieldClass extends JPanel {
private JTextField textField = new JTextField(10);
private JButton button = new JButton(new ButtonAction("Send Text"));
// one possible solution -- give this class a variable
// of the LabelClass -- but don't initialize the variable
// here, but rather do it in a setter
private LabelClass labelClass;
public ButtonTextFieldClass() {
setBorder(BorderFactory.createTitledBorder("Button TextField Panel"));
add(textField);
add(button);
}
// here we allow other classes to set instances of our LabelClass
public void setLabelClass(LabelClass labelClass) {
this.labelClass = labelClass;
}
// an AbstractAction is like a "super" ActionListener
private class ButtonAction extends AbstractAction {
public ButtonAction(String name) {
super(name); // set the button's text and actionCommand
int mnemonic = (int) name.charAt(0); // get first char
putValue(MNEMONIC_KEY, mnemonic); // set mnemonic
}
#Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
if (labelClass != null) {
labelClass.setLabelText(text);
}
}
}
}
For simplicity's sake, I've displayed both JPanels within the same GUI, but it could work just as well if one JPanel were in one JFrame and the other within a JDialog.
I have class DnyMesice what creates many instances of JButton. Every instance contains variable poznamkaDne. This class DnyMesice contains actionListener to find poznamkaDne value of pushed JButton.
I have class Gui what creates one instance of mentioned class DnyMesice and one instance of JTextArea.
How can I refresh value of JTextArea (names poznamkovePole) if some JButton (in class DnyMesice) is pushed?
public class DnyMesice extends JPanel {
public String poznamkaDne="first note";
jButton tlacitkoDen;
public void zobrazMesic(Calendar kalendar){
for (c=1; c<30; c++){
tlacitkoDen = new JButton(Integer.toString(denvMesici));
tlacitkoDen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
poznamkaDne="New note";
};
});
add(tlacitkoDen);
}
}
}
public class Gui extends JFrame {
...
public void zobrazKalendar(){
...
panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
add(panel3);
JTextArea poznamkovePole;
poznamkovePole = new JTextArea();
poznamkovePole.setColumns(30);
poznamkovePole.setRows(5);
poznamkovePole.setText(panel2.poznamkaDne);
panel3.add(poznamkovePole);
}
Now the program shows in JTextArea only "first note" (which is defined during creating of instance JButton) but hot to refresh it after ActionListener action?
Maybe better if you will use:
Add to DnyMesice JTextArea link and in ActionListener change text.
public class DnyMesice extends JPanel {
private JTextArea poznamkaDne;
jButton tlacitkoDen;
public DnyMesice (JTextArea jTextArea){
this.poznamkaDne = jTextArea;
}
public void zobrazMesic(Calendar kalendar){
for (c=1; c<30; c++){
tlacitkoDen = new JButton(Integer.toString(denvMesici));
tlacitkoDen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
poznamkaDne.setText("New note");
};
});
add(tlacitkoDen);
}
}
}
P.S. - And please don't forget to use Code Conventions for the Java
Modify as follows:
// add these methods
public void setPoznamkaDne(String s) {
poznamkaDne = s;
}
public String getPoznamkaDne() {
return poznamkaDne;
}
// CHANGE this method (KEEP the rest of the code!)
public void actionPerformed(ActionEvent evt) {
setPoznamkaDne("New note");
};
poznamkovePole.setText(panel2.getPoznamkaDne());
These changes should allow you to update the text. BUT you need to either call poznamekovePole.setText() somehow, or implement an advanced listener class. I recommend combining your class like #Too Strong Magic said, above.
I have a private action listener class in the same class that uses it and I'm trying to make the current class disappear and make a new class visible when the JLabel is clicked. The JLabel is added dynamically at runtime. I don't know how to refer to my class without using this.setVisible(). I get a "cannot find symbol" error from my netbeans IDE when I use it. Here is my code:
public class displayImage extends javax.swing.JFrame {
private static class tagListener extends MouseAdapter {
public tagListener() {
}
public void mouseClicked(MouseEvent e) {
JLabel text = (JLabel) e.getSource();
displayImage display = new displayImage(text.getText());
this.setVisible(false);
display.setVisible(true);
}
}
//Creates new form displayImage
public static String tagWord;
public displayImage(String tag) {
initComponents();
tagWord = tag;
JLabel labelTag = new JLabel();
labelTag.setText(tagWord);
labelTag.addMouseListener(new tagListener());
}
//...other methods
}
There are two ways you can do this. The first was already stated by Cyrille Karmann above. The second is to pass in a reference to your JFrame to your MouseAdapter:
private static class tagListener extends MouseAdapter
{
private final JFrame frame;
public tagListener(JFrame frame)
{ this.frame = frame; }
public void mouseClicked(MouseEvent e)
{
JLabel text =(JLabel)e.getSource();
displayImage display = new displayImage(text.getText());
frame.setVisible(false);
display.setVisible(true);
}
}
public displayImage(String tag)
{
/* snip */
labelTag.addMouseListener(new tagListener(this));
}
You have made tagListener as an inner static class. Therefore it is just like another class and it doesn't share the this reference of the displayImage class.
Change the inner class definition to
private class tagListener extends MouseAdapter
and modify your call to setVisible to use tagListener.this displayImage.this, to specify which this you are refferring to:
displayImage.this.setVisible(false);
Some remarks: you should really use Java convention for class names: tagListener and displayImage should be TagListener and DisplayImage. Also, it is bad style to extends JFrame. Favor composition instead of inheritance.
public void mouseClicked(MouseEvent e)
{
JLabel text = (JLabel) e.getSource();
Window window = SwingUtilities.windowForComponent(text);
window.setVisible( false );
...
}
I am trying to create NewCard class, with implements a frame. How can I add Actionlisteners to elements in constructor of NewCard class? I can't put Actionlistener into constructor, and when I put it outside, element "field" is invisible for saveButtonListener block..
Second question: class Record in try block throws two exceptions, why try block generate error?
package Interface;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import Engine.*;
class NewCard extends JFrame
{
NewCard()
{
JFrame Card = new JFrame();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("New Card");
setSize(340, 580);
setVisible(true);
Container contentPane = getContentPane();
contentPane.setLayout(null);
// Field
JTextField field = new JTextField();
contentPane.add(field);
field.setBounds(110,15,200,25);
// Button:
JButton saveButton = new JButton("Save");
powZawartosci.add(saveButton);
saveButton.setBounds(95,495,150,25);
saveButtonListener listener1 = new saveButtonListener();
saveButton.addActionListener(listener1);
}
private class saveButtonListener implements ActionListener
{
try
{
#Override
public void actionPerformed(ActionEvent event)
{
new Record(field.getText());
}
}
catch(IOException e)
{
System.out.println("IOException");
}
catch(SQLException e)
{
System.out.println("SQLException");
}
finally
{
}
}
}
You could put your action listener inside constructor like this:
final JTextField field = new JTextField();
...
saveButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
new Record(field.getText());
}
});
Pass field to the saveButtonListener by creating an appropriate constructor for the listener.
Or use an anonymous listener, as shown in Mersenne's answer.
regarding:
I can't put Actionlistener into constructor, ...
Who says you can't? Simply add the ActionListener...
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonListener()); // capitalize class names
Or you can use anonymous inner classes, or even better, use AbstractActions.
Edit 1:
Regarding:
Second question: class Record in try block throws two exceptions, why try block generate error?
If you have a question about an exception, it makes lots of sense to show the exception.
Edit 2
Regarding:
class NewCard extends JFrame
{
NewCard()
{
JFrame Card = new JFrame();
Why have the class extend JFrame and create a JFrame inside the class that is never used?Best to not have the class extend JFrame but rather to create the JFrame when needed.
I would like to use setAlwaysOnTop(boolean) in java.
I want to setAlwaysOnTop() when I click on a JButton and this JButton has its own actionListener
My Problem is I don't know how to set the JFrame on top at this situation, because it's not inside the constructor nor there is a method getFrame()
I tried creating a method inside the constructor but it does not work :S.
UPDATE:
private class optionAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == onTop) //onTop is a menuItem when I click it it should make the frame Always on top.
frame.setAlwaysOnTop(true); //This does not work of course just to demonstrat you what I want to do
}
}
The following code lines show you how it can be done with a direct implementation of ActionListener() assigned to a button declared inside the constructor. (You can also do this anywhere else in your class.)
class MyFrame extends JFrame {
public MyFrame() {
// ...
JButton button = new JButton("PRESS");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setAlwaysOnTop(true);
// Alternatively use MyFrame.this.setAlwaysOnTop(true);
}
});
add(button);
// ...
}
}
An idea might be to pass a reference to your JFrame to the constructor of your implementation of the ActionListener.
Maybe something like this:
class MyActionListener implements ActionListener {
private JFrame jFrame;
public MyActionListener(JFrame jFrame) {
this.jFrame = jframe;
}
public void onClick(Event event) {
jFrame.setAlwaysOnTop(true);
}
}
create a boolean called ontop
boolean ontop = false;
jbutton.addActionListener(new ActionListener()) {
public void actionPerformed(ActionEvent e){
if (ontop) {
frame.setAlwaysOnTop(false);
ontop = false;
}
else {frame.setAlwaysOnTop(true); ontop = true}
});
The correct working code for this question is below:
private class optionAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == onTop) //onTop is a menuItem
setAlwaysOnTop(true); //This does not work of course just to demonstrate you what I want to do
}
}
The reason this is does not work because I was setting the setAlwaysOnTop on a JFrame object, which it doesn't exists in that class.
To set the setALwaysOnTop on a JFrame you have to remove the frame. and just add `setAlwaysOnTop()