Swing components resizing when keyboard focus

I have two problems with the layout of my gui. I am using JTabbedPane to hold two JPanels, each panel has a set of buttons and text areas, and each is laid out using a GridBagLayout. In one of my panels, I have a JScrollPane that uses a JTextArea. When I add something to that text area and then exit the gui so that it doesn't focus anymore, or if I change the tab, all text boxes and text resize as much as possible.

To illustrate my problem again, here are the before and after snapshots when I click the gui after adding to the text area: Before I click off the gui

After I click off the gui

Here is the code that I am using to add the JTextArea to the Panel:

table = new JTextArea();
    table.setEditable(false);
    JScrollPane sp = new JScrollPane(table);
    sp.setSize(40, 10);
    c.insets = new Insets(10,10,10,10);
    c.gridx = 1;
    c.gridwidth = 4;
    c.gridy = 7;
    c.gridheight = 7;
    this.add(sp, c);

      

And here is the code I'm using to add the Region text to the panel:

title = new JTextField(10);
    author = new JTextField(10);
    dueDate = new JTextField(10);
    setDate = new JTextField(10);
    setWeighting = new JTextField(10);

    c.gridx = 2;
    c.gridy = 1;
    this.add(title, c);//add title field
    c.gridx = 2;
    c.gridy = 2;
    this.add(author, c);//add author field
    c.gridx = 2;
    c.gridy = 3;
    this.add(dueDate, c);//add dueDate field
    c.gridx = 2;
    c.gridy = 4;
    this.add(setDate, c);//add setDate field
    c.gridx = 2;
    c.gridy = 5;
    this.add(setWeighting, c);//add set Weighting field

      

+3


source to share


2 answers


I was able to partially reproduce your problem as follows:

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

public class Foo002 {

   private static final int ROWS = 5;

   private static void createAndShowGui() {
      JPanel assignmentsPanel = new JPanel(new GridBagLayout());
      final JTextArea textarea = new JTextArea(ROWS, 20);

      GridBagConstraints c = new GridBagConstraints();
      int insetGap = 2;
      c.insets = new Insets(insetGap, insetGap, insetGap, insetGap);
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.weightx = 1.0;
      c.weighty = 1.0;
      String[] labels = { "title", "author", "date due", "date set",
            "set weighting" };
      int row = 0;
      for (int i = 0; i < labels.length; i++) {
         JLabel label = new JLabel(labels[i], SwingConstants.CENTER);
         c.gridx = 0;
         c.gridy = i;
         assignmentsPanel.add(label, c);
         c.gridx = 1;
         JTextField textfield = new JTextField(10);
         assignmentsPanel.add(textfield, c);

         label.setPreferredSize(textfield.getPreferredSize());
         row++;
      }
      c.gridx = 0;
      c.gridy = row;
      c.fill = GridBagConstraints.HORIZONTAL;
      Action myAction = new AbstractAction("Fill Area") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 10; i++) {
               sb.append("foo bar bif baz spam\n");
            }
            textarea.setText(sb.toString());
         }
      };
      assignmentsPanel.add(new JButton(myAction), c);
      c.gridx = 1;
      assignmentsPanel.add(new JButton("Button 2"), c);
      row++;

      c.gridx = 0;
      c.gridy = row;
      c.gridwidth = 2;
      c.gridheight = ROWS;

      JScrollPane scrollpane = new JScrollPane(textarea);

      assignmentsPanel.add(scrollpane, c);

      JTabbedPane tabbedPanel = new JTabbedPane();
      tabbedPanel.add("Assignments", assignmentsPanel);
      tabbedPanel.add("Modules", new JPanel());

      JOptionPane.showMessageDialog(null, tabbedPanel, "Foo",
            JOptionPane.PLAIN_MESSAGE);

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

      

Before and after adding text to the JTextArea, it looks like this: enter image description hereenter image description here

And after clicking on the tabs:
enter image description here

But this can be fixed by giving the JScrollPane a vertical scrollbar:



  // JScrollPane scrollpane = new JScrollPane(textarea);
  JScrollPane scrollpane = new JScrollPane(textarea,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

      

What looks like this at startup:
enter image description here

Looks good on Mac OS too:

enter image description here

+3


source


You can try to refrain from using Gridbag and use nested JPanels instead. I allowed to create a short example:



public class LilrooPanel extends JPanel
{
    private static final int GAP = 5;

    public static void main(String[] args){
        JFrame main = new JFrame("Dims");
        JTabbedPane tabbed = new JTabbedPane(); 
        JPanel myPanel = new LilrooPanel();
        tabbed.add("Assignments", myPanel);
        tabbed.add("Modules", new JPanel());
        main.setContentPane(tabbed);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(400, 400);
        main.setLocationRelativeTo(null);
        main.setVisible(true);
    }

    public LilrooPanel(){
        super(new BorderLayout(0, GAP));
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        Box north = new Box(BoxLayout.Y_AXIS);

        north.add(new BorderPanel("Assignment Title", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Author", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Date Due", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Date Set", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));
        north.add(new BorderPanel("Set Weighting", new JTextField()));
        north.add(Box.createRigidArea(new Dimension(0, GAP)));

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(new JButton("Add Assignment"));
        buttonsPanel.add(new JButton("Remove Assignment"));
        north.add(buttonsPanel);

        add(north, BorderLayout.NORTH);

        add(new JScrollPane(new JTable(new Object[][]{}, new Object[]{"ModTitle", "ModId", "Assignments"})));
    }

    private static class BorderPanel extends JPanel
    {
        private static final Dimension LABELS_WIDTH = new Dimension(100, 0);

        public BorderPanel(String label, JComponent right){
            super(new BorderLayout(GAP, 0));
            JLabel jLabel = new JLabel(label);
            jLabel.setPreferredSize(LABELS_WIDTH);
            add(jLabel, BorderLayout.WEST);
            add(right);
        }
    }
}

      

+1


source







All Articles