I need to solve this error.
The window that i need to register the beer is not openning.
I'm creating a simple system where we can register and search beer's aparrently.
Here is the code.
package cadastro;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Entrada extends JFrame {
private static final long serialVersionUID = 42L;
private final Container container;
private final JButton btCadastrar;
private final JButton btPesquisar;
private final JButton btSair;
public Entrada() {
super("Sistema de Avaliação de Cervejas");
container = getContentPane();
container.setLayout(new GridLayout(0,1));
GerenciadorBotoes btManager = new GerenciadorBotoes();
btCadastrar = new JButton();
btPesquisar = new JButton();
btSair = new JButton();
btCadastrar.setText("Cadastrar Cervejas");
btCadastrar.setActionCommand("1");
btPesquisar.setText("Pesquisar Cervejas");
btPesquisar.setActionCommand("2");
btSair.setText("Sair");
btSair.setActionCommand("3");
btCadastrar.addActionListener(btManager);
btPesquisar.addActionListener(btManager);
btSair.addActionListener(btManager);
container.add(btCadastrar);
container.add(btPesquisar);
container.add(btSair);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(new java.awt.Rectangle(300, 150, 0, 0));
setSize(300, 200);
setResizable(false);
}
private class GerenciadorBotoes implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (null != e.getActionCommand()) {
if ("1".equals(e.getActionCommand())) {
new Cadastra().setVisible(true);
} else {
if ("2".equals(e.getActionCommand())) {
new Pesquisar().setVisible(true);
}
else {
}
}
}
}
}
static void abrir() {
try {
Acervo.getInstance().open(new File("agenda.poo"));
} catch (IOException | ClassNotFoundException e) {
}
}
public static void main(String args[]) {
Entrada.abrir();
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Entrada().setVisible(true);
}
});
}
}
and here the other.
package cadastro;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class Cadastra extends JFrame {
private final int id = 0;
private JButton btCadastrar;
private JButton btLimpar;
private JLabel lbNome;
private JLabel lbEmpresa;
private JLabel lbTipo;
private JLabel lbNota;
private JLabel lbComentario;
private JScrollPane scrollComent;
private JComboBox cbNota;
private JTextArea tfComentario;
private JTextField tfEmpresa;
private JTextField tfTipo;
private JTextField tfNome;
public Cadastra() {
initComponents();
}
private void initComponents() {
lbNome = new JLabel();
tfNome = new JTextField();
lbEmpresa = new JLabel();
tfEmpresa = new JTextField();
lbTipo = new JLabel();
tfTipo = new JTextField();
lbNota = new JLabel();
lbComentario = new JLabel();
scrollComent = new JScrollPane();
tfComentario = new JTextArea();
cbNota = new JComboBox();
btCadastrar = new JButton();
btLimpar = new JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Cadastrar");
setBounds(new java.awt.Rectangle(300, 150, 0, 0));
setPreferredSize(new java.awt.Dimension(700, 452));
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt) {
windowClose(evt);
}
});
lbNome.setText("Nome da Cerveja");
lbEmpresa.setText("Empresa");
tfEmpresa.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtnaActionPerformed(evt);
}
});
lbTipo.setText("Tipo");
lbNota.setText("Nota");
lbComentario.setText("Comentário");
tfComentario.setColumns(20);
tfComentario.setRows(5);
scrollComent.setViewportView(tfComentario);
cbNota.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
cbNota.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
notaActionPerformed(evt);
}
});
btCadastrar.setText("Cadastrar");
btCadastrar.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
btLimpar.setText("Limpar");
btLimpar.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(layout.createSequentialGroup()
.addComponent(lbNome)
.addGap(18, 18, 18)
.addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbTipo)
.addGap(18, 18, 18)
.addComponent(tfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(79, 79, 79)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lbNota)
.addGap(18, 18, 18)
.addComponent(cbNota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(173, 173, 173))
.addGroup(layout.createSequentialGroup()
.addComponent(lbEmpresa)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tfEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup()
.addComponent(lbComentario)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(scrollComent)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbEmpresa)
.addComponent(lbNome))
.addGap(82, 82, 82)))));
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]{btCadastrar, btLimpar});
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbNome)
.addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbEmpresa)
.addComponent(tfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbTipo)
.addComponent(tfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbNota)
.addComponent(cbNota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbComentario)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(btCadastrar)
.addGap(30, 30, 30)
.addComponent(btLimpar))
.addComponent(scrollComent, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(150, Short.MAX_VALUE)));
pack();
}
private void notaActionPerformed(java.awt.event.ActionEvent evt) {
//
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // Apaga os caracteres escritos
tfNome.setText("");
tfEmpresa.setText("");
tfTipo.setText("");
tfComentario.setText("");
cbNota.setSelectedIndex(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Gravar os dados cadastrados
String nome = tfNome.getText();
String nomeEmpresa = tfEmpresa.getText();
String tipo = tfTipo.getText();
String comentario = tfComentario.getText();
int selecionado = cbNota.getSelectedIndex();
String Snota = Integer.toString(selecionado + 1);
if (nome == null || nome.trim().equals("")) { // Se o nome for null ou o nome, retirando os espaços em branco, for igual a ""
JOptionPane.showMessageDialog(this, "Não foi possível adicionar. Forneça o nome da Cerveja!", "Erro", JOptionPane.ERROR_MESSAGE);
} else {
boolean temCerveja = Acervo.getInstance().temCerveja(nome);
if (temCerveja) {
JOptionPane.showMessageDialog(this, "Não foi possível adicionar. Cerveja com mesmo nome já cadastrado!", "Aviso", JOptionPane.WARNING_MESSAGE);
} else {
CervejaAtual cerveja = new CervejaAtual(nome ,nomeEmpresa,tipo , comentario, Snota);
Acervo acervo = Acervo.getInstance();
acervo.addCerveja(cerveja);
JOptionPane.showMessageDialog(this, "Adicionado com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
}
}
}
private void txtnaActionPerformed(java.awt.event.ActionEvent evt) {
}
private void windowClose(java.awt.event.WindowEvent evt) {
}
}
The error.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7e83ef0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Limpar,defaultCapable=true] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
at java.awt.Container.layout(Container.java:1510)
at java.awt.Container.doLayout(Container.java:1499)
at java.awt.Container.validateTree(Container.java:1695)
at java.awt.Container.validateTree(Container.java:1704)
at java.awt.Container.validateTree(Container.java:1704)
at java.awt.Container.validateTree(Container.java:1704)
at java.awt.Container.validate(Container.java:1630)
at java.awt.Container.validateUnconditionally(Container.java:1667)
at java.awt.Window.pack(Window.java:818)
at cadastro.Cadastra.initComponents(Cadastra.java:173)
at cadastro.Cadastra.<init>(Cadastra.java:36)
at cadastro.Entrada$GerenciadorBotoes.actionPerformed(Entrada.java:66)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
These lines:
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbEmpresa)
.addComponent(lbNome))
and these lines:
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(btCadastrar)
.addGap(30, 30, 30)
.addComponent(btLimpar))
you have to change lbEmpresa and lbNome to btCadastrar and btLimpar.