hi all, I wanted to build a small and easy interface where I have some text and two buttons, one to increase the size of the text and one to decrease it. I haven't built anything as yet, I am still thinking what's the best way to go.
In terms of where the text sits, I thought that perhaps a normal JTextField is probably not a good idea because it will have to grow/shrink with the text and I am not sure that a JTextField has the ability to do that. SHould I use a text area instead? Or what do you guys suggest?

JTextField will fit to the text in it when you pack() its container (more precisely: it updates its preferred size to fit the text, and the layout manager uses that).

Right, so does it mean that everytime I press the + or - button I have to call pack()?

yes, if you wnat your component sizes/posiitons to be updated

Righ, here is what I came up with.
First the most annoying prob, I can't get it to layout properly. I want the txt field on the top and the buttons at the bottom. I purposely used the easiest layout manager because I didn't want to worry about the layout, and yet, here I am, with a broken layout. I know it can be done with that because the textbook has done a similar program with a text field at the top and 2 radio buttons aligned centrally at the bottom with the flowlayout.
Second the jtextfield doesn't grow with the text. At first, I did a very stupid thing, textField.pack(), then I realized that pack needs to be called on a JFrame object, but I don't have any so I called it from the other file, textSizeTest.java, but, almost as I expected it, it doesn't work. I know we said I need to call it everytime the button is pressed, but how do I do it?
SHould I create a JFrame object in the main file and call pack() on it? Any input welcome as usual
Here are the files:

/*TextSize.java*/
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JTextField;

public class TextSize extends JFrame{
    private JTextField textField;//where the txt sits
    private JButton increaseSize;//plus button
    private JButton decreaseSize; //minus button
    /* private Font biggerFont;//bigger font object
    private Font smallerFont;//smaller font object */
    private Font newFontSize;
    private int size;//font size

    public TextSize(){
        super("resizing text");
        setLayout( new FlowLayout() );
        textField = new JTextField("This text will change in size", 25);//create the textfield with text
        textField.setFont( new Font( "Serif", Font.PLAIN, 14));//sets the font size
        add( textField );//adds txt field to JFrame

        increaseSize = new JButton("increase");
        decreaseSize = new JButton("decrease");

        add( increaseSize );//add to JFrame
        add( decreaseSize );

        size = 14;//initial size
        //new font objects
        //biggerFont = new Font( "Serif", Font.PLAIN, )
        //register events
        increaseSize.addActionListener( new ButtonHandler() );
        decreaseSize.addActionListener( new ButtonHandler() );

    }//end of constructor

    //inner class
    private class ButtonHandler implements ActionListener{
        public void actionPerformed( ActionEvent event){

            //identify the event source
            if( event.getSource() == increaseSize ){//if you pressed the + button
                size += 2;//increment
            }
            else if( event.getSource() == decreaseSize ){//if you pressed the - button
                size -= 2;//decrement
            }
            newFontSize = new Font( "Serif", Font.PLAIN, size );//new font object with updated size
            //set the new font size (this step and the preceeding one surely can be combined)
            textField.setFont( newFontSize );
            //textField.pack();//allows the size of the textfield to grow with the txt

        }
    }//inner class


}//end of class



/*TextSizeTest.java*/
import javax.swing.JFrame;
import java.awt.Dimension;
public class TextSizeTest{
    public static void main( String[] args){
        TextSize textSize = new TextSize();
        textSize.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        textSize.setVisible( true );
        Dimension minimumSize = new Dimension( 275, 100 );
        textSize.setMinimumSize( minimumSize );
        textSize.pack();
    }//end of main
}//end of class

TextSize inherits pack() from JFrame, so you can simply call that from within an inner class of TextSize, eg

textField.setFont( newFontSize);
pack();

Ah I see, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.