Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Code and Structure Help

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Location
    Patra Greece
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Code and Structure Help

    Hello
    I am building an arduino weather station that produces a CSV file with all the readings.

    I am trying to make a GUI java program that has some buttons (Temp Graph , Hum Graph ,... ) that when you press them , a window with the graph opens.

    My structure is as follows :

    openCSVreader.class --> Contains Main , Read method of the csv file , Run of the MainWeather gui
    MainWeather.class --> Its the GUI form and with the click listeners
    TempGraph.class --> Contains the reading of the csv and preparing the graph.

    For some reason the forum doesnt let me post code so I attach it.
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Aug 2018
    Location
    Patra Greece
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code and Structure Help

    really had problem posting my first post due to forum regulations.
    so I will continue here :

    problem
    Find a way to open again the graph without having " Application launch must not be called more than once" problem

    this is my code in the action listener. I know that I can use the Application.launch only one time. is there any other way to do it?
    import java.io.FileReader;
    import com.opencsv.CSVReader;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Scene;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.ScatterChart;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;
     
    public class TempGraph extends Application {
     
        @Override
        public void start(Stage TempStage) throws Exception {
            //Platform.setImplicitExit(false);
            TempStage.setTitle("Index Chart Sample");
            final NumberAxis yAxis = new NumberAxis(0, 80, 1);
            final CategoryAxis xAxis = new CategoryAxis();
     
            final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
            yAxis.setLabel("Temperature");
            xAxis.setLabel("Altitude");
            lineChart.setTitle("Sensor Graph");
     
            XYChart.Series temp = new XYChart.Series();
     
            temp.setName("Temperature");
     
            try (CSVReader dataReader = new CSVReader(new FileReader("test1.csv"))) {
                String[] nextLine;
                while ((nextLine = dataReader.readNext()) != null) {
                    String alt = String.valueOf(nextLine[0]);
                    int temperature = Integer.parseInt(nextLine[1]);
                    temp.getData().add(new XYChart.Data(alt, temperature));
                }
            }
     
            lineChart.getData().addAll(temp);
            Scene scene = new Scene(lineChart, 500, 400);
            TempStage.setScene(scene);
            TempStage.show();
        }
     
        }


    --- Update ---

    import com.opencsv.CSVReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.embed.swing.JFXPanel;
    import javafx.scene.Scene;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.ScatterChart;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;
     
     
    public class OpenCSVReader {
     
    private static final String SAMPLE_CSV_FILE_PATH = "./test.csv";
     
        public static void main(String[] args) throws IOException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
     
                @Override
     
                public void run() {
                    MainWeather mainWeather = new MainWeather();
                    mainWeather.setVisible(true);
                    mainWeather.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
                }
            });
     
            try (
                    Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
                    CSVReader csvReader = new CSVReader(reader);
            ) {
                // Reading Records One by One in a String array
                String[] nextRecord;
                while ((nextRecord = csvReader.readNext()) != null) {
                    System.out.println("Alt : " + nextRecord[0]);
                    System.out.println("Temp : " + nextRecord[1]);
                    System.out.println("Hyd : " + nextRecord[2]);
                    System.out.println("Hpa : " + nextRecord[3]);
                    System.out.println("==========================");
                }
            }
        }
    }


    --- Update ---

    import javafx.application.Application;
    import javafx.embed.swing.JFXPanel;
    import javafx.stage.Stage;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class MainWeather extends JFrame {
     
     
        private JButton TempButton;
        private JButton HumidButton;
        private JPanel MainPanel;
     
        public MainWeather ()
        {
     
            add(MainPanel);
            setTitle("Drone Weather Station");
            setSize(500,500);
            TempButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    TempGraph tempGraph = new TempGraph();
                    Application.launch(TempGraph.class);
                }
            });
            HumidButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Application.launch(HumGraph.class);
                }
            });
        }
    }
    Last edited by Kolokotronis; August 2nd, 2018 at 11:40 AM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code and Structure Help

    Can you copy and post the full contents of the error message stack trace?

    Also please post the import statements for the code so it can be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Aug 2018
    Location
    Patra Greece
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code and Structure Help

    Quote Originally Posted by Norm View Post
    Can you copy and post the full contents of the error message stack trace?

    Also please post the import statements for the code so it can be compiled and executed for testing.
    I updated my first post with the import statements

    I also attach the MainWeather.form which is the xml file for the GUI.

    Lastly here is the error message

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Application launch must not be called more than once
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:178)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:159)
    at javafx.graphics/javafx.application.Application.launch(Application. java:227)
    at MainWeather$1.actionPerformed(MainWeather.java:23)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:270)
    at java.desktop/java.awt.Component.processMouseEvent(Component.jav a:6589)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponen t.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:635 4)
    at java.desktop/java.awt.Container.processEvent(Container.java:226 1)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.jav a:4966)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.jav a:2319)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:47 98)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4914)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4543)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4484)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.jav a:2305)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772 )
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:47 98)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:772)
    at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 87)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 87)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java: 742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
    Attached Files Attached Files

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code and Structure Help

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Application launch must not be called more than once
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:178)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:159)
    at javafx.graphics/javafx.application.Application.launch(Application. java:227)
    at MainWeather$1.actionPerformed(MainWeather.java:23)
    The JVM thinks that the call to launch at line 23 is not the first time the launch method was called. Has the code called launch before?
    I guess the code can only call launch one time. (I don't know JavaFX and am guessing from the error message).
    The API doc says: This method is typically called from the main method(). It must not be called more than once or an exception will be thrown.

    From that error message it seems that the code should only call the launch method one time so the code needs to be changed so it only calls launch one time.

    Moved to JavaFX section
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Aug 2018
    Location
    Patra Greece
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code and Structure Help

    Quote Originally Posted by Norm View Post
    The JVM thinks that the call to launch at line 23 is not the first time the launch method was called. Has the code called launch before?
    I guess the code can only call launch one time. (I don't know JavaFX and am guessing from the error message).
    The API doc says: This method is typically called from the main method(). It must not be called more than once or an exception will be thrown.

    From that error message it seems that the code should only call the launch method one time so the code needs to be changed so it only calls launch one time.

    Moved to JavaFX section
    Yes I use this call--> Application.launch(TempGraph.class);
    so I launch the application once. How do I have to arrange my structure so I can open a window several times? Should I insert the Tempgraph code inside the action listener?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code and Structure Help

    Try asking the question here: https://coderanch.com/f/98/JavaFX
    I think there are people there that know JavaFX
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Kolokotronis (August 2nd, 2018)

Similar Threads

  1. Loop Structure
    By jasonkwp in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 23rd, 2017, 11:06 AM
  2. Replies: 1
    Last Post: February 8th, 2014, 05:20 PM
  3. Replies: 4
    Last Post: December 19th, 2013, 02:54 PM
  4. Structure
    By Gerardgrundy in forum Object Oriented Programming
    Replies: 8
    Last Post: November 3rd, 2012, 02:25 AM
  5. Re: Data Structure
    By jim17 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 16th, 2011, 11:14 PM