-1

I am simply trying to change the actual size of my JLabel and JTextField. At the moment I have them on a JPanel with a GridLayout. I just want the label and the textfield right next to one another in each row.

Right now they're so spread out because my JLabel is huge even though I want it very small. I have tried using setPreferredSize() and setSize(), but their sizes do not change. I just want them set to 30.

Can someone please provide some insight into my dilemma?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
amiej33
  • 19
  • 5
  • Please [edit] your question to include a [mcve] (i.e. include your, well formatted, code in your question). The current state of your question will lead to multiple questions being asked in the comments section or your question will be closed as a duplicate. – Jonny Henly Aug 01 '18 at 16:03

2 Answers2

2

Components in a GridLayout are all assigned the same size (as wide as the widest, as tall as the tallest). This can be fixed by using an alternate layout (e.g. GridBagLayout).

I just want the label and the textfield right next to one another in each row.

But we can 'cheat' here in that only the text (and icon) of a JLabel is visible. I.E. it has no visible border. To do that, ensure that the text of the label is aligned to the side closest to the component that it needs to be adjacent to.

But I'd probably go for the GridBagLayout approach. It will only assign as much space as required for the widest label in that column.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

For changing the size JFrame you can write this

myJFrame.setSize(30,30)

For changing the size of fonts in JTextField you can write this:

myJLabel.setFont(new Font("calibri", Font.BOLD, 30))
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    OP stated in the question that `setSize(...)` changes nothing. Changing the font of a `JTextField` may change it's height, but not to a reliably exact amount, especially not from one OS to another. OP should use a layout manager that takes preferred sizes into account, such as `GridBagLayout`. – Jonny Henly Aug 01 '18 at 16:17
  • yes, the class GridBagLayout helps him in arranges the components in a horizontal and vertical manner. But it cant not do anything with the size of the fonts in the JLabel text – Saurabh sinha Aug 01 '18 at 16:20
  • right, but OP did not specify if the font size of the `JLabel` is huge or if the `JLabel` itself is huge. Either way, using a layout manager that respects `setPreferredSize(...)` will *fix* the latter and illuminate the former. – Jonny Henly Aug 01 '18 at 16:25
  • the font size within the jlabel and the jtexfield is currently set to 30, and 20 respectively. – amiej33 Aug 01 '18 at 17:19
  • 1
    `myJLabel.setFont(new Font("calibri", Font.BOLD, 30))` Less fragile as to the particular font (Calibri) being installed is `label.setFont(label.getFont().deriveFont(30f).deriveFont(Font.BOLD))` – Andrew Thompson Sep 13 '18 at 03:32