java swing error dialog box - java

I have a dialog box to be shown but it is giving compilation errors. The compilation errors are given in the last part.
import javax.swing.*;
class SwingDemo {
SwingDemo() {
JFrame jfrm = new JFrame("A Simple Swing Application");
jfrm.setSize(275, 100);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel(" Swing means powerful GUIs.");
jfrm.add(jlab);
jfrm.setVisible(true);
}
public static void main(String args[]) {
public void run() {
new SwingDemo();
}
}
}
The errors are:
Multiple markers at this line
- Syntax error on token "void", # expected
- Syntax error, insert "enum Identifier" to complete EnumHeaderName
- Syntax error, insert "EnumBody" to complete BlockStatements

Just replace your main function with this.
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo();
}
});
}

First of all, do you use an IDE?
Your run() method is inside your main() method. You don't need a run method anyway. Just instantiate from main() new SwingDemo(); and remove the run() function like this:
public static void main(String[] args) {
new SwingDemo();
}

Related

How to create JFrame from another main class as separate thread

I want to create one Jframe from another class (main class) and then I want to display Jframe only if no error in main method. Otherwise I want to create & display dialog by passing that same jframe.
I forgot thread concept anywhere give me the solution, I tried below code, that prints "abcde" but not display the frame program complete.
Note : there is no main method in JFrame od ErrorDialog. They are just custom container.
public class Start{
public static Main mf=null;
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
mf = new Main();
}
});
try {
// Some extra code
System.out.println("abcde"); // this is print and then program complete
mf.setVisible(true); // this line will not run
} catch (Exception e) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ErrorDialog(mf, true).setVisible(true);;
}
});
}
}
}
Try my suggestion its working..
Just use invokeAndWait() method insted of using invokeLater() method.
Because invokeLater() method create NullpointerException.
But invokeAndWait() must be inside try-catch block.
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
mf = new Main();
}
});
} catch (Exception e) {
System.out.println(e);
}

Creating JFrame in an instance object

I'm trying to display a countdown and I'm searching how to do it and trying codes out but that's not what I'm here to ask in this question, although I'd be happy if you helped me in that area too.
This seems a bit elementary but I can't seem to get the JFrame showing.
I predicted that if I create an instance of the testmain and there's a creation of a JFrame in the constructor, it'd show the JFrame.
I even tried getting an input from the keyboard so that it'll stop.
But nothing happens and the program ends right away.
It says build successful.
What am I missing?
public class testmain
{
Timer t;
JLabel label;
public void testmain()
{
JFrame myFrame = new JFrame();
label = new JLabel();
myFrame.setSize(400, 400);
myFrame.setAlwaysOnTop(true);
myFrame.setLocationRelativeTo(null);
label.setText("This works");
myFrame.add(label);
myFrame.setVisible(true);
// Scanner keyboard = new Scanner(System.in);
// keyboard.nextInt();
// start();
}
void start()
{
t = new Timer(1000, new TimeTest());
}
class TimeTest implements ActionListener
{
private int counter = 0;
#Override
public void actionPerformed(ActionEvent e)
{
label.setText("" + counter++);
if(counter == 10)
t.removeActionListener(this);
}
}
public static void main(String[] args)
{
testmain tester = new testmain();
}
}
You've got a pseudo-constructor which is not being called. Constructors have no return type, not void, not anything.
Change
// this never gets called
public void testmain() {
}
to
// but this **will** be called
public testmain() {
}
As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
So the class should actually be called TestMain:
public class TestMain {
public TestMain() {
// constructor code here
}
}

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.

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