0

I want to know if it is possible to create a Java class that allows to set the height of all the JTextFields in a Java frame, instead of manually change.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You could use reflection – Rakesh Mar 05 '13 at 09:28
  • just to emphasize @Madprogrammer - *never-ever* do any manual sizing/locating, that's the exclusive task of the LayoutManager – kleopatra Mar 05 '13 at 10:17
  • possible duplicate of [How to set the height and the width of a textfield in Java?](http://stackoverflow.com/questions/14805124/how-to-set-the-height-and-the-width-of-a-textfield-in-java) – Andrew Thompson Mar 05 '13 at 10:40

2 Answers2

2

It depends on the structure of the layout.

Basically you need to walk the component hierarcy of the frame (and it's containers) looking for instances of JTextField

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WalkComponentTree {

    public static void main(String[] args) {
        new WalkComponentTree();
    }

    public WalkComponentTree() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                List<JTextField> fields = getTextFields(frame.getContentPane());
            }

        });
    }

    public List<JTextField> getTextFields(Container container) {
        List<JTextField> fields = new ArrayList<JTextField>(25);
        for (Component comp : container.getComponents()) {
            if (comp instanceof JTextField) {
                fields.add((JTextField)comp);
            } else if (comp instanceof Container) {
                fields.addAll(getTextFields((Container)comp));
            }
        }
        return fields;
    }

    public class TestPane extends JPanel {
        public TestPane() {
            add(new JTextField());
        }
    }
}

I should note, that the modification of any component's size like this is probably a significantly bad idea. Apart from the fact that there might be (multiple) layout managers involved, it will significantly alter the current look of the UI and may cause more problems then it's worth (for example, this will pick up editable combo boxes ;))

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You could modify the look and feel to achieve this: Modifying the Look and Feel

Kai
  • 38,985
  • 14
  • 88
  • 103