Just a quick question.
I'm doing a GUI which involves jTextfield, jButton, jLabels. Is it possible to group up this 3 component and have it hide. Only once a jButton is click, this 3 component will appear?
Similar usage for a button to create a new component, but in this case I wish to have it hide and unhide upon click on jButton.
You can add all the components to a JPanel and change the panel's visibility. When doing this you should consider a layout that doesn't use the space when the components are not visible. You could use MigLayout and set hideMode 3.
Here's a code example showing how to show/hide a grouped component, which is implemented as a JPanel
as others have suggested. The GroupedComponent
retains it's size even when hidden.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class SimpleTest extends JFrame {
GroupedComponent test = new GroupedComponent("one", "two", "three");
public SimpleTest() {
super("GroupedComponent Example");
JPanel content = (JPanel)getContentPane();
content.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
final JButton hideButton = new JButton(test.getButtonText());
hideButton.setPreferredSize(new Dimension(100,hideButton.getPreferredSize().height));
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
test.toggle();
hideButton.setText(test.getButtonText());
}
});
content.add(hideButton);
content.add(test);
}
public static void main(String[] argv) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleTest c = new SimpleTest();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.pack();
c.setVisible(true);
}
});
}
class GroupedComponent extends JPanel {
boolean visible = true;
JTextField field;
JButton button;
JLabel label;
GroupedComponent(String fieldText, String buttonText, String labelText) {
super(new GridLayout(1, 3, 4, 4));
field = new JTextField(fieldText);
button = new JButton(buttonText);
label = new JLabel(labelText);
add(field);
add(button);
add(label);
setBorder(new CompoundBorder(new LineBorder(Color.lightGray), new EmptyBorder(4,4,4,4)));
}
void toggle() {
if(visible) {
visible = false;
field.setVisible(false);
button.setVisible(false);
label.setVisible(false);
} else {
visible = true;
field.setVisible(true);
button.setVisible(true);
label.setVisible(true);
}
}
String getButtonText() {
return visible ? "Hide" : "Show";
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.