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
Follow THESE Watch Brands For Your Collection | Kitco
[Music] And Jordan is still alive today. He’s not, uh, you know, he’s still with us. If you had to ask him one question, you had to ask Jordan today, what would that be? Well, I see FP quite a bit. We were at Watch Week in Dubai just a few weeks ago. I w…
Transforming exponential graphs | Mathematics III | High School Math | Khan Academy
We’re told the graph of y = 2^x is shown below. All right, which of the following is the graph of y = 2^(-x) - 5? So there’s two changes here: instead of 2^x, we have 2^(-x) and then we’re not leaving that alone; we then subtract five. So let’s take them…
Peter Lynch: How to Achieve a 30% Return Per Year
I’m amazed how many people own stocks. They would not be able to tell you why they own it. They couldn’t say in a minute or less why they own it. If you really press them down, they’d say the reason I own this is, “The sucker’s going up,” and that’s the o…
The 7 BEST Tax Write-Offs when Investing in Real Estate!
What’s up you guys, it’s Crypto Brand here. Just a prank! Don’t unsubscribe, don’t dislike, don’t leave angry comments. Today, I want to talk about the seven best tax write-offs in real estate that you might not know about. So let’s start here at the very…
Wabi-Sabi | A Japanese Philosophy of Perfect Imperfection
The pursuit of perfection has become the norm in today’s world, where chronic dissatisfaction, burnout, depression, and anxiety reign supreme. We’ve subjected ourselves to unrealistic standards and rigorously chase an ideal that’s impossible to reach. Adv…
BIGGEST Game Collection Ever... and More! -- Mind Blow 5
Yeah, let me get a beer. And how are you feeling that from the bottom? Are you some kind of evil wizard? I’m not drinking your evil wizard. But I forget it, just give me two. Speaking of wizards, let’s see what a little hydrogen peroxide and dry yeast ca…