Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resize the textfield in runtime

I changed the code again. But now when I run the code, The text box is very small. Only whn I choose either of the left, center or the right alignment, will the text box size change. Where did I make the mistake.

I have written the below program. When I click on the left, right or the center button, the program should also read the value in the column size textbox and then automatically resize the message textfield. BUt I cannot seem to do it. Any insight will be greatly appreciated. package workingwithjtextfields;

   package workingwithjtextfields;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;

public class WorkingwithJTextFields extends JFrame
{
   // private int size = 100;
    private JTextField jtfMessage = new JTextField(100);

    private JTextField jtfColumn = new JTextField(5);
    private JRadioButton jrbLeft,jrbCenter,jrbRight;

    public static void main(String[] args) //Main program begins here.
    {
        JFrame frame = new WorkingwithJTextFields();//Instantiating an object.
        frame.setTitle("Exercise 17.11");//Setting the frame title.
        frame.setSize(470,110);//Setting the size.
        frame.setLocationRelativeTo(null);//Setting the location.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Default closing options.
        frame.setVisible(true);//Setting visibility to true.
    }//End of main program

    public WorkingwithJTextFields()
    {

   // jtfMessage.setColumns(100);
    final JPanel parent = new JPanel();
    parent.setLayout(new GridLayout(2,1,3,3));

    final JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout(FlowLayout.LEFT,30,0));

    p1.add(new JLabel("TextField",SwingConstants.CENTER));

    jtfMessage= new JTextField("Type anything",SwingConstants.RIGHT);
    jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
    p1.add(jtfMessage);

    parent.add(p1);

    JPanel jpRadioButtons = new JPanel();
    jpRadioButtons.setLayout(new GridLayout(1,3));
    jpRadioButtons.add(jrbLeft= new JRadioButton("Left"));
    jpRadioButtons.add(jrbCenter = new JRadioButton("Center"));
    jpRadioButtons.add(jrbRight = new JRadioButton("Right"));
    jpRadioButtons.setBorder(new TitledBorder("Horizontal Border"));

    final JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(1,2,1,1));

    p2.add(jpRadioButtons);

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(1,1,1,1));
    p3.add(new JLabel("Column Size"));

    jtfColumn= new JTextField("60",SwingConstants.RIGHT);
    jtfColumn.setHorizontalAlignment(SwingConstants.CENTER);

    p3.add(jtfColumn);      
    Border lineBorder = new LineBorder(Color.LIGHT_GRAY,1);
    p3.setBorder(lineBorder);
    p2.add(p3);
    parent.add(p2);
    add(parent);



    jrbLeft.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    jtfMessage.setHorizontalAlignment(SwingConstants.LEFT);
                    jrbCenter.setSelected(false);
                    jrbRight.setSelected(false);
                    jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                 //   p1.revalidate();
                   // p1.repaint();
                }
            }
            );

    jrbCenter.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
                    jrbLeft.setSelected(false);
                    jrbRight.setSelected(false);
                    jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                }
            }
            );

    jrbRight.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    jtfMessage.setHorizontalAlignment(SwingConstants.RIGHT);
                    jrbCenter.setSelected(false);
                    jrbLeft.setSelected(false);
                    jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                }

            }
            );

    }

}
like image 493
user2918968 Avatar asked Apr 17 '26 03:04

user2918968


1 Answers

The main problem is with GridLayout.

GridLayout, by design, gives equal width and height to the components in the column/width based on the available space of the parent container.

Instead, try using something like FlowLayout or GridBagLayout which work with the components preferred size instead

like image 57
MadProgrammer Avatar answered Apr 18 '26 17:04

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!