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
Could this be the oldest known human burial? #archaeology
So this is the Superman crawl. It’s an opening less than 10 inches wide where you literally have to make a Superman pose just to make it through. If you follow the cape through the Dragon’s Back chamber and then go down to shoot, yeah, that’s you. Superm…
The Court in Action | AP US Government and Politics | Khan Academy
Of the three branches of the US government, the judicial branch is the one that is least bound by public opinion. Supreme Court justices aren’t elected; they’re appointed, and they serve for life or until they decide to retire. Usually, justices serve on …
THE DOWNFALL OF CREDIT CARDS | HOW TO PREPARE
What’s up you guys? It’s Graham here. So as I’m sure many of you know by now, I am a huge proponent and believer in credit cards. I think they’re a great way to leverage your money, get purchase protection, get cash back, collect points, travel for free, …
How Far Can We Go? Limits of Humanity (Old Version – Watch the New One)
Is there a border we will never cross? Are there places we will never reach, no matter how hard we try? Turns out there are. Even with science fiction technology, we are trapped in our pocket of the universe. How can that be? And how far can we go? We li…
See Why This Roller Skating Girl Squad Is the First of Its Kind | Short Film Showcase
The thing that I love about Derby is I forget everything around me when I’m playing. Every one of us kind of have nicknames, so my nickname around there be as shiny tiny, because I’m tiny, I’m short, and I had this like bling. Users like shiny tiny, and m…
What VCs Look for When Investing in Bio and Healthcare
Right, so welcome back. In this next panel features bio and Healthcare investors from Andreessen Horowitz, Coastal Adventures, and Ben Rock. They are some of the most respected firms out there. So, before we bring them up on stage, I wanted to introduce y…