2
I’m new to Swing and this is the first Java application I’m actually doing, but I’m having doubts about Jcombobox, because I would like to make a field (Jtextfield) where I put the directory and click on a button (Jbutton) and send it to Jcombobox, that is, insert data to Jcombobox at runtime.
Someone could give me a light?
PS: I’m making an application where I create a directory by this program.
Follows the code: https://ghostbin.com/paste/jumzm
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());
}
});
}
}
campoDiretorio.addItem("anItem");
If it’s a list, just scroll through it with a loop and add it that way inside the loop.– user28595
Well I did what you told me, but Jcombobox is in a Jlabel, and the screen that I add another directory to Jcombobox is in another Jlabel. 

Quando chamo o campoDiretorio.addItem(novoDiretorio) da erro, pois o JLabel não tem tal JComboBox...

Como faço pra haver uma comunicação entre uma JLabel que tem uma combox e outra jlabel que n tem combox mas quero usá-la para adicionar itens?
a
– Renan Narciso
Janelajavaeasydirectory: https://ghostbin.com/paste/yft Janelanovodiretorio: https://ghostbin.com/paste/ben8h
– Renan Narciso
Provide a [mcve] so that it is possible to test the code.
– user28595
In 2° gostbin link, I commented where the bug is. It’s on line 69
– Renan Narciso
In 2° ghostbin link, I commented where the bug is. It’s on line 69 and is commented, see if it’s enough, thank you.
– Renan Narciso
It is necessary to test the code, and both of the links are not testable. Without testing it is almost impossible to suggest a good solution.
– user28595
How do I post a code here in the commentary. The number of characters in the code here exceeds the limits.
– Renan Narciso
There is no way you can make a testable and reproducible example of your code only with the relevant parts of the problem instead of pasting it complete?
– user28595
I got to decrease the code, but I think if I post almost nothing will be something very superficial. Hold on
– Renan Narciso
JButton btnAddDiretorio = new JButton("Adicionar diretório");
 btnAddDiretorio.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent arg0) {
 
 String novoDiretorio = campoNovoDiretorio.getText();
 
 campoDiretorio.addItem(novoDiretorio); //Está com erro, pois não consigo acessar o JComboBox 'campoDiretorio' da outra classe no caso o JFrame JavaEasyDirectory.
 }
 });
– Renan Narciso
Renan, avoid posting code in the comments, It is horrible to read. Instead edit the question. What @diegofm was trying to tell you is to edit the question, remove everything unrelated to your doubt and leave only the smallest and simplest possible code that demonstrates your doubt, as long as it is compilable, executable and demonstrate the occurrence of your problem. See a huge code with a lot of things that has nothing to do with your problem and that matters a lot of things that you haven’t posted anywhere it doesn’t give!
– Victor Stafusa