1
I’m having trouble trying to make a tab (tabbedPane) that is displayed according to the status of a Checkbox ("selected/unchecked"). I do not know if it is possible, since it will end up changing the screen size at runtime.
what I have done so far (Simple example):
package teste02;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class ExemploTela extends JFrame {
public ExemploTela() {
add(monta());
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public JComponent monta() {
JPanel painel = new JPanel();
painel.setLayout(null);
JTextField text = new JTextField();
JLabel label = new JLabel();
JCheckBox check = new JCheckBox("Exibe/Oculta");
painel.add(label);
label.setBounds(95, 90, 100, 25);
label.setText("Exemplo:");
painel.add(text);
text.setBounds(155, 90, 200, 25);
painel.add(check);
check.setBounds(155, 150, 200, 25);
painel.setBounds(1, 1, 500, 300);
check.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (check.isSelected()) {
add(montaAba());
} else {
}
}
});
return painel;
}
public JComponent montaAba() {
JPanel aba = new JPanel();
JTabbedPane tabbedPane = new JTabbedPane();
JLabel label = new JLabel("Testando aba");
tabbedPane.setPreferredSize(new Dimension(300, 100));
add(tabbedPane, BorderLayout.BEFORE_LINE_BEGINS);
tabbedPane.add(label);
aba.add(tabbedPane);
return aba;
}
public static void main(String[] args) {
ExemploTela x = new ExemploTela();
}
}
To Illustrate:
I even found the error (several, by the way) but I didn’t answer because I don’t see much sense in what you want to do. Can you better explain the purpose of this?
– user28595
@diegofm is that when registering, depending on the product the user will have to register "extra things" (example products with expiration date), more, in the case of a clothes registration, do not need to have this lot of field to more, then I wanted to leave hidden, It’s only gonna show up if he clicks on that checkbox. This example in the products, I thought of other utilities in other screens as well. If you can give me a help, I thank you already !
– Java
But why create a tabbedpane and put inside a panel then? Why not do it right on the panel or right on the tabbedpanel? I still don’t understand...
– user28595
@diegofm because I would like to do it this way, I didn’t want to leave it visible, more if there’s no way to do it, I’ll try to think of something else..
– Java
I’m not understanding your idea right, it might even be a different suggestion, but for that, I need to understand what you’re trying to do. If it’s not too much to ask, could you illustrate what you intend to do? Because this code has a lot of problems, the tabbed ta overlapping everything and is being added to two places, outside others.
– user28595
@diegofm see if this "understandable" now.
– Java
Now yes, I understand, because your code does something very different
– user28595