Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 54
Like Tree6Likes

Thread: Is it worth to learn Swing?

  1. #21
    Join Date
    Dec 2016
    Posts
    1
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Quote Originally Posted by JosAH View Post
    A lot of GUIs are still written using Swing; it is far from dead. You can create Swing applications in Eclipse easy enough.

    kind regards,

    Jos

    edit: I moved this thread to the New to Java section; there's nothing advanced about it.
    This is really helpful

  2. #22
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Java Code:
    package Button;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javafx.application.*;
    import javafx.stage.*;
    import javafx.scene.*;
    import javafx.scene.layout.*;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    import javafx.scene.control.*;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    
    public class Talent extends Application {
    
        Stage window;
        Scene scene1, scene2,scene3;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
            window = primaryStage;
    
     
            Image image = new Image(new FileInputStream("telnet.bmp"));
           
            ImageView imageView1 = new ImageView(image);
    	   	 imageView1.setFitHeight(400);
    	   	 imageView1.setFitWidth(339);
    	  
    	   
    	       Text tekst = new Text("Welcome to the first scene!");
    	       tekst.setFont(Font.font("Edwardian Script ITC", 42));
    	       tekst.setTranslateY(20);
    	       tekst.setTranslateX(160);	
    	       tekst.setFill(Color.LIGHTBLUE);
    	       
    	       
    	    Button mygt = new Button("Go to scene 2");
    	    mygt.setOnAction(e -> window.setScene(scene2));
    	 
    	  
            StackPane viskas = new StackPane();
            viskas.getChildren().addAll( imageView1,mygt,tekst);
            scene1 = new Scene(viskas, 500, 500);
            
         
            ///
            
            
            Button end = new Button ("backwards");
            end.setOnAction ( e -> window.setScene(scene1) );
            
            Button next = new Button ("Last stage");
            next.setOnAction ( e-> window.setScene (scene3));
            next.setTranslateY(14);
            next.setTranslateX(14);
            
            StackPane lay3 = new StackPane ();
            lay3.getChildren ().addAll (end,next);
            scene2= new Scene (lay3, 200, 200);
            scene2.setFill(Color.GREEN);
           
            
            ///
          
            
            final java.net.URL resource = getClass().getResource("pilvo.mp3");
            final Media media = new Media(resource.toString());
            final MediaPlayer mediaPlayer = new MediaPlayer(media);
            mediaPlayer.play();
            
            StackPane layout = new StackPane ();
            scene3 = new Scene (layout, 500,500);
            
    
    
            //Display scene 1 at first
            window.setScene(scene1);
            window.setTitle("Title Here");
            window.show();
            
        }
    
    }
    Attention to be focused on 77-80 line.

    Once user switches to next scene, I wish that audio file would be played.
    I found this code in internet, If I run this code - I receive:

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchAppl icationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchAppl ication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
    at Button.Talent.start(Talent.java:78)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$run AndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$nul l$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$run Later$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run( InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Nativ e Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$14 8(WinApplication.java:191)
    ... 1 more
    Exception running application Button.Talent

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,739
    Rep Power
    10

    Default Re: Is it worth to learn Swing?

    Apparently it can't find the mp3 file (resource) so its returning null and you get an NPE.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #24
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Cannot be. Because I even have not added this to scene3.

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,739
    Rep Power
    10

    Default Re: Is it worth to learn Swing?

    I am going by this.

    "at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
    at Button.Talent.start(Talent.java:78)"

    Otherwise, I don't know what is going on.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #26
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    203
    Rep Power
    3

    Default Re: Is it worth to learn Swing?

    I think Jim is right.
    try to replace line 78 with something like
    Java Code:
    final Media media = new Media("file:///Users/yourName/yourDocuments/folder1/folder2/folder3/folder4/pilvo.mp3");
    whereby the String inside the Media Constructor is the address of the .mp3 file on your machine.

  7. #27
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Nope.
    Please note: that code 77-80 is copied from another code.
    That another code had no scenes switching.
    So the code structure was diffrend. Here possibly can be lacking class/overload or something... ( in my full code)

  8. #28
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,739
    Rep Power
    10

    Default Re: Is it worth to learn Swing?

    Okay. You ask for help. We provide it based on the error message. And both times you disagree. If you know for certain then why are you asking for help?? I recommend that you print out each variable starting with line 77.

    Java Code:
    final java.net.URL resource = getClass().getResource("pilvo.mp3");
    System.out.println("resource = " + resource);
    Somewhere you are getting an NPE. You need to learn and apply the basic technique of debugging code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #29
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    203
    Rep Power
    3

    Default Re: Is it worth to learn Swing?

    What is the name of your media file and what is the extension?

  10. #30
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Is it possible to set 2 actions to happen once button is clicked?

    For example: If user clicks button 'play', the scene switches and integer value increases?

    Java Code:
     Button play = new Button("Play");
    	    	play.setTranslateX(100);
    	    	play.setTranslateY(60);
                    play.setOnAction ( e-> window.setScene (scene2));
    
                play.setOnMouseClicked((new EventHandler<MouseEvent>() {
    	    		private int a=0;
    
    				public void handle(MouseEvent event) {
    	    		   a = ++a;
    	    		  System.out.println (a);
    However I dont know how to combine them both?

  11. #31
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    203
    Rep Power
    3

    Default Re: Is it worth to learn Swing?

    There are multiple ways of doing this. I would surround the lambda section with curly brackets, the you can add multiple lines.

    button.setOnAction(e->{
    line1;
    line2;
    })

  12. #32
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    I tried. But it happens:
    scene switches, but integer value isnt increased.

  13. #33
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Java Code:
    Button play = new Button("Play");
    	        play.setOnAction(e-> {
    	        	 window.setScene (scene1) ;
    	        	
    	        	 play.setOnMouseClicked((new EventHandler<MouseEvent>() {
    		    		 int a;
    
    					public void handle(MouseEvent event) {
    		    		   a = ++a;
    		    		  System.out.println (a);
    		    	
    		    		}
    		    		})); 
    	        	 
    	        	 
    	        	 
    	        	});
    	        
    	        play.setTranslateX(100);
    	    	play.setTranslateY(60);
    I cant be sure, maybe I placed something wrong.

  14. #34
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    203
    Rep Power
    3

    Default Re: Is it worth to learn Swing?

    Maybe. Can you describe what you want the program to do?

  15. #35
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    I wrote a question.
    "a dog is..."
    The following 3 answers. User have to choose right button.
    buttons:
    "bird" "animal" "food"

    If user select "animal" - he gets 1 point ( Points keep stacking ). If choose another - gains 0 points.

    Once user selected answer ( clicked button). The scene is switching to another scene.
    The second scene also contains a question, and user have to choose right button to click.

    After that. The third scene shows how much % right questions was answered. ( In my case, user could collect 0 or 50 or 100% ).

  16. #36
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Java Code:
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    public class Main extends Application implements EventHandler<ActionEvent> {
        int a = 0;
        Button first, second;
       
        Scene scene1, scene2, scene3;
        Stage window;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
        	
        	window = primaryStage;
        	
        	primaryStage.setTitle("Title of Window");
           
        	first = new Button("Get bonus");
          
        	Button falsu = new Button ("wrong");
        	falsu.setOnAction ( e->   window.setScene (scene2) ) ;
        	falsu.setTranslateY (80);
        	
            //This class will handle the button events
            first.setOnAction(this);
    
            StackPane layout = new StackPane();
            layout.getChildren().addAll(first,falsu);
             scene1 = new Scene(layout, 300, 250);
           
            
            
         ///
            
             
            second = new Button ("get bonus");
        
            second.setOnAction (this);
            
            Button fals = new Button ("no points");
            fals.setOnAction ( e->   window.setScene (scene3) ) ;
            fals.setTranslateY(50);
            
            StackPane lay= new StackPane ();
            lay.getChildren().addAll( second, fals );
            scene2= new Scene ( lay,500, 500);
             
             
          ///
             
             
            Text answer = new Text (" You answered % "+ a);
            
            StackPane la = new StackPane (); 
            la.getChildren().addAll( answer );
            
            scene3 = new Scene (la,500,500); 
            
            primaryStage.setScene(scene1);
            primaryStage.show();
           
        }
    
        //When button is clicked, handle() gets called
        @Override
       
        public void handle (ActionEvent event) {
            if (event.getSource() == first) 
            	first.setOnAction ( e->   window.setScene (scene2) ) ;
            	this.a= ++a;
    	        System.out.println (a);
        }
    	
     	
        public void handle1 (ActionEvent event) {
      	     if (event.getSource() == second) 
      	         second.setOnAction ( e->   window.setScene (scene3) ) ;
      	        this.a= ++a;
      	         System.out.println (a);
        }
        
          
       
    }
    i have rewrote code.
    problems with points counting and (second) button at 2nd scene dont works.

    https://www.youtube.com/watch?v=PdFT0F5_5Ow
    Last edited by asdfg; 01-03-2017 at 01:23 AM.

  17. #37
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    203
    Rep Power
    3

    Default Re: Is it worth to learn Swing?

    The Actionhandler of first and second point for a place inside your class and therefore your Actionhandler will look for a method called handle(ActionEvent e){}. That method only exists once and it asks: if the event is coming from button first, do this. A method handle1(){} will not be called in your code. You have to add the second button action with another if statement in the handle method. Then you have to kill the line where you add an additional Actionhandler to the buttons in the handle() method. Just the window.setScene() method should do the trick. Then replace line 80 with "a++;" and see where that gets us.
    Last edited by benji2505; 01-03-2017 at 01:51 AM.

  18. #38
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Java Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    public class Main extends Application  {
        int a;
      
        Button first, second;
       
        Scene scene1, scene2, scene3;
        Stage window;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
        	
        	window = primaryStage;
        	
        	primaryStage.setTitle("Title of Window");
           
        	first = new Button("Get bonus");
        	first.setOnAction ( e-> {
        	a++;
        	System.out.println(a);
        	window.setScene (scene2); 
        	} ) ;
        	
        	
        	Button falsu = new Button ("wrong");
        	falsu.setOnAction ( e->   window.setScene (scene2) ) ;
        	falsu.setTranslateY (80);
        	
    
            StackPane layout = new StackPane();
            layout.getChildren().addAll(first,falsu);
            scene1 = new Scene(layout, 300, 250);
            
            
         ///
            
             
            second = new Button ("get bonus");
            second.setOnAction ( e->  {
            a++;
            System.out.println (a);
            window.setScene (scene3); 
            }	) ;
           
            
            Button fals = new Button ("no points");
            fals.setOnAction ( e->   window.setScene (scene3) ) ;
            fals.setTranslateY(50);
            
            StackPane lay= new StackPane ();
            lay.getChildren().addAll( second, fals );
            scene2 = new Scene ( lay,500, 500);
        
             
          ///
             
             
            Text answer = new Text (" You answered % "+ a);
            
            StackPane la = new StackPane (); 
            la.getChildren().addAll( answer );
            
            scene3 = new Scene (la,500,500); 
            
            primaryStage.setScene(scene1);
            primaryStage.show();
           
        }
    
       
       }
    Buttons now work well, but in 3rd scene it still shows 0%. While it should show 2%, because I clicked "get bonus" twice.

  19. #39
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,679
    Rep Power
    6

    Default Re: Is it worth to learn Swing?

    Quote Originally Posted by asdfg View Post
    Buttons now work well, but in 3rd scene it still shows 0%. While it should show 2%, because I clicked "get bonus" twice.
    You add the int a to the end of the String in line 68. At that point it's still 0, so the String will be like that all the time. You never update the String and you don't have any Observable properties either. I am no FX expert, but here's an example that works:
    Java Code:
    package forums;
    
    import javafx.application.Application;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    public class MainFXTest extends Application {
        SimpleIntegerProperty a;
    
        Button first, second;
    
        Scene scene1, scene2, scene3;
        Stage window;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            a = new SimpleIntegerProperty();
    
            window = primaryStage;
    
            primaryStage.setTitle("Title of Window");
    
            first = new Button("Get bonus");
            first.setOnAction(e -> {
                a.set(a.get() + 1);
                System.out.println(a);
                window.setScene(scene2);
            });
    
    
            Button falsu = new Button("wrong");
            falsu.setOnAction(e -> window.setScene(scene2));
            falsu.setTranslateY(80);
    
    
            StackPane layout = new StackPane();
            layout.getChildren().addAll(first, falsu);
            scene1 = new Scene(layout, 300, 250);
    
            second = new Button("get bonus");
            second.setOnAction(e -> {
                a.set(a.get()+1);
                System.out.println(a);
                window.setScene(scene3);
            });
    
    
            Button fals = new Button("no points");
            fals.setOnAction(e -> window.setScene(scene3));
            fals.setTranslateY(50);
    
            StackPane lay = new StackPane();
            lay.getChildren().addAll(second, fals);
            scene2 = new Scene(lay, 500, 500);
    
    
            Text answer = new Text();
            answer.textProperty().bind( a.asString("You answered: %d%%") );
    
            StackPane la = new StackPane();
            la.getChildren().addAll(answer);
    
            scene3 = new Scene(la, 500, 500);
    
            primaryStage.setScene(scene1);
            primaryStage.show();
    
        }
    
    
    }
    asdfg likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  20. #40
    asdfg is offline Member
    Join Date
    Nov 2016
    Posts
    98
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    Very interesting concept with SimpleIntegerProperty.
    Is it used only in FX ?
    Is it used to 'update' integer values?
    Was any other available option to overcome this?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Learn Niche Technology , browse your book now and learn
    By tech-ebooks in forum Reviews / Advertising
    Replies: 10
    Last Post: 11-08-2016, 02:11 PM
  2. What do I need to learn AWT or Swing?
    By kkid in forum AWT / Swing
    Replies: 5
    Last Post: 10-19-2012, 05:53 PM
  3. Replies: 4
    Last Post: 07-17-2010, 10:50 AM
  4. Replies: 0
    Last Post: 08-05-2008, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •