Search code examples
javaworldwind

How to add widgets(Textfield,Combobox etc.) in world wind NASA


I am trying to work with world wind NASA, instead of selecting layer that comes in the left pane by default, I want to provide drop down so that I can select particular country /state from it and then display the things corresponding to the country/state on globe.

So how can I add widgets and reflect the same on globe?


Solution

  • Step 1: You need jdk 1.6 or more as per worldwind requirements. Install jar files like

    worldwinx.jar
    worldwind.jar
    gdal.jar
    glue-genrt.jar
    gluegen-rt-natives-windows-i586.jar
    jogl-all.jar
    jogl-all-natives-windows-i586.jar
    plugin.jar
    vpf-symbols.jar

    in your project library folder according to your operating system.

    Step 2: extend gov.nasa.worldwindx.examples.ApplicationTemplate class

    package yourPackageName;
    
    import gov.nasa.worldwind.WorldWind;
    import gov.nasa.worldwind.avlist.AVKey;
    import gov.nasa.worldwind.geom.Position;
    import gov.nasa.worldwind.layers.RenderableLayer;
    import gov.nasa.worldwind.render.*;
    import gov.nasa.worldwind.util.BasicDragger;
    import gov.nasa.worldwindx.examples.ApplicationTemplate;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class YourclassName extends ApplicationTemplate
    {
        public static class AppFrame extends ApplicationTemplate.AppFrame
        {
            private static JComboBox comboBox;
            private static JTextField textField;
    
            public AppFrame()
            {
                JPanel MainPanel = new JPanel();// new LayerPanel(this.wwjPanel.getWwd(), new Dimension(50, 50));
    
                GridLayout experimentLayout = new GridLayout(0,2);
    
                // Combobox
                JLabel labelCombo = new JLabel("Select Country");
    
                // Options in the combobox
                String[] options = { "India", "China", "USA", "Japan", "Russia" };
                    comboBox = new JComboBox(options);
                    comboBox.addActionListener(new ActionListener() {
    
                    public void actionPerformed(ActionEvent e) {
                       // Do something when you select a value
                    }
                });
    
                // Textfield
            JLabel labelTextField = new JLabel("Enter city");
             // Add controls
    
            textField = new JTextField();
            JPanel fieldPanel = new JPanel(experimentLayout);
    
    
            fieldPanel.add(labelCombo);
            fieldPanel.add(comboBox);
            fieldPanel.add(labelTextField);
            fieldPanel.add(textField);
            fieldPanel.add(new JButton("Go To city"));
            MainPanel.add(fieldPanel, BorderLayout.NORTH);
    
            this.getContentPane().add(MainPanel, BorderLayout.EAST);
        }
    }
    public static void main(String[] args)
    {
        ApplicationTemplate.start("World Wind Example", AppFrame.class);
    }
    }
    

    To reflect it on globe make a viewcontroller and add animator. The code is available in Nasa World Wind SDK Examples.

    Output : World Wind Widgets