Why can't I add an attribute to my constructor? - java

public class Ouvrier extends javax.swing.JFrame {
private final int numOuvrier;
public Ouvrier(int numOuvrier) {
initComponents();
this.numOuvrier=numOuvrier;
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ouvrier().setVisible(true);
}
});
}
I have added an attribute to a constructor and an error appears in the main frame - why is that?

new Ouvrier(0).setVisible(true);
I dont know what numOuvrier means, but you need pass it.

Related

manipulating jLabel from main method in netbeans

I am trying to change the text inside a jLabel from the main method, the reason for this is because there are conditions that I need to meet for the change to happen and it is not trigger based.
Code:
public class TheMain extends javax.swing.JFrame {
public TheMain() {
initComponents();
}
public void changeLabel1(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jLabel1.setText("looo");
}
});
}
public void changeLabel2(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jLabel2.setText("looo");
}
});
}
public static void main(String args[]) {
TheMain some = new TheMain();
if(condition){
some.changeLabel1();
}else{
some.changeLabel2();
}
}
}
I tried printing some stuff inside changelabel1 and changelabel2 just to check if they are successfully called and it did print but I'm guessing it is not possible to implement the UI changes inside them, or am I mistaken?
if(condition){
some.changeLabel1();
}else{
some.changeLabel2();
}
The above logice needs to be defined in TheMain class because that is where the label variables will be defined.
The main() method is just used to create the GUI. There should be no application logic in the main() method.

SwingWorker: Syntax error on token "execute" -?

I'm trying to use a SwingWorker but for some reason at worker.execute(); I receive the following error:
"Syntax error on token "execute", Identifier expected after this
token"
That doesn't help much, it's a generic error, and no matter how stupid it might be, it's been torturing me for hours.. I've seen that moving SwingWorker somewhere else outside the class may fix it, but I don't understand how and why and why it should not work like this! Ugh!
public class App {
private App() { // CONSTRUCTOR
final int WINHSIZE = 2000;
final int WINVSIZE = 2000;
class Enjoy extends JPanel {
#Override
public void paintComponent(Graphics g) {
g.drawLine(0, 0, 2000, 2000);
}
class MyExecutor extends SwingWorker<Void,Void> {
#Override
protected Void doInBackground() {
return null;
}
#Override
protected void done(){ // runs on EDT
}
}
MyExecutor worker = new MyExecutor();
worker.execute(); // What the hell is going wrong here?
}
Runnable runner = new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Title");
JPanel panel = new Enjoy();
JScrollPane myScrollPane = new JScrollPane(panel);
f.add("Center", myScrollPane);
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater(runner);
}
public static void main(String[] args) {
new App();
}
}
Thanks.
worker.execute(); must be inside some method, it can't be directly within the scope of your Enjoy class.

How to reference the class fromasubclass

I am trying to find the answer but I can't. In java, when I create a subclass, how can I reffer the first level class? Using "this" accesses the subclass so I can't. The other option is passing the argument to the subclass but I'm curious if there is a simpliest method.
//here there is my other 1st level class implementation, this is a JFrame
btnNewProject.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
VehicleScreen pp=new VehicleScreen(**here should go the top class reference**);
//v is a JPanel that I pass to allJframes
v.setContentPane(pp);
v.setVisible(true);
}
});
class Foo {
int x;
Foo() {
new Runnable() {
public void run() {
Foo.this.x = 1;
}
}
}
}
You have a couple choices:
Make the variable outside the anonymous class an instance variable:
public class AnonymousClass extends JFrame {
private JLabel label;
...
public AnonymousClass() {
...
btnOk.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText(textField.getText());
}
});
...
}
You can also access the instance variable as follows:
AnonymousClass.this.label.setText(textField.getText());
Use the final modifier on the local variable:
public class AnonymousClass extends JFrame {
...
public AnonymousClass() {
final JLabel label = new JLabel("Enter a new message!");
btnOk.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText(textField.getText());
}
});
...
}
Be aware that using the final modifier will mean you cannot re-assign the variable at a later point in your program.

How to access my JFrame on EDT

I have a class on which it hold a JFrame, and on my JFrame, it contains some methods that i would want to call. The problem is that my class is in the main thread and mFrame is now delegated to the EDT thread, so how do i sent instructions to EDT to run those methods?
Update:
This is my FrameRadar that extends JFrame, as you see i have a update method, basically there is also a update on mDisplay and mRadar, those are jpanel.
FrameRadar Class ...(
public class FrameRadar{
...
public void update() {
mAccessor.getmDisplay().update();
mAccessor.getmRadar().update();
}
This is my MainRadar class, it contains a FrameRadar on which i also have a update method. That update method will call jframe's update.
class MainRadar(
....
private FrameRadar mFrame;
....
public StateMainGame() {
init();
}
public void init() {
....
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
mFrame = new FrameRadar();
mFrame.setVisible(true);
}
});
}
....
public void update() {
mFrame.update();
}
So as you see, MainRadar.Update-> FrameRadar->Update-> Jpanels->Update. The problem is that i cannot use
mFrame.update();

Java Event Queue : how to update component in JFrame

I have read about that when programming Java Swing, we should put these components into Java Event Queue, because Java Swing thread is not-thread safe.
But, when I use Event Queue, I don't know how to update component properties (for example : set text for label or change something..). Here is my code :
public class SwingExample {
private JLabel lblLabel;
SwingExample(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
lblLabel = new JLabel("Hello, world!", JLabel.CENTER);
frame.getContentPane().add(lblLabel); // adds to CENTER
frame.setSize(200, 150);
frame.setVisible(true);
}
public void setLabel(){
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception
{
SwingExample example = null;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
example = new SwingExample(); // ERROR : Cannot refer to non-final variable inside an inner class defined in different method
}
});
// sometime in the futures, i want to update label, so i will call this method...
example.setLabel();
}
}
I know, if I write SwingExample example = new SwingExample(); the error won't appear again, but if i use that, I cannot process example.setLabel later.
Please tell me about this error and how to fix this.
Thanks :)
By having your SwingExample instance as a field, you can reference it inside the inner classes without it being final.
public class SwingExample {
private JLabel lblLabel;
private static SwingExample instance;
SwingExample() {
// code omitted
}
public void setLabel() {
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
instance = new SwingExample();
}
});
// ...
EventQueue.invokeLater(new Runnable() {
public void run() {
instance.setLabel();
}
});
}
}

Categories

Resources