3

I Have some part of view which show text fields in JOptionPane. And there is a button "other". After click on it I need JOptionPane repaint itself and show some hidden text fields.

I use GridLayout for my text fields. I tried revalidate() and repaint() methods in the button listener but they do nothing changes.

The validate() method works but the size of all components is too small. Seems wrong resizing in the grid panel. In my original code some components are just hiding. I didn't setPrefferedSize() or getPrefferedSize() because I read it is wrong, but can't figure out how to make right resizing after.

I take my code and did simple as I can, just a part of view.

public class AddView {

    private JFrame parentFrame;
    private JPanel addPanel;
    private JPanel gd;

    private JLabel nameLabel;
    private JLabel surName;
    private JLabel skype;

    private JTextField nameTField;
    private JTextField surNameTField;
    private JTextField skypeTField;

    private JButton otherButton;

    public AddView(JFrame parent) {
        this.parentFrame = parent;
        initComponents();
    }

    private void initComponents() {
        addPanel = new JPanel(new BorderLayout());
        gd = new JPanel(new GridLayout(2, 2, 0, 5));

        nameLabel = new JLabel("Name");
        surName = new JLabel("Surname");
        skype = new JLabel("Skype");

        nameTField = new JTextField();
        surNameTField = new JTextField();
        skypeTField = new JTextField();

        otherButton = new JButton("Other");

        gd.add(nameLabel);
        gd.add(nameTField);
        gd.add(surName);
        gd.add(surNameTField);

        addPanel.add(gd, BorderLayout.CENTER);
        addPanel.add(otherButton, BorderLayout.SOUTH);

        otherButton.addActionListener(new OtherFieldsAction());
    }

    public int showAddPane() {
        return JOptionPane.showConfirmDialog(parentFrame, addPanel, "Add Contact", JOptionPane.OK_CANCEL_OPTION);
    }

    private class OtherFieldsAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            gd.setLayout(new GridLayout(3, 2, 0, 5));
            gd.add(skype);
            gd.add(skypeTField);
            // gd.revalidate(); not work
            // gd.repaint();
            gd.validate();

        }
    }
}

This is JOptionPane

enter image description here

When I click "other" button I have this result

enter image description here

But I need to get it with right resize like this

enter image description here

Can you give an advice about how to make right repaint.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
dimads
  • 77
  • 6
  • 2
    In this case it might be better to disable the components until they are relevant. – Andrew Thompson Oct 28 '15 at 17:29
  • @Andrew Thompson You mean using `JComponent.disable()`? Not sure I understand it right. – dimads Oct 28 '15 at 17:55
  • *"You mean using `JComponent.disable()`?"* Well, given that method has been deprecated since **Java 1.1,** no. Try instead [`setEnabled(boolean)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#setEnabled-boolean-) .. – Andrew Thompson Oct 28 '15 at 18:39
  • @Andrew Thompson But components will be visible. I need to hide them until user click the button. – dimads Oct 28 '15 at 19:21
  • Then put them in a `CardLayout` with a blank panel as the other card.. But don't try to resize the container - that is confusing to the user. – Andrew Thompson Oct 28 '15 at 19:24

2 Answers2

2

Maybe the Window#pack() method will work:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddView2 {
  private JFrame parentFrame;
  private JPanel addPanel;
  private JPanel gd;

  private JLabel nameLabel;
  private JLabel surName;
  private JLabel skype;

  private JTextField nameTField;
  private JTextField surNameTField;
  private JTextField skypeTField;

  private JButton otherButton;

  public AddView2(JFrame parent) {
    this.parentFrame = parent;
    initComponents();
  }

  private void initComponents() {
    addPanel = new JPanel(new BorderLayout());
    gd = new JPanel(new GridLayout(2, 2, 0, 5));

    nameLabel = new JLabel("Name");
    surName = new JLabel("Surname");
    skype = new JLabel("Skype");

    nameTField = new JTextField();
    surNameTField = new JTextField();
    skypeTField = new JTextField();

    otherButton = new JButton("Other");

    gd.add(nameLabel);
    gd.add(nameTField);
    gd.add(surName);
    gd.add(surNameTField);

    addPanel.add(gd, BorderLayout.CENTER);
    addPanel.add(otherButton, BorderLayout.SOUTH);

    otherButton.addActionListener(new OtherFieldsAction());
  }

  public int showAddPane() {
    return JOptionPane.showConfirmDialog(parentFrame, addPanel, "Add Contact", JOptionPane.OK_CANCEL_OPTION);
  }

  private class OtherFieldsAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      gd.setLayout(new GridLayout(3, 2, 0, 5));
      gd.add(skype);
      gd.add(skypeTField);
      // gd.revalidate(); not work
      // gd.repaint();
      // gd.validate();
      SwingUtilities.getWindowAncestor((Component) e.getSource()).pack();
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    new AddView2(f).showAddPane();
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
0

You may need to call setPreferredSize() of the added components to get the right sizes you need.

Rajeev Sampath
  • 2,739
  • 1
  • 16
  • 19