Place two JLabels above each other with JTextfields next to them

I have this code:

button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { panel.add(label, BorderLayout.LINE_START); panel.add(label2, BorderLayout.LINE_START); panel.add(textfield, BorderLayout.LINE_END); panel.add(textfield2, BorderLayout.LINE_END); panel.add(button5); panel.revalidate(); panel.repaint(); label.setText("Geef de basis van de driehoek in cm: "); label2.setText("Geef de hoogte van de driehoek in cm: "); } });

Which corresponds with this screenshot:

Place two JLabels above each other with JTextfields next to them

I want it to actually look like this:

Place two JLabels above each other with JTextfields next to them

I'm new to Java and I know this is probably a dumb question, but I can't figure it out with information on the Internet.

Thanks in advance!

-------------Problems Reply------------

Looking at the code you provided then I see that the top three buttons are not added in this method so I suppose that they are already there:

In that case lets break your GUI down into bits, like so:

Place two JLabels above each other with JTextfields next to them

We can now make those three left over rechtangles. These rectangles are going to be JPanels each having the corresponding components in them:

So:

button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
panel.add(base, BorderLayout.PAGE_START);//top of the page this may interfere with your three buttons I don't know where you are in your layout structure

//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
panel.add(height, BorderLayout.CENTER);

//button5 doesn't need a panel of it's own as it's only one component
panel.add(button5, BorderLayout.PAGE_END);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});

And that should solve your problem, if you have any questions regarding the answer please let me know.

I hope this helps :)

EDIT #1:

Code version 2:

button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel content = new JPanel(new BorderLayout());
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
content.add(base, BorderLayout.PAGE_START

//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
content.add(height, BorderLayout.CENTER);

JPanel forbutton5 = new JPanel();
forbutton5.add(button5);
content.add(forbutton5, BorderLayout.PAGE_END);
panel.add(content);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});

Adding to the answer of Roan: I am not sure what layout manager you are using. Panel has a FlowLayout as default layout manager, and that one puts all components on 1 line. I think you have to use a GridLayout,or a GridbagLayout, and then your components will appear one above the other

Check out the the visual guide to layout managers. What you're probably looking for is the GridBagLayout. The following might be something like what you want:

button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setLayout(new GridBagLayout()) // This should be set earlier
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
panel.add(label, c);
c.gridx = 0;
c.gridy = 2;
panel.add(label2, c);
c.gridx = 1;
c.gridy = 1;
panel.add(textfield, c);
c.gridx = 1;
c.gridy = 2;
panel.add(textfield2, c);
c.gridx = 2;
c.gridy = 1;
panel.add(button5, c);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});

You are using BorderLayout for this you have to use GridBagLayout.

For More : https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Category:java Views:4 Time:2018-11-06

Related post

  • Center two JLabels beneath each other, vertically 2011-08-03

    I've got to create two JLabels, and the should been positioned center and right under each other in the JFrame. I've beeing using the gridbaglayout from swing, but I can't figure out how to do this. terminalLabel = new JLabel("No reader connected!",

  • how to place two divs besides each other 2011-04-26

    On a web page, I have two divs. I would like to place the second div just right to the other one so that they're in a line. --------------Solutions------------- You can use the float CSS style. Set it to left for the first div. The second div will be

  • Two backgrounds upon each other with CSS 2012-02-10

    I have a div that i want to apply two backgrounds to it. Basically I have one small picture that will be repeated all over the div, and another big one (no repeats). I tried to make two divs of the same size layed one upon the other and here is the C

  • How to show images above each others with exact location in HTML? 2013-11-13

    Alright i didn't know what to write in the title but here's my problem.. im implementing a tickets system where people can book a ticket in a specific seat and i wanna put a picture of the bus seats showing which seats are available and which ones is

  • How do I StretchDraw two graphics beside each other on a custom TGraphicControl? 2009-01-21

    I'm writing my Delphi TGraphicControl paint procedure. I create a canvas and I stretchdraw it onto the graphic area. It works well. Then I repeat this with another Stretchdraw onto the graphic area but it is drawn in the area of the first Stretchdraw

  • how to postion two divs above each over 2009-12-02

    Is there any way to start drawing divs from the same point? That means if I add new div and then I add another div, they will appear above each other. Because I want to move them all together depending on the same point. CSS: #num1,#num2{ display : i

  • Put some UILabels above each other and react on dynamic sizes in iPhone 2011-05-22

    I'm starting with iPhone programming. I have a navigationcontroller, to which I push an new view. For this new view I created a new viewcontroller class with XIB file. in Interface Builder I added some UILabels above each other, showing informations.

  • OSMdroid: Markers above each other - how to click 2011-09-02

    I have a big problem with OSMdroid: I have a map with a bunch of markers, some of them are near each other so that the marker drawables are above each other. Now, when the user clicks such a marker "stack" the code from the onClickListener for each m

  • Two UILabels under each other, hide the top one 2012-01-25

    As described in the title, I have an app with two UILabels under each other. If I want to hide to top one programmatically, it's possible with myLabel.hidden = true. If I'm doing this in Android (with myLabel.setVisibility(View.Gone);), the label und

  • How to let images added above each other when dropped by changing the CSS? 2012-12-24

    I'm customizing the CSS for a survey in Qualtrics. I use a Drag and Drop function with pictures. When dropped the pictures are added to a div. The images are added below each other. Is there a way to add the pictures above each other when dropped in

  • how to make two form in each other 2010-12-22

    how can i make two form in each other,i mean when i click a button on a form,another form come up,thanks --------------Solutions------------- Do you mean something like Form2 frm = new Form2(); frm.Show(); Update It seems like you want want Kent's an

  • Compositing 2 videos on top of each other with alpha 2012-01-07

    AVFoundation allows you to "compose" 2 assets (2 videos) as 2 "tracks", just like in Final Cut Pro, for example. The theory says I can have 2 videos on top of each other, with alpha, and see both. Either I'm doing something wrong, or there's a bug so

  • How to define two modules linking each other in OCaml? 2012-01-19

    I know that we can define two types linking each other, for instance: type a = | CC of b and b = | CD of a Does anyone know how to do same thing for two modules? module A = struct type t = | CC of B.t end ?and? B = struct type t = | CD of A.t end ---

  • HTML/CSS Making 3 divs line up next to each other with one with flexible width 2012-02-01

    I'm trying to align 3 divs next to each other, with 2 flexible width and 1 fixed width. Please see the following jsfiddle: http://jsfiddle.net/qyGC5/82/ I've seen this post: Three DIVs next to each other with fluid horizontal width but it doesn't wor

  • How to code that two images touches each other? 2012-03-18

    I'm new to android. i'm making a game in which if two image touch each other the points are gained. now i don't know how to find that how to code it. can anybody help? --------------Solutions------------- If you're going to use the Canvas class to dr

  • view two worksheets alongside each other 2012-10-05

    How do I show two worksheets alonside each other in Excel Starter? It used to be simple in Works by right clicking on the desktop. --------------Solutions------------- Excel Starter is very limited. The command to view sheets side by side is not avai

  • How do I place two tables adjacent to each other with no white space between them? 2013-12-20

    I currently print 4 long thin tables on a page (hundreds of pages) and then cut them out on the border lines. That means 8 vertical cuts per page because the tables are separated by about 1/8 inch of white space. I cannot get the tables to move any c

  • How to make two arrays correspond with each other with php? 2011-09-18

    How can I make two arrays correspond with each other? For instance, this the 1st-months array I generated, Array ( [0] => Sep [1] => Oct [2] => Nov [3] => Dec [4] => Jan [5] => Feb [6] => Mar [7] => Apr [8] => May [9] =>

  • Regex get content between two pipes AND return a space where two pipes are next to each other with no spaces 2011-06-01

    How can I get all content between pipes and return a space where it comes across two pipes next to each other? An example string and desired output is: |test1| test2|test3 || test 4 | Result1: "test1" Result2: "test2" Result3: "test3" Result4: " " Re

  • Silverlight for Windows phone 7 - two text blocks after each other with NO linebreak 2011-03-04

    I'm relatively new to Windows Phone 7 development, so bear with me on this. I've two text blocks, each with binding to two different properties, like this: <TextBlock Text="{Binding ID}" /> <TextBlock Text="{Binding Title}"/> So when I de

  • How two laptops can git push/pull to each other with no internet 2011-05-10

    Say my co-hacker and I find ourselves on a desert island. Normally we push and pull to github to sync with each other. How would you recommend we do so when we have no connection to the outside world? (User @bee and I actually have that problem as I

  • Determine if two rectangles overlap each other? 2008-11-20

    I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges w

  • C++: Two classes needing each other 2010-07-06

    I'm making a game and I have a class called Man and a class called Block in their code they both need each other, but they're in seperate files. Is there a way to "predefine" a class? Like Objective-C's @class macro? --------------Solutions----------

  • grep two words near each other 2011-03-06

    Say I have a line in a file "This is perhaps the easiest place to add new functionality." and I want to grep two words close to each other. I do grep -ERHn "\beasiest\W+(?:\w+\W+){1,6}?place\b" * that works and gives me the line. But when I do grep -

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.221 (s). 12 q(s)