Olá, pessoal!
Não sou um expert em Java mas estou engatinhando e fazendo por onde para aprender esta linguagem.
Bem, estou desenvolvendo uma aplicação desktop relativamente simples, mas como ainda estou no começo, torna-se difícil.
A aplicação se comportará da seguinte forma:
Através de um jComboBox, o usuário selecionará uma filial e ao clicar em OK, e de acordo com a filial selecionada, um arquivo .ini correspondente a esta filial será copiado de um diretório para outro e renomeado para um determinado nome (ex.: arquivo.ini). O nome a ser renomeado após cópia para o destino será sempre o mesmo. O que muda é o nome do arquivo de origem.
Esta é minha classe principal:
[code]package pacs.selector;
public class PacsSelector {
public static void main(String[] args) {
new JFramePacsSelector().setVisible(true);
}
}
[/code]
Esta é a minha classe com o jFrame e seu jComboBox, botão, etc:
[code]package pacs.selector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
public class JFramePacsSelector extends javax.swing.JFrame {
public static final String pathIn = “C:\Teste”;
public static final String pathOut = “C:\Teste\Teste2”;
/**
* Creates new form JFramePacsSelector
*/
public JFramePacsSelector() {
initComponents();
}
/**
* 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">
private void initComponents() {
Painel = new javax.swing.JPanel();
LabelSelecaoUnidade = new javax.swing.JLabel();
ComboListaUnidades = new javax.swing.JComboBox();
BotaoSair = new javax.swing.JButton();
BotaoOK = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Pacs Selector V. 1.0");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
LabelSelecaoUnidade.setText("Selecione a unidade:");
ComboListaUnidades.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(1) Botafogo", "(107) Ilha", "(108) Caxias", "(109) Madureira", "(111) Soriano", "(112) Saens Pena", "(113) São Gonçalo", "(121) Niterói", "(123) Volta Redonda", "(130) Debret", "(134) Redentor", "(135) Meier - Dias da Cruz", "(137) Figueiredo Magalhães", "(139) Jardim Botânico", "(141) Vila da Penha", "(144) Recreio", "(155) Campo Grande", "(157) Icaraí", "(194) Barra Life" }));
ComboListaUnidades.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ComboListaUnidadesActionPerformed(evt);
}
});
BotaoSair.setText("Sair");
BotaoSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BotaoSairActionPerformed(evt);
}
});
BotaoOK.setText("OK");
BotaoOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BotaoOKActionPerformed(evt);
}
});
javax.swing.GroupLayout PainelLayout = new javax.swing.GroupLayout(Painel);
Painel.setLayout(PainelLayout);
PainelLayout.setHorizontalGroup(
PainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PainelLayout.createSequentialGroup()
.addGroup(PainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PainelLayout.createSequentialGroup()
.addGap(150, 150, 150)
.addComponent(BotaoSair))
.addGroup(PainelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(LabelSelecaoUnidade)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ComboListaUnidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addComponent(BotaoOK)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
PainelLayout.setVerticalGroup(
PainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PainelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(PainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LabelSelecaoUnidade)
.addComponent(ComboListaUnidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(BotaoOK))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(BotaoSair)
.addGap(19, 19, 19))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void BotaoSairActionPerformed(java.awt.event.ActionEvent evt) {
// Fecha a tela quando clica no botão sair
System.exit(0);
}
private void ComboListaUnidadesActionPerformed(java.awt.event.ActionEvent evt) {
//Obtém o item selecionado
ComboListaUnidades.getSelectedItem();
}
private void BotaoOKActionPerformed(java.awt.event.ActionEvent evt) {
//Converte o item selecionado para String
String unidade = ComboListaUnidades.getSelectedItem().toString();
if (ComboListaUnidades.getSelectedIndex()==0 ){
JOptionPane.showMessageDialog(null, "A unidade escolhida foi: " + unidade);
}
else{
JOptionPane.showMessageDialog(null, "A unidade não existe!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFramePacsSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFramePacsSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFramePacsSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFramePacsSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFramePacsSelector().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton BotaoOK;
private javax.swing.JButton BotaoSair;
private javax.swing.JComboBox ComboListaUnidades;
private javax.swing.JLabel LabelSelecaoUnidade;
private javax.swing.JPanel Painel;
// End of variables declaration
}
[/code]
O que fiz até o momento, foi o seguinte:
-
De acordo com o item selecionado, é realizada uma conversão para String.
-
Utilizei um if de acordo com a posição do item na jComboBox
Como ainda estou testando conforme o desenvolvimento, deixei apenas em teste o if exibindo a filial selecionada caso a mesma esteja na posição 0 e caso não esteja, dizendo que a filial não existe.
As dúvidas são as seguintes:
-
A utilização do if é recomendada para este caso?
A ideia de utilizar o if é: caso a posição seja 0 (ou 1, 2, 3, etc) ele busque o arquivo .ini (ex.: c:\filiais\filial1.ini) e copie para o diretório de origem renomeando para o nome que será sempre padrão (ex.: c:\programa\arquivo.ini). -
Como implementar a função de copiar o arquivo e renomeá-lo de acordo com a opção de filial escolhida?
Não sei se ficou confuso. Tentei explicar bem o que estou tentando. Caso haja alguma dúvida, eu explico melhor.
Desde já grato,

