yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java GUI Lesson 3 | JTextFields and Absolute Positioning


4m read
·Nov 3, 2024

That's 101. Welcome to your third Java GUI tutorial! Today I'm gonna be talking about JLabels, which is another component, and positioning your components using absolute positioning.

So first I'm gonna go here, I'm gonna import java.awt.*; and javax.swing.*; Alright, and make this extend JFrame. The first thing I want to do is create two JTextFields. One is going to be where we can write things, and one is going to be uneditable; it's just gonna be there, kind of like a JLabel.

So, private static JTextField canChange; and private static JTextField cantChange. Yep, there we go! I didn't make a constructor. Went over all this in the last tutorial. And then I'm still gonna do canChange equals new JTextField, and it takes that string -- what you want to put in there, and that would be the default text that would be in there by default. Something doesn't have to be in there by default, but in canChange, I'm just gonna put "default text," and in cantChange I'm gonna put "I can't."

Alright, so the next thing we want to do is, because we're going to be positioning these using absolute positioning, we have to do this: we have to set layout to null. There's no layout! We're gonna be creating our own layout by setting the X and Y coordinates of everything.

By the way, the X and Y coordinates, the X and Y, is the top left corner of your thing. So if I’d like a rectangle and I did 0-0, that would make it the top-left corner. 0, 0 would be the top-left corner of my rectangle. X's start from zero and go up here, and Y starts from zero up here. I mean, X starts from zero and goes up, and Y starts from zero and increases as you go down. So it's kind of a little different than a regular graph.

Alright, so next thing I wanna do is make two dimensions. I'll call it Dimension size1 = canChange.getPreferredSize(); What this means is a dimension has things like .width and .height. If I get the preferred size of canChange, I can call like, it's kind of like its default width and height. This allows me to get like a default width and height for it, but I can code my own width and height.

I'll show how to do that in a sec. Dimension size2 = cantChange.getPreferredSize(); No, not what I want! So I get preferred size, and here's where we can actually -- the method to position with absolute is setBounds. Therefore, cantChange.setBounds(); This takes four things: the first one is the x-coordinate, second is the y-coordinate, third is the width, and the fourth is the height.

So let's say I'll put it at: alright, so this thing's gonna be at 350. I’ll put it like a hundred in, and like 20 down. And size1.getWidth() and size1.getHeight(). If I wanted to, I could just put a number in there, like 120, and then it would be that long; and like 30, and it would be that tall.

But I'm not, just letting you know, 'cause I just want to show you about the dimension thing. I setBounds, and I'll put that at: a hundred in, and like 55 down, and size2.getWidth() and size2.getHeight(). And I spelled width wrong; it’s fine.

The next thing we're gonna do is make it so cantChange you actually can't change 'cause right now this is normal. So I do cantChange.setEditable(), and that takes a boolean true or false, whether or not you're gonna edit it. I’ll make that editable false. I don’t have to do canChange.setEditable(true) because it's editable by default.

Now, I just have to add these: add(canChange); add(cantChange); and I guess I’ll just set the size and all that now: setSize(350, 300); setTitle(); and I’ll set that as JTextFields. SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).

And yeah, that's about it. Now in here, I'm just gonna make the second object: secondX = new Second(); setVisible(true). Alright, so what we did is we made these two text fields, we set the default text for them, we said our layout to null so we can position them with X and Y coordinates as opposed to using a layout manager, which I’m not even going to go over because I never use them anyway.

It's just a bunch of different default layouts. The way you made these dimensions, so we can get like the default size for them, and then we use the setBounds method to create where they are, and just use the default width and height. Set editable to false so you can't change this one; you can change that one. We added them, set the size, set the title, and made it so it exits when you press X.

Here, we just put it in our main method so it can actually run. I will show you this right now: here we go! JTextFields "default text" and "can't change."

I'm clicking, and I'm unable to delete, and I cannot change it. This I can type anything I want in here, and this I can, and it closes when I press exit.

So that's all for your third Java GUI tutorial! Thank you for watching Mac Hits One and Walk 101! Subscribe, and goodbye!

More Articles

View All
Fire Starter Extraordinaire | Dirty Rotten Survival
Now we’ll find out if Dave really is the fire-making Maestro he claims to be. Depending on your environment and your resources, a very, very difficult challenge. I’m going to use everything that Dick and Johnny have in their kits, as well as what I have i…
Why I won't be getting the Apple Credit Card...
Alright guys, so we got to have a heart-to-heart and talk about the Apple credit card and why I won’t be getting one. They know a lot of you have asked for my opinion when it comes to this, especially coming from the perspective of someone who’s a bit of …
15 Steps To Force Your Way Out Of Poverty
Poor people work just as hard, if not harder, than those born into wealth. However, that hard work rarely translates into wealth because poverty, as a system, is designed for survival, not growth. You have just enough to get by until tomorrow but never en…
Behind the Scenes with Geoffrey Rush | Genius
[music playing] Hello, my name is Geoffrey Rush and I play Albert Einstein the older. I was four when Albert Einstein died. So everything I know about him is more from the legend that he became because he was almost like a cult figure in a way. Einstein …
The New Sourcerer
Hey guys, this is Mac Edson, and today is going to be a video on the latest version of Sorcerer. So before I admit that Sorcerer didn’t do the best stuff in the world, and it was pretty plain and simple, but now I added a few great features to Sorcerer an…
How I tricked my brain to like doing hard things
So for the majority of my life, I struggled to go to the gym consistently. Even though the gym has always been a part of my life to some degree, I grew up playing hockey, and all my brothers played hockey and went to the gym. So going to the gym was alway…