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
Analyzing an author's purpose | Reading | Khan Academy
Hello readers. Today we are going on a dangerous journey inside the mind of the author. Every piece of text is written for a purpose, and especially in informational texts, every author structures their texts, words, and their ideas with that purpose in m…
Probability with discrete random variable example | Random variables | AP Statistics | Khan Academy
Hugo plans to buy packs of baseball cards until he gets the card of his favorite player, but he only has enough money to buy at most four packs. Suppose that each pack has a probability of 0.2 of containing the card Hugo is hoping for. Let the random vari…
Tips From an Ultramarathoner for Common Trail Injuries | Get Out: A Guide to Adventure
I’m Scott Jurek, and I’m an ultra-marathoner. Today, we’re gonna be talking about injury prevention. I love pushing myself to the edge, and when I’m injured, I’m not able to train; I’m not able to race. Thus, avoiding injuries — but then once they do occu…
Do Robots Deserve Rights? What if Machines Become Conscious?
Imagine a future where your toaster anticipates what kind of toast you want. During the day, it scans the Internet for new and exciting types of toast. Maybe it asks you about your day and wants to chat about new achievements in toast technology. At what …
IKIGAI | A Japanese Philosophy for Finding Purpose
Have you ever asked yourself this question: what is my purpose? With so many opportunities that life has to offer, and all these different jobs and careers, it’s quite difficult to decide what we want to do with our lives. Society demands us to make decis…
A Conversation with Elizabeth Iorns - Advice for Biotech Founders
All right, guys, we’re gonna get started. Sorry for being late. So I have up here Elizabeth Irons. Is it Dr. Elizabeth Irons? No, you’re Professor Elizabeth Irons. So Elizabeth is a cancer biologist by training. You got your PhD in cancer biology from the…