0

The code inside constructor is:

{
    JTextField txt = new JTextField(10);
    JPanel jp = new JPanel();
    jp.add(txt);
    JButton btn1 = new JButton("change");
    jp.add(btn1);
}

I'm just stuck in my actionPerformed method. What can i code here?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
  • Have you looked at the JTextField class? go there. Google 'JTextField java' You'll find it. Do research before coming to SO. – TheBrenny May 23 '14 at 12:27

4 Answers4

0

I haven't touched Java Swing for years, but have you tried something like this?

public void actionPerformed(ActionEvent evt) {
     int currentCols = txt.getColumns();
     txt.setColumns(currentCols + 5);
}

What I'm trying to do here is increasing the columns of the JTextBox by 5, everytime the button is clicked

I'm not sure if this works, but I hope I helped you

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

You can change size using this method txt.setColumns(yourNewSize);

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

If you go like:

((the JFrames name) if do not extend JFrame).setLayout(null);

Then Im pretty sure you can simply go:

txt.setSize(newSizeX, newSizeY) //in pixels

whenever you press the button.

0

In anonymous block you've to set the size of text field.

 JButton btnChangeWidth = new JButton("Change width");
 btnChangeWidth.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
         textField.setSize(x, y);

     }
 });

don't forget to add the button on container..

for more reference you can visit here http://cshotopics.blogspot.in/2014/05/how-to-change-width-of-textfield.html

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Avnish Tiwary
  • 2,188
  • 22
  • 27
  • i visited your link and yes it really helped me but as you mentioned in your topic what is the meant of EventQueue.invokeLater(new Runnable() {public void run() { // code }); the code inside your main method.. – user3657838 May 25 '14 at 07:26