我需要帮助导入此类信息(CSV 文件)。
Stanmore College,51.612767,-0.311072
它按以下顺序排列:地点、经度、纬度。
如何使用上述数据从 CSV 文件导入数据?
我为用户创建了一个JFileChooser
&ActionListener
来加载 CSV 文件...
//---- btnLoadCSV ----
btnLoadCSV.setText("Load from CSV");
btnLoadCSV.setHorizontalAlignment(SwingConstants.LEFT);
btnLoadCSV.setMnemonic('L');
btnLoadCSV.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"CSV files (*csv)", "csv");
jfc.setFileFilter(filter);
int ret = jfc.showOpenDialog(GoogleApp.this);
if(ret == JFileChooser.APPROVE_OPTION)
{
File f = jfc.getSelectedFile();
String s = f.getAbsolutePath();
}
}
});
panel1.add(btnLoadCSV, new TableLayoutConstraints(5, 1, 5, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
导入时,它应该从文件中获取数据并将数据放入其中JLabels
(但我不知道从这里做什么......)
Place: label7
Longitude: ttfLon
Latitude: ttfLat
非常感谢!
完整的代码/类(如果你需要的话):
import Task.*;
import Task.Manager.*;
import Task.ProgressMonitor.*;
import Task.Support.CoreSupport.*;
import Task.Support.GUISupport.*;
import com.jgoodies.forms.factories.*;
import info.clearthought.layout.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.beans.*;
import java.text.*;
import java.util.concurrent.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
public class GoogleApp extends JFrame {
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// data members
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/** reference to task */
private SimpleTask _task;
/** this might be null. holds the image to display in a popup */
private BufferedImage _img;
/** this might be null. holds the text in case image doesn't display */
private String _respStr;
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// main method...
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public static void main(String[] args) {
Utils.createInEDT(GoogleApp.class);
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// constructor
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private void doInit() {
GUIUtils.setAppIcon(this, "burn.png");
GUIUtils.centerOnScreen(this);
setVisible(true);
int W = 28, H = W;
boolean blur = false;
float alpha = .7f;
try {
btnGetMap.setIcon(ImageUtils.loadScaledBufferedIcon("ok1.png", W, H, blur, alpha));
btnQuit.setIcon(ImageUtils.loadScaledBufferedIcon("charging.png", W, H, blur, alpha));
}
catch (Exception e) {
System.out.println(e);
}
_setupTask();
}
/** create a test task and wire it up with a task handler that dumps output to the textarea */
@SuppressWarnings("unchecked")
private void _setupTask() {
TaskExecutorIF<ByteBuffer> functor = new TaskExecutorAdapter<ByteBuffer>() {
public ByteBuffer doInBackground(Future<ByteBuffer> swingWorker,
SwingUIHookAdapter hook) throws Exception
{
_initHook(hook);
// get the uri for the static map
String uri = MapLookup.getMap(Double.parseDouble(ttfLat.getText()),
Double.parseDouble(ttfLon.getText()),
Integer.parseInt(ttfSizeW.getText()),
Integer.parseInt(ttfSizeH.getText()),
Integer.parseInt(ttfZoom.getText())
);
sout("Google Maps URI=" + uri);
// get the map from Google
GetMethod get = new GetMethod(uri);
new HttpClient().executeMethod(get);
ByteBuffer data = HttpUtils.getMonitoredResponse(hook, get);
try {
_img = ImageUtils.toCompatibleImage(ImageIO.read(data.getInputStream()));
sout("converted downloaded data to image...");
}
catch (Exception e) {
_img = null;
sout("The URI is not an image. Data is downloaded, can't display it as an image.");
_respStr = new String(data.getBytes());
}
return data;
}
@Override public String getName() {
return _task.getName();
}
};
_task = new SimpleTask(
new TaskManager(),
functor,
"HTTP GET Task",
"Download an image from a URL",
AutoShutdownSignals.Daemon
);
_task.addStatusListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
sout(":: task status change - " + ProgressMonitorUtils.parseStatusMessageFrom(evt));
lblProgressStatus.setText(ProgressMonitorUtils.parseStatusMessageFrom(evt));
}
});
_task.setTaskHandler(new
SimpleTaskHandler<ByteBuffer>() {
@Override public void beforeStart(AbstractTask task) {
sout(":: taskHandler - beforeStart");
}
@Override public void started(AbstractTask task) {
sout(":: taskHandler - started ");
}
/** {@link SampleApp#_initHook} adds the task status listener, which is removed here */
@Override public void stopped(long time, AbstractTask task) {
sout(":: taskHandler [" + task.getName() + "]- stopped");
sout(":: time = " + time / 1000f + "sec");
task.getUIHook().clearAllStatusListeners();
}
@Override public void interrupted(Throwable e, AbstractTask task) {
sout(":: taskHandler [" + task.getName() + "]- interrupted - " + e.toString());
}
@Override public void ok(ByteBuffer value, long time, AbstractTask task) {
sout(":: taskHandler [" + task.getName() + "]- ok - size=" + (value == null
? "null"
: value.toString()));
if (_img != null) {
_displayImgInFrame();
}
else _displayRespStrInFrame();
}
@Override public void error(Throwable e, long time, AbstractTask task) {
sout(":: taskHandler [" + task.getName() + "]- error - " + e.toString());
}
@Override public void cancelled(long time, AbstractTask task) {
sout(" :: taskHandler [" + task.getName() + "]- cancelled");
}
}
);
}
private SwingUIHookAdapter _initHook(SwingUIHookAdapter hook) {
hook.enableRecieveStatusNotification(checkboxRecvStatus.isSelected());
hook.enableSendStatusNotification(checkboxSendStatus.isSelected());
hook.setProgressMessage(ttfProgressMsg.getText());
PropertyChangeListener listener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
SwingUIHookAdapter.PropertyList type = ProgressMonitorUtils.parseTypeFrom(evt);
int progress = ProgressMonitorUtils.parsePercentFrom(evt);
String msg = ProgressMonitorUtils.parseMessageFrom(evt);
progressBar.setValue(progress);
progressBar.setString(type.toString());
sout(msg);
}
};
hook.addRecieveStatusListener(listener);
hook.addSendStatusListener(listener);
hook.addUnderlyingIOStreamInterruptedOrClosed(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
sout(evt.getPropertyName() + " fired!!!");
}
});
return hook;
}
private void _displayImgInFrame() {
final JFrame frame = new JFrame("Google Static Map");
GUIUtils.setAppIcon(frame, "71.png");
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JLabel imgLbl = new JLabel(new ImageIcon(_img));
imgLbl.setToolTipText(MessageFormat.format("<html>Image downloaded from URI<br>size: w={0}, h={1}</html>",
_img.getWidth(), _img.getHeight()));
imgLbl.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) { frame.dispose();}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
});
frame.setContentPane(imgLbl);
frame.pack();
GUIUtils.centerOnScreen(frame);
frame.setVisible(true);
}
private void _displayRespStrInFrame() {
final JFrame frame = new JFrame("Google Static Map - Error");
GUIUtils.setAppIcon(frame, "69.png");
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JTextArea response = new JTextArea(_respStr, 25, 80);
response.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) { frame.dispose();}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
});
frame.setContentPane(new JScrollPane(response));
frame.pack();
GUIUtils.centerOnScreen(frame);
frame.setVisible(true);
}
/** simply dump status info to the textarea */
private void sout(final String s) {
Runnable soutRunner = new Runnable() {
public void run() {
if (ttaStatus.getText().equals("")) {
ttaStatus.setText(s);
}
else {
ttaStatus.setText(ttaStatus.getText() + "\n" + s);
}
}
};
if (ThreadUtils.isInEDT()) {
soutRunner.run();
}
else {
SwingUtilities.invokeLater(soutRunner);
}
}
private void startTaskAction() {
try {
_task.execute();
}
catch (TaskException e) {
sout(e.getMessage());
}
}
public GoogleApp() {
initComponents();
doInit();
}
private void quitProgram() {
_task.shutdown();
System.exit(0);
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner non-commercial license
dialogPane = new JPanel();
contentPanel = new JPanel();
panel1 = new JPanel();
label2 = new JLabel();
ttfSizeW = new JTextField();
label4 = new JLabel();
ttfLat = new JTextField();
btnGetMap = new JButton();
btnLoadCSV = new JButton();
label3 = new JLabel();
ttfSizeH = new JTextField();
label5 = new JLabel();
ttfLon = new JTextField();
btnQuit = new JButton();
label1 = new JLabel();
label6 = new JLabel();
label7 = new JLabel();
ttfZoom = new JTextField();
scrollPane1 = new JScrollPane();
ttaStatus = new JTextArea();
panel2 = new JPanel();
panel3 = new JPanel();
checkboxRecvStatus = new JCheckBox();
checkboxSendStatus = new JCheckBox();
ttfProgressMsg = new JTextField();
progressBar = new JProgressBar();
lblProgressStatus = new JLabel();
//======== this ========
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("GMaps App Loader");
setIconImage(null);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setOpaque(false);
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setOpaque(false);
contentPanel.setLayout(new TableLayout(new double[][] {
{TableLayout.FILL},
{TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED}}));
((TableLayout)contentPanel.getLayout()).setHGap(5);
((TableLayout)contentPanel.getLayout()).setVGap(5);
//======== panel1 ========
{
panel1.setOpaque(false);
panel1.setBorder(new CompoundBorder(
new TitledBorder("Configure the inputs or load from CSV..."),
Borders.DLU2_BORDER));
panel1.setLayout(new TableLayout(new double[][] {
{0.17, 0.17, 0.17, 0.17, 0.05, TableLayout.FILL},
{TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}));
((TableLayout)panel1.getLayout()).setHGap(5);
((TableLayout)panel1.getLayout()).setVGap(5);
//---- label2 ----
label2.setText("Size Width");
label2.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label2, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfSizeW ----
ttfSizeW.setText("512");
panel1.add(ttfSizeW, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- label4 ----
label4.setText("Latitude");
label4.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label4, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfLat ----
ttfLat.setText("52.95907");
panel1.add(ttfLat, new TableLayoutConstraints(3, 0, 3, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- btnGetMap ----
btnGetMap.setText("Get Map");
btnGetMap.setHorizontalAlignment(SwingConstants.LEFT);
btnGetMap.setMnemonic('G');
btnGetMap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startTaskAction();
}
});
panel1.add(btnGetMap, new TableLayoutConstraints(5, 0, 5, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- btnLoadCSV ----
btnLoadCSV.setText("Load from CSV");
btnLoadCSV.setHorizontalAlignment(SwingConstants.LEFT);
btnLoadCSV.setMnemonic('L');
btnLoadCSV.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"CSV files (*csv)", "csv");
jfc.setFileFilter(filter);
int ret = jfc.showOpenDialog(GoogleApp.this);
if(ret == JFileChooser.APPROVE_OPTION)
{
File f = jfc.getSelectedFile();
String s = f.getAbsolutePath();
}
}
});
panel1.add(btnLoadCSV, new TableLayoutConstraints(5, 1, 5, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- label3 ----
label3.setText("Size Height");
label3.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label3, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfSizeH ----
ttfSizeH.setText("512");
panel1.add(ttfSizeH, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- label5 ----
label5.setText("Longitude");
label5.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label5, new TableLayoutConstraints(2, 1, 2, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfLon ----
ttfLon.setText("-1.182468");
panel1.add(ttfLon, new TableLayoutConstraints(3, 1, 3, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- btnQuit ----
btnQuit.setText("Quit");
btnQuit.setMnemonic('Q');
btnQuit.setHorizontalAlignment(SwingConstants.LEFT);
btnQuit.setHorizontalTextPosition(SwingConstants.RIGHT);
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quitProgram();
}
});
panel1.add(btnQuit, new TableLayoutConstraints(5, 2, 5, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- label1 ----
label1.setText("Zoom");
label1.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label1, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfZoom ----
ttfZoom.setText("17");
ttfZoom.setToolTipText("Enter Zoom Number. Max 19.");
panel1.add(ttfZoom, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- Spare ----
label6.setText("Place");
label6.setHorizontalAlignment(SwingConstants.RIGHT);
panel1.add(label6, new TableLayoutConstraints(2, 2, 2, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- Sparetextbox ----
label7.setText("-- CSV Only --");
panel1.add(label7, new TableLayoutConstraints(3, 2, 3, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
}
contentPanel.add(panel1, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//======== scrollPane1 ========
{
scrollPane1.setBorder(new TitledBorder("System.out - displays all status and progress messages, etc."));
scrollPane1.setOpaque(false);
//---- ttaStatus ----
ttaStatus.setBorder(Borders.createEmptyBorder("1dlu, 1dlu, 1dlu, 1dlu"));
ttaStatus.setToolTipText("<html>Task progress updates (messages) are displayed here,<br>along with any other output generated by the Task.<html>");
scrollPane1.setViewportView(ttaStatus);
}
contentPanel.add(scrollPane1, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//======== panel2 ========
{
panel2.setOpaque(false);
panel2.setBorder(new CompoundBorder(
new TitledBorder("Status - control progress reporting"),
Borders.DLU2_BORDER));
panel2.setLayout(new TableLayout(new double[][] {
{0.45, TableLayout.FILL, 0.45},
{TableLayout.PREFERRED, TableLayout.PREFERRED}}));
((TableLayout)panel2.getLayout()).setHGap(5);
((TableLayout)panel2.getLayout()).setVGap(5);
//======== panel3 ========
{
panel3.setOpaque(false);
panel3.setLayout(new GridLayout(1, 2));
//---- checkboxRecvStatus ----
checkboxRecvStatus.setText("Enable \"Recieve\"");
checkboxRecvStatus.setOpaque(false);
checkboxRecvStatus.setToolTipText("Task will fire \"send\" status updates");
checkboxRecvStatus.setSelected(true);
panel3.add(checkboxRecvStatus);
//---- checkboxSendStatus ----
checkboxSendStatus.setText("Enable \"Send\"");
checkboxSendStatus.setOpaque(false);
checkboxSendStatus.setToolTipText("Task will fire \"recieve\" status updates");
panel3.add(checkboxSendStatus);
}
panel2.add(panel3, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- ttfProgressMsg ----
ttfProgressMsg.setText("Connecting to Google Maps...");
ttfProgressMsg.setToolTipText("Set the task progress message here");
panel2.add(ttfProgressMsg, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- progressBar ----
progressBar.setStringPainted(true);
progressBar.setString("progress %");
progressBar.setToolTipText("% progress is displayed here");
panel2.add(progressBar, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
//---- lblProgressStatus ----
lblProgressStatus.setText("task status listener");
lblProgressStatus.setHorizontalTextPosition(SwingConstants.LEFT);
lblProgressStatus.setHorizontalAlignment(SwingConstants.LEFT);
lblProgressStatus.setToolTipText("Task status messages are displayed here when the task runs");
panel2.add(lblProgressStatus, new TableLayoutConstraints(2, 1, 2, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
}
contentPanel.add(panel2, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
setSize(675, 485);
setLocationRelativeTo(null);
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner non-commercial license
private JPanel dialogPane;
private JPanel contentPanel;
private JPanel panel1;
private JLabel label2;
private JTextField ttfSizeW;
private JLabel label4;
private JTextField ttfLat;
private JButton btnGetMap;
private JButton btnLoadCSV;
private JLabel label3;
private JTextField ttfSizeH;
private JLabel label5;
private JTextField ttfLon;
private JButton btnQuit;
private JLabel label1;
private JLabel label6;
private JLabel label7;
private JTextField ttfZoom;
private JScrollPane scrollPane1;
private JTextArea ttaStatus;
private JPanel panel2;
private JPanel panel3;
private JCheckBox checkboxRecvStatus;
private JCheckBox checkboxSendStatus;
private JTextField ttfProgressMsg;
private JFileChooser jfc;
private JProgressBar progressBar;
private JLabel lblProgressStatus;
}