I have one JDialog in which there is one Main Jpanel called "dialogPane".
This JPanel "dialogPane" has below 2 JPanels
- ContentPanel which contains JTextArea
- buttonBar which contains progress bar and one toggle button.
Now my use case is
- On Click Of Toggle Button On/Off , TextArea should be made hidden(on)/displayed(off).
- When toggle button is clicked , JTextArea should be hidden and the Dialog size should be reduced (small)
- When toggle button is clicked again, JTextArea should be shown and Dialog Should be Resized (big)
Below is my actionListener for the Toggle Button. My code just removes the JTextArea but the size of the dialog is still big
private void ShowOrHideLogToggleButtonItemStateChanged(final ItemEvent e) {
final int state = e.getStateChange();
if (state == ItemEvent.SELECTED) {
ShowOrHideLogToggleButton.setText("Show Log");
dialogPane.remove(contentPanel);
dialogPane.updateUI();
dialogPane.revalidate();
dialogPane.repaint();
} else {
ShowOrHideLogToggleButton.setText("Hide Log");
dialogPane.add(contentPanel);
dialogPane.updateUI();
dialogPane.revalidate();
dialogPane.repaint();
this.pack();
}
}
Note : ContentPanel is the Jpanel which contains the JTextArea and dialogPane is the parent JPanel which contains ContentPanel
Minimal, Complete, and Verifiable example (Code To Reproduce The Problem)
package CollectionsPractice;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
/**
* @author User #2
*/
public class Main
{
public static void main(String[] args) {
JDialog myDialog=new JDialog();
new UpdateDialog(myDialog);
}
}
class UpdateDialog extends JDialog {
public UpdateDialog(final JDialog owner) {
super(owner);
initComponents();
resultTextPane.setText("Sample Text In Result Text Area");
}
private void ShowOrHideLogToggleButtonActionPerformed(final ActionEvent e) {
final JToggleButton btn = (JToggleButton) e.getSource();
if (btn.isSelected()) {
btn.setText("Hide Myy Log");
contentPanel.setVisible(true);
} else {
btn.setText("Show Myy Log");
contentPanel.setVisible(false);
}
pack();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
dialogPane = new JPanel();
contentPanel = new JPanel();
scrollPane1 = new JScrollPane();
resultTextPane = new JTextArea();
buttonBar = new JPanel();
progressBar = new JProgressBar();
ShowOrHideLogToggleButton = new JToggleButton();
logoutButton = new JButton();
//======== this ========
setTitle("Updater");
setAlwaysOnTop(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setMinimumSize(new Dimension(1024, 800));
setVisible(true);
setName("this");
final Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setMinimumSize(new Dimension(640, 480));
dialogPane.setName("dialogPane");
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setVisible(false);
contentPanel.setName("contentPanel");
contentPanel.setLayout(new BorderLayout());
//======== scrollPane1 ========
{
scrollPane1.setName("scrollPane1");
//---- resultTextPane ----
resultTextPane.setEditable(false);
resultTextPane.setName("resultTextPane");
scrollPane1.setViewportView(resultTextPane);
}
contentPanel.add(scrollPane1, BorderLayout.CENTER);
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setName("buttonBar");
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[]{1.0, 0.0, 0.0};
//---- progressBar ----
progressBar.setStringPainted(true);
progressBar.setName("progressBar");
buttonBar.add(progressBar, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
//---- ShowOrHideLogToggleButton ----
ShowOrHideLogToggleButton.setText("Show Log");
ShowOrHideLogToggleButton.setName("ShowOrHideLogToggleButton");
ShowOrHideLogToggleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
ShowOrHideLogToggleButtonActionPerformed(e);
}
});
buttonBar.add(ShowOrHideLogToggleButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
//---- logoutButton ----
logoutButton.setText("Logout");
logoutButton.setEnabled(false);
logoutButton.setName("logoutButton");
buttonBar.add(logoutButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel dialogPane;
private JPanel contentPanel;
private JScrollPane scrollPane1;
private JTextArea resultTextPane;
private JPanel buttonBar;
private JProgressBar progressBar;
private JToggleButton ShowOrHideLogToggleButton;
private JButton logoutButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}