2

I'm trying to set the height of a JTextField. At present its the full size of the displayed JFrame. Any ideas how I set the height to 50?

Edit: Here is the amended code along with screenshot thanks!

ps.png

public class Display extends JFrame {

    private DrawCanvas canvas;
    private JTextField Altitude;
    private JTextField TASpeed;
    private JLabel altButton;
    private int countA = 0;
    private int countS = 0;
    private int Bcount1 = 0;
    public String Ccount = Integer.toString(Bcount1);

    public Display() {
        canvas = new DrawCanvas();
        canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
        canvas.setLayout(new BorderLayout());
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(canvas, BorderLayout.LINE_START);

        //here are the 2 side fields![enter image description here][2]
        Altitude = new JTextField("0", 5);
        Altitude.setHorizontalAlignment(JTextField.CENTER);
        Altitude.setEditable(false);
        Altitude.setOpaque(false);
        Altitude.setFont(Altitude.getFont().deriveFont(25f));

        TASpeed = new JTextField("0", 5);
        TASpeed.setHorizontalAlignment(JTextField.CENTER);
        TASpeed.setEditable(false);
        TASpeed.setOpaque(false);
        TASpeed.setFont(Altitude.getFont().deriveFont(25f));

        altButton = new JLabel();
        altButton.setText(Ccount);

        canvas.add(altButton, BorderLayout.SOUTH);

        canvas.add(Altitude, BorderLayout.LINE_END);
        canvas.add(TASpeed, BorderLayout.LINE_START);

        canvas.add(new JLabel(Ccount), BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("FLIGHT DISPLAY");
        pack();
        setVisible(true);
        requestFocus();
    }

    class DrawCanvas extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(CANVAS_BACKGROUND);

            g.setColor(GROUND_COLOR);
            g.drawString(Ccount, 100, 100);

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Delish
  • 77
  • 1
  • 12
  • 6
    Don't put the text field directly onto a container using a border layout – MadProgrammer Feb 06 '13 at 11:45
  • 3
    for better help sooner post an [SSCCE](http://sscce.org/), please there no idea `1)` whats DrawCanvas(); `2)` for why reason you set `BorderLayout` there `3)` didn't read Oracles tutorial about `LayoutManagers` – mKorbel Feb 06 '13 at 12:09
  • 3
    wrong question - you _never_ set the size of a component ([not even its sizing hints, aka min/pref/maxSize](http://stackoverflow.com/a/7229519/203657)). Instead, use a suitable LayoutManager, f.i. one that respects a component's prefSize always like FlowLayout. Or in other words: you need to learn all about LayoutManagers :-) – kleopatra Feb 06 '13 at 12:32
  • your edit doesnt work as i do need the canvas beceause of the many obj that i have omitted.. – Delish Feb 13 '13 at 16:14

1 Answers1

1

As @kleopatra correctly comments, the JTextField can calculate it's own preferred size based on the platform designer's choice of Font. This example changes the size.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/a/14734937/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField tfCount = new JTextField("42", 8);
        tfCount.setHorizontalAlignment(JTextField.CENTER);
        tfCount.setFont(tfCount.getFont().deriveFont(50f));
        f.add(tfCount, BorderLayout.NORTH);
        f.add(new JPanel() { //placeholder

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        }, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • not exatly what i meant as when you change the BorderLayout position to LINE_END its still the full length of the screen!!! but also thank you as this ansered my problem of setting the text size which was going to be my next question if i hadnt figured it out.. What i need is a JTextField that is only half the size of the screen but center justified.. any Ideas?? and thanks again for your help – Delish Feb 06 '13 at 18:30
  • 1
    Add `tfCount` to a `JPanel` and add the panel to the top. – trashgod Feb 06 '13 at 18:39
  • nopes man no luck.. but i may be doing it wrong.. or is the any way to set the border invisible? – Delish Feb 08 '13 at 13:08
  • 1
    DYM the selection border? In the screenshot above, it's a dusky blue rectangle that varies by L&F. Update your question with your current [sscce](http://sscce.org/) and a [screenshot](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post). – trashgod Feb 08 '13 at 15:00
  • i updated my original post to include a screenshot but could not get the border invisible even after modifing your previous code.. any ides?? – Delish Feb 13 '13 at 12:00
  • 2
    @BenJi: Since you're painting the flight display, paint the numbers whereever you wish them to be. Don't use any JComponents at all. – Gilbert Le Blanc Feb 13 '13 at 14:02
  • 1
    @GilbertLeBlanc has a good idea, for [example](http://stackoverflow.com/a/3256941/230513); also, don't use [setPreferredSize](http://stackoverflow.com/q/7229226/230513); override `getPreferredSize` in `DrawCanvas`. – trashgod Feb 13 '13 at 14:09
  • @Gilbert Le Blanc thanks thats a gud idea but will i still be able to scroll the numbers with the assigned keyboard buttons?? – Delish Feb 13 '13 at 15:44
  • nopes a Jtextfield or Jbutton or anything that will display a number that changes on keypress.. – Delish Feb 13 '13 at 16:12