1

I am new to Java and I want to put a text field of a certain size in a certain coordinate location for a program I am creating. I don't know how to put it in a coordinate location , only in a cardinal location(top, bottom, center, ect). Here is what I have so far:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;
public class For extends JPanel implements PropertyChangeListener {
   private JFormattedTextField amountField;

    private NumberFormat amountFormat;

    public For() {
        super(new BorderLayout());
        setUpFormats();
        amountField = new JFormattedTextField(amountFormat);
        amountField.setValue(3.0);
        amountField.setColumns(10);
        amountField.addPropertyChangeListener("value", this);


        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(amountField);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(fieldPane, BorderLayout.CENTER);
    }
    public void propertyChange(PropertyChangeEvent e) {
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("FormattedTextFieldDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(dim.width, dim.height);  
        frame.add(new For());
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
         UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
    private void setUpFormats() {
        amountFormat = NumberFormat.getNumberInstance();
    }
}
  • Great Mimimal Complete Example, but can you also post an image of how you _want_ this to look. Since you don't have enough rep, you paste a link to an image hosting site – Paul Samsotha Feb 15 '14 at 03:46

2 Answers2

0

You could always use absolute positioning. In other words, not using a layout.

You can accomplish this with:

pane.setLayout(null);

You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Jasin Ali
  • 66
  • 3
  • 1
    [I don't think that would be a good idea](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Josh M Feb 15 '14 at 02:40
0

Because a JFormattedTextField extends java.awt.component you can do all kinds of stuff like that of which you would like to do.

// use whatever values you would like for these.
amountField = new JFormattedTextField(amountFormat);
amountField.setValue(3.0);
amountField.setColumns(10);
amountField.addPropertyChangeListener("value", this);

// Here is the cool stuff. Use whatever values you would like for these.
amountField.setSize(width, height);
amountField.setLocation(xPos, yPos);

// if you want absolute control over the positioning of your components it is best
// not to have a layout.
JPanel fieldPane = new JPanel();
fieldPane.add(amountField);

Hope that helps!

BitNinja
  • 1,477
  • 1
  • 19
  • 25