How to determine the size of a JTextField?


I'm doing a program involving the Pythagorean theorem, and wanted to leave a triangle in the middle, with three JTextField around three sides of it. I did it, but the problem is that when I create the JTextField and add it to the desired container, it is immense. It is a very large white rectangle that when I click to enter, get a small letter. So, my question is: How can I change the size of this JTextField to decrease it and avoid unnecessary sizes? P.S:.'m Using Swing and AWT library for free! Example of code:

Container content = window.getContentPane(); Contaiiner grid = new JPanel(); grid.setLayout(new GridLayout(3,3)); grid.add(new JTextField());

Well, this simple code, gives to you one example of the JTextField, when i executes than (with main method and more...) the JTextField is very very big, so, i want to decrease it.


Admittedly, it involves some guesses about your intention, but here is an example of what I think that you want to achieve:

<img alt="Triangle Text Fields" class="b-lazy" data-src="https://i.stack.imgur.com/Kvd1q.png" data-original="https://i.stack.imgur.com/Kvd1q.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

It's a program that contains a panel with some points, and between each pair of points, it places a text field. The text fields are arranged so that their upper left corner remains at the center of the respective edge. When the points are dragged with the mouse, the locations of the text fields are updated accordingly. (In this example, the text fields contain the <em>distance</em> between the points - that is, the edge lengths - but this is just for illustration).

import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class TriangleTextFieldsTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TriangleTextFieldsTest(); } }); } public TriangleTextFieldsTest() { JFrame frame = new JFrame(); frame.getContentPane().add(new TriangleTextFieldsPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,800); frame.setLocationRelativeTo(null); frame.setVisible(true); } } class TriangleTextFieldsPanel extends JPanel implements MouseListener, MouseMotionListener { private final List<Point2D> points; private final List<JTextField> textFields; private Point2D draggedPoint = null; TriangleTextFieldsPanel() { super(null); points = new ArrayList<Point2D>(); points.add(new Point2D.Double(250,250)); points.add(new Point2D.Double(750,250)); points.add(new Point2D.Double(500,500)); textFields = new ArrayList<JTextField>(); for (int i=0; i<points.size(); i++) { JTextField t = new JTextField(10); textFields.add(t); add(t); } updateTextFieldPositions(); updateTextFieldContents(); addMouseListener(this); addMouseMotionListener(this); } private void updateTextFieldPositions() { for (int i=0; i<points.size(); i++) { Point2D p0 = points.get(i); Point2D p1 = points.get((i+1)%points.size()); JTextField textField = textFields.get(i); int cx = (int)(0.5 * (p1.getX() + p0.getX())); int cy = (int)(0.5 * (p1.getY() + p0.getY())); Dimension d = textField.getPreferredSize(); textField.setBounds(cx, cy, d.width, d.height); } } private void updateTextFieldContents() { for (int i=0; i<points.size(); i++) { Point2D p0 = points.get(i); Point2D p1 = points.get((i+1)%points.size()); JTextField textField = textFields.get(i); double d = p0.distance(p1); textField.setText(String.format("%.2f", d)); } } @Override protected void paintComponent(Graphics gr) { super.paintComponent(gr); Graphics2D g = (Graphics2D)gr; g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.BLUE); drawPoints(g, points); g.setColor(Color.BLACK); for (int i=0; i<points.size(); i++) { Point2D p0 = points.get(i); Point2D p1 = points.get((i+1)%points.size()); g.draw(new Line2D.Double(p0,p1)); } } static void drawPoints(Graphics2D g, List<Point2D> points) { double r = 3; for (Point2D point : points) { double x = point.getX(); double y = point.getY(); g.fill(new Ellipse2D.Double( x-r, y-r, r+r, r+r)); } } @Override public void mouseDragged(MouseEvent e) { if (draggedPoint != null) { draggedPoint.setLocation(e.getPoint()); updateTextFieldPositions(); updateTextFieldContents(); repaint(); } } @Override public void mouseMoved(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { draggedPoint = null; double thresholdSquared = 10*10; double minDs = Double.MAX_VALUE; for (Point2D point : points) { double ds = point.distanceSq(e.getPoint()); if (ds < thresholdSquared && ds < minDs) { minDs = ds; draggedPoint = point; } } } @Override public void mouseReleased(MouseEvent e) { draggedPoint = null; } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }


  • Get latest records in a data frame based on time stamp with condition
  • Put elements of a 1D vector into a 3D matrix using another matrix of positions
  • pg_dump serial datatype issues
  • Multiplying two matrices in Java
  • Pandas multi-index subtract from value based on value in other column
  • creating password field in oracle
  • JS object key sequence
  • How to control Trigger state (Pause, Play) using code (not just buttons)
  • How to replace TouchesBegan with UIGestureRecognizer
  • custom string delimiters stringtemplate-4
  • How do you keep a running instance for Google App Engine
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • my tic-tac-toe program in matlab does not work [closed]
  • jQuery - resize an elements height to match window without refreshing, on window resize
  • Plotting densities in R
  • Update Google Maps traffic layer without page reloading
  • Selenium to click on a javascript button corresponding to a text
  • How to remove a SwiftyJSON element?
  • Query to find the duplicates between the name and number in table
  • Installing iPhone App to iPhone
  • Jackson Parser: ignore deserializing for type mismatch
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Avoid links criss cross / overlap in d3.js using force layout
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Retrieving value from sql ExecuteScalar()
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to check if every primary key value is being referenced as foreign key in another table
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • How to format a variable of double type
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • python regex in pyparsing
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • how does django model after text[] in postgresql [duplicate]
  • Checking variable from a different class in C#
  • java string with new operator and a literal