Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package client.ui; import client.IBusinessLogic; import common.UploadFileResult; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.LayoutStyle; import javax.swing.WindowConstants; import org.apache.http.client.ClientProtocolException; /** * * @author yzj */ public class UploadFileWindow extends javax.swing.JDialog { private boolean confirmFlag; /** * Creates new form uploadFileWindow */ public UploadFileWindow(JFrame mainFrame) { super(mainFrame, true); confirmFlag = false; initComponents(); this.setTitle(""); this.setLocationRelativeTo(null); this.setVisible(true); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { filePathLabel = new JLabel(); pathField = new JTextField(); pathButton = new JButton(); filenameLabel = new JLabel(); filenameField = new JTextField(); confirmButton = new JButton(); filenameTip = new JLabel(); FormListener formListener = new FormListener(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setTitle(""); setResizable(false); filePathLabel.setFont(new Font("", 0, 12)); // NOI18N filePathLabel.setText(""); pathField.setFont(new Font("", 0, 12)); // NOI18N pathButton.setFont(new Font("", 0, 12)); // NOI18N pathButton.setText("?"); pathButton.addActionListener(formListener); filenameLabel.setFont(new Font("", 0, 12)); // NOI18N filenameLabel.setText("??"); filenameField.setFont(new Font("", 0, 12)); // NOI18N confirmButton.setFont(new Font("", 0, 12)); // NOI18N confirmButton.setText(""); confirmButton.addActionListener(formListener); filenameTip.setFont(new Font("", 0, 12)); // NOI18N filenameTip.setForeground(new Color(153, 153, 153)); filenameTip.setText("???"); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout .createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout .createSequentialGroup().addGap(19, 19, 19) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(filePathLabel).addComponent(filenameLabel)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(filenameTip) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false) .addComponent(pathField, GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE) .addComponent(filenameField)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(pathButton)))) .addGroup(layout.createSequentialGroup().addGap(167, 167, 167).addComponent(confirmButton))) .addContainerGap(33, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout .createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(filePathLabel) .addComponent(pathField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(pathButton)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(filenameLabel) .addComponent(filenameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(filenameTip) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE) .addComponent(confirmButton).addContainerGap())); pack(); } // Code for dispatching events from components to event handlers. private class FormListener implements ActionListener { FormListener() { } public void actionPerformed(ActionEvent evt) { if (evt.getSource() == pathButton) { UploadFileWindow.this.pathButtonActionPerformed(evt); } else if (evt.getSource() == confirmButton) { UploadFileWindow.this.confirmButtonActionPerformed(evt); } } }// </editor-fold>//GEN-END:initComponents private void confirmButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed String filePath = pathField.getText(); String filename = filenameField.getText(); if (filename.equals("")) { JOptionPane.showMessageDialog(this, "??", "", JOptionPane.WARNING_MESSAGE); return; } if (filePath.equals("")) { JOptionPane.showMessageDialog(this, "", "", JOptionPane.WARNING_MESSAGE); return; } confirmFlag = true; this.setVisible(false); }//GEN-LAST:event_confirmButtonActionPerformed private void pathButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_pathButtonActionPerformed JFileChooser fileWindow = new JFileChooser(); int confirm = fileWindow.showOpenDialog(null); if (confirm == JFileChooser.APPROVE_OPTION) { String filePath = fileWindow.getSelectedFile().getPath(); String filename = fileWindow.getSelectedFile().getName(); pathField.setText(filePath); filenameField.setText(filename); } }//GEN-LAST:event_pathButtonActionPerformed public String getFilePath() { return pathField.getText(); } public String getFileName() { return filenameField.getText(); } public boolean isConfirmed() { return confirmFlag; } // Variables declaration - do not modify//GEN-BEGIN:variables JButton confirmButton; JLabel filePathLabel; JTextField filenameField; JLabel filenameLabel; JLabel filenameTip; JButton pathButton; JTextField pathField; // End of variables declaration//GEN-END:variables }