0

I want to put value in txtf1 at output screen and get it. How can we put value on text field on output screen?

import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class demog extends JPanel implements ActionListener{

private TextField textf, txtf1;

public void jhand(){
textf = new TextField();
    textf.setSize(40, 40);
    textf.setText("20");
    textf.setEditable(false);
    textf.setBackground(Color.WHITE);
    textf.setForeground(Color.BLACK);
    //textf.setHorizontalAlignment(SwingConstants.CENTER);
    textf.setLocation(15, 15);
    //textf.addActionListener(this);
    txtf1 = new TextField();
    txtf1.setSize(40, 40);
    txtf1.getText();
    txtf1.setEditable(false);
    txtf1.setBackground(Color.WHITE);
    txtf1.setForeground(Color.BLACK);
    //txtf1.setHorizontalAlignment(SwingConstants.CENTER);
    txtf1.setLocation(50, 50);
    JFrame frame = new JFrame("demo");
    JPanel p = new JPanel();
    p.setOpaque(true);
      p.setBackground(Color.WHITE);
      p.setLayout(null);
      frame.setContentPane(p);
      frame.setSize(500,500);
        frame.setVisible(true);
        p.add(textf);
        p.add(txtf1);
}

public void actionPerformed(ActionEvent evt) {
    String text = textf.getText();
    System.out.println(text);
}

public static void main(String... args){
    demog g = new demog();
    g.jhand();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rahu
  • 1
  • 1
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Jul 02 '15 at 07:18
  • `p.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jul 02 '15 at 07:21
  • `public class demog extends JPanel implements ActionListener{ private TextField textf, txtf1; ..` Don't mix Swing and AWT components, use Swing entirely. So that should be `public class demog extends JPanel implements ActionListener{ private JTextField textf, txtf1; ..` – Andrew Thompson Jul 02 '15 at 07:24
  • but i need to put value on output screen in txtf1 – Rahu Jul 06 '15 at 07:46

1 Answers1

1

You have to change some of your code in order to work. You had some problem in your code which I resolved them for you in the following code. See the comments to learn some in swing ;-)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

// Use upper Case in the start of you class names:
public class Demog extends JPanel implements ActionListener {

    private JTextField textf, txtf1;

    public Demog() {
        jhand();
    }

    public void jhand() {
        setLayout(new FlowLayout()); // Always set the layout before you add components

        // you can use null layout, but you have to use setBounds() method 
        //      for placing the components. For an advanced layout see the 
        //      tutorials for GridBagLayout and mixing layouts with each other.

        textf = new JTextField(); // Do not mix AWT component with 
                                  //    Swing (J components. See the packages)
        //textf.setSize(40, 40); // Use setPreferredSize instead
        textf.setPreferredSize(new Dimension(40, 40));
        textf.setText("20");
        textf.setEditable(false); // Text fields are for getting data from user
                                  //    If you need to show something to user
                                  //    use JLabel instead.
        textf.setBackground(Color.WHITE);
        textf.setForeground(Color.BLACK);
        add(textf);

        txtf1 = new JTextField();
        //txtf1.setSize(40, 40); Use setPreferredSize instead
        txtf1.setPreferredSize(new Dimension(40, 40));
        txtf1.getText();
        txtf1.setEditable(false);
        txtf1.setBackground(Color.WHITE);
        txtf1.setForeground(Color.BLACK);
        add(txtf1);

        JButton b = new JButton("Click ME!");
        b.addActionListener(this);
        add(b);
    }

    public void actionPerformed(ActionEvent evt) {
        String text = textf.getText();
        JOptionPane.showMessageDialog(Demog.this, "\"textf\" text is: "+text);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Demog p = new Demog();
        p.setBackground(Color.WHITE);
        frame.setContentPane(p);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • 1
    `txtf1.setSize(40, 40);` or ` txtf1.setPreferredSize(new Dimension(40, 40));` == `textf = new JTextField(12-15);` – mKorbel Jul 02 '15 at 11:54