• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

how to place a JLabel and JTextField

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working bit by bit on my contacts program, and am trying to figure out how to place a JLabel and a JTextField next to one another2 as well as place the field I want (Name, phone, address, etc) aligned to the left  Can anyone help?

Here's a screen shot so far.  The Field and label change being next to one another or on top depending on how small the window is reduced.



Screenshot-from-2022-07-13-20-53-21.png
[Thumbnail for Screenshot-from-2022-07-13-20-53-21.png]
 
Marshal
Posts: 4812
604
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using a Layout Manager?
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I am -- it's flow layout, the default one for a JPanel.  I'm just still learning these things, and am still getting used to the API's.  My apologies if  my question is too obvious.  I take it that there's something in the layout Manager to do this, but I thought there might be other ways to put the components where I want them, rather than have the Layout Manager take care of it, if I'm saying that correctly.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I take it that there's something in the layout Manager to do this



There are different layout managers, each designed to provide different layouts. You can also nest panels with different layout managers.

Read the tutorial and use the appropriate layout manager for your requirements.

Your verbal description is too vague for us to provide a complete suggestion.
 
Sheriff
Posts: 28411
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would strongly recommend reading Oracle's tutorial about layout managers -- in fact I see Campbell has already linked to it. Download the example code from the tutorial and start playing around with it, keeping your requirements in the back of your mind so you can notice what might work for you.

Also... I mostly use BoxLayout these days because it's more flexible than the Java 1.0 layout managers. But for complex layouts I still find myself trying to referee the battling layout managers and it can take a long time to bring order and predictability to the layout.

Back in the days when Swing still mattered there were people who tried to produce more useful layout managers, and there were several available on line. I still use DesignGridLayout because it's reasonably easy to produce layouts which it sounds like you're describing. You may still be able to track down the documentation and jar file for it, although it was written over a decade ago.
 
Bartender
Posts: 11105
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a form style layout I find nothing beats a Grid Bag Layout. The problem with it is  without any help it makes the code you have to write unmanageable and unreadable. There's a wrapper class called "GBC" to the rescue which it makes it totally manageable. You'd still want to read up on Grid Bag Layout so you have an idea of what the GBC class is doing for you. With this layout you can imagine you've got a piece of graph paper that you are plopping components on to. I've put together a small sample for you. And you can search for GBC on this  site and copy it.


 
Carey Brown
Bartender
Posts: 11105
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use a layout manager.
If it's not a school or university project and you are allowed to use whatever layout manager you want, I recommend you to use MigLayout.
Very easy to use and very flexible.

https://www.miglayout.com/
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The https://coderanch.com/wiki/659817/Swing-FAQ links to a couple more alternatives, like GridLayout2 (basically GridLayout with variable row and column widths and heights - that was often sufficient for my purposes) and SGLayout.
 
Bartender
Posts: 5639
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if it is really complicated, you can always use a GUI-builder.
 
Marshal
Posts: 80764
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . Campbell has already linked to it. . . .

Have I? I think that was somebody else: Ron.

Carey Brown wrote:. . . There's a wrapper class called "GBC" . . .

i like GBC; it was written by Cay Horstmann and I have written my own versions of Hortmann's class several times.

Piet Souris wrote:. . . you can always use a GUI-builder.

I prefer to restrict GUI builders to experienced people; I think beginners should always set out the layout by hand.
 
Carey Brown
Bartender
Posts: 11105
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going through  the demos in the tutorials -- for example, I downloaded the code re: GridLayout.  I tried to compile it, and got this message:

Note: GridLayoutDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Was wondering if anyone could tell me what this means.  I can't run the  demo.   The tutorials say this is for Java 8, so maybe that has something to do with it.  Any suggestions?

I thought I had posted this reply, but it isn't there for some reason, so my apologies if I'm posting it again.
 
Carey Brown
Bartender
Posts: 11105
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point you should never use any Java version older than 8. Java 8 had so many core changes that you should be using that at the very least. The recommended thing to do is use the most current LTS (Long Term Support) version, which right now is 17.

Note that 8 was released in 2014 - that's ancient.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't mean that I was using Java 8.  I meant the tutorial that covered this topic was for java 8.   I'm on Java 18 and I'm getting this message.  Could there be something in the code that could cause this?
 
Ron McLeod
Marshal
Posts: 4812
604
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is probably due to one of the lines like this which performing some unchecked casting:I would ignore the warning (or hide it using the @SuppressWarnings("unchecked") annotation) and not let it be a distraction getting in the way of the goal of learning more about Swing and layout managers.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic