OK... There are some problems which are yours and some problems which
are ours :)
Your problem is that you don't know enough about Java Swing. I would
*strongly* suggest that you do some background reading. There are some
great tutorials on the web (google is your friend) and some very good
books available. You don't need to know much but you do need to
understand the basics quite well before using the gt-swing module.
Our problem is that there is a bug related to some new map classes
which causes an Exception if you try to add an empty MapContext, ie.
one that you haven't added any layers to yet, to the map frame. (Jody:
the problem is in MapContent line 274, adding listeners but not
checking for a null list).
The code below is a very cut-down version of your code. It works, ie.
can load a shapefile. You should be able to use it as the starting
point for your app.
May the force be with you :)
Michael
// main class
import java.util.Properties;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
Properties p = new Properties(System.getProperties());
p.put("com.sun.media.jai.disableMediaLib", "true");
System.setProperties(p);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyMapFrame frame = new MyMapFrame();
frame.setDefaultCloseOperation(MyMapFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
// frame class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import org.geotools.data.DataStore;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
public class MyMapFrame extends JMapFrame {
private static final long serialVersionUID = 1L;
public MyMapFrame() {
setTitle("Viewer");
setLocation(300, 100);
setSize(1280, 750);
enableLayerTable(true);
enableTool(JMapFrame.Tool.ZOOM, JMapFrame.Tool.PAN,
JMapFrame.Tool.RESET);
enableStatusBar(true);
initComponents();
}
@Override
public final void initComponents() {
super.initComponents();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem item = new JMenuItem("Open shapefile...");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openShapefile();
}
});
menu.add(item);
menuBar.add(menu);
setJMenuBar(menuBar);
}
private void openShapefile() {
File file = JFileDataStoreChooser.showOpenFile("shp", this);
if (file != null) {
try {
FileDataStore dataStore =
FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource source = dataStore.getFeatureSource();
Style style = SLD.createSimpleStyle(source.getSchema());
MapContext map = getMapContext();
if (map == null) {
// No map context set yet. Create one and also
// set a renderer
map = new DefaultMapContext();
map.addLayer(source, style);
setMapContext(map);
setRenderer(new StreamingRenderer());
}
getMapPane().repaint();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users