I'm new to Swing and this is the first Java application I'm actually doing, however I'm doubtfully related to JComboBox because I'd like to do a field (JTextField) where I'd put the directory and click a button JButton) and send it to the JComboBox, that is, to enter data into the JComboBox at run time. Could someone give me a light?
PS: I'm doing an application where I create a directory by that program.
Follow the code: link
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import controller.ControleJavaEasyDirectory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JSpinner;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JMenuBar;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollBar;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextPane;
import java.awt.List;
import java.awt.Button;
import javax.swing.JSeparator;
public class JanelaJavaEasyDirectory extends JFrame {
private JPanel contentPane;
private JTextField campoNomeDoArquivo;
private ControleJavaEasyDirectory jed = new ControleJavaEasyDirectory();
Frame janela2 = new Frame();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JanelaJavaEasyDirectory frame = new JanelaJavaEasyDirectory();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JanelaJavaEasyDirectory() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 308);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuSistema = new JMenu("Sistema");
menuBar.add(menuSistema);
JMenuItem mntmDiretorio = new JMenuItem("Novo diret\u00F3rio");
mntmDiretorio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
janela2 = new Frame();
janela2.setLocationRelativeTo(contentPane);
janela2.setVisible(true);
// janela2.setResizable(false);
}
});
mntmDiretorio.setIcon(new ImageIcon(JanelaJavaEasyDirectory.class
.getResource("/com/sun/javafx/scene/control/skin/caspian/fxvk-capslock-button.png")));
menuSistema.add(mntmDiretorio);
JMenuItem mntmSair = new JMenuItem("Sair");
mntmSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(JFrame.EXIT_ON_CLOSE);
}
});
JSeparator separator = new JSeparator();
menuSistema.add(separator);
mntmSair.setIcon(new ImageIcon(
JanelaJavaEasyDirectory.class.getResource("/com/sun/java/swing/plaf/windows/icons/Error.gif")));
menuSistema.add(mntmSair);
contentPane = new JPanel();
contentPane.setToolTipText("");
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblJavaEasyDirectory = new JLabel("Java Easy Directory");
lblJavaEasyDirectory.setFont(new Font("Tahoma", Font.BOLD, 16));
lblJavaEasyDirectory.setBounds(139, 50, 159, 17);
contentPane.add(lblJavaEasyDirectory);
JLabel lblDiretrio = new JLabel("Diret\u00F3rio:");
lblDiretrio.setToolTipText("Selecione um diret\u00F3rio.");
lblDiretrio.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblDiretrio.setBounds(35, 88, 75, 14);
contentPane.add(lblDiretrio);
JComboBox campoDiretorio = new JComboBox();
campoDiretorio.setModel(new DefaultComboBoxModel(new String[] { "C:\Users\renan\Downloads\ISOs" }));
campoDiretorio.setBounds(109, 87, 264, 20);
contentPane.add(campoDiretorio);
JLabel lblNomeDoArquivo = new JLabel("Nome da pasta:");
lblNomeDoArquivo.setToolTipText("Insira o nome da pasta que deseja criar.");
lblNomeDoArquivo.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNomeDoArquivo.setBounds(51, 129, 135, 14);
contentPane.add(lblNomeDoArquivo);
campoNomeDoArquivo = new JTextField();
campoNomeDoArquivo.setBounds(170, 128, 203, 20);
contentPane.add(campoNomeDoArquivo);
campoNomeDoArquivo.setColumns(10);
JButton btnCriarDiretrio = new JButton("Criar diret\u00F3rio");
btnCriarDiretrio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String nomeDirDesejado = (String) campoDiretorio.getSelectedItem();
String nomePasta = campoNomeDoArquivo.getText();
File novaPasta = new File(nomeDirDesejado, nomePasta);
novaPasta.mkdir();
if (nomePasta.equals(""))
throw new IllegalArgumentException();
else
JOptionPane.showMessageDialog(null, "Diretório criado com sucesso!");
}
});
btnCriarDiretrio.setBounds(152, 171, 126, 23);
contentPane.add(btnCriarDiretrio);
JLabel lblDesenvolvidoPorRenan = new JLabel("Desenvolvido por Renan Narciso");
lblDesenvolvidoPorRenan.setBounds(125, 223, 185, 14);
contentPane.add(lblDesenvolvidoPorRenan);
JLabel lblV = new JLabel("v1.0 - 2017");
lblV.setBounds(361, 223, 63, 14);
contentPane.add(lblV);
contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] { lblJavaEasyDirectory,
lblDiretrio, campoDiretorio, lblNomeDoArquivo, campoNomeDoArquivo, btnCriarDiretrio }));
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}