0

Im making a simple java swing program but i'm having trouble with the size and positions of my components. I have three Jlabels and three JtextFields. The jlabels are overlapping so only the last one i add is showing. The textfields are showing up very small.(almost look like lines.) Also, everything appears on one line and i would like each component on its own line.

CODE:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class setCustoms {
static JPanel panel = new JPanel();
static JFrame frame2 = new JFrame("Settings");

public static void makeWindow(){
    frame2.setVisible(true);
    frame2.add(panel);
    frame2.setBounds(500, 300, 500, 200);

    JLabel app1l = new JLabel("");
    JLabel app2l = new JLabel("");
    JLabel app3l = new JLabel("");
    JLabel site1l = new JLabel("");
    JLabel site2l = new JLabel("");
    JLabel site3l = new JLabel("");

    panel.add(app1l);
    app1l.setText("Set  Application ONE name and path");
    JTextField app1t = new JTextField();
    app1t.setBounds(100, 100, 100, 25);
    app1t.addActionListener(new Action());
    panel.add(app1t);

    panel.add(app2l);
    app1l.setText("Set  Application TWO name and path");
    JTextField app2t = new JTextField();
    app2t.addActionListener(new Action());
    panel.add(app2t);


    panel.add(app3l);
    app1l.setText("Set  Application THREE name and path");
    JTextField app3t = new JTextField();
    app3t.addActionListener(new Action());
    panel.add(app3t);

}
static class Action implements ActionListener{
    public void actionPerformed (ActionEvent e){


    }
}

}

EDIT: althoguh i do have code to set bounds for certain items, it does not work

user2947797
  • 33
  • 1
  • 1
  • 6

3 Answers3

0

The JPanel uses a FlowLayout by default, this lays out each component next to each in a line.

setBounds has no effect as the panel is under the control of a layout manager, which is making it's own decisions about how best to place and size the components

Try using a different layout manager.

Take a look at Laying out components within a container for more details and ideas

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You need to use a LayoutManager (A Visual Guide to Layout Managers) to organize your components.

A example with GridLayout:

1) Configure GridLayout in your JFrame

// with 3 rows and 2 columns
static JPanel panel = new JPanel(new GridLayout(3, 2));

2) Create and add the elements in order left-to-right-to-down

JLabel app1l = new JLabel("");
app1l.setText("Set  Application ONE name and path");
panel.add(app1l);

JTextField app1t = new JTextField();
panel.add(app1t);

// (...)
//keep adding your elements in order

3) Pack your JFrame to organize all elements added

frame3.pack(); 
Pedro Vítor
  • 191
  • 1
  • 6
0
  • Everything appears on one line and i would like each component on its own line.

As MadProgrammer pointed out, the elements appear in a single row because you're not setting the layout manager for panel. Following Pedro Vítor's suggestion, you could use a grid layout. For example, for a single-column layout you can do:

static JPanel panel = new JPanel (new GridLayout (0,1));

where 0 indicates no rows and 1 is the number of columns.

  • The jlabels are overlapping

Take a closer look at your code. What's actually happenning is that you're setting the text only for app1l all the time. The other labels are there. It's just that they have no text. Probably the product of copy and paste ;)

  • A note on practices

As Pedro Vítor mentioned, you should make a habit of adding elements in order. It'll save you headaches when you go back to the code. Additionally, I'd advise you to make the frame visible only after you've added ALL the components. Otherwise, it is not guaranteed that all those components will be rendered. Take a look at this question: Why shouldn't I call setVisible(true) before adding components?

So, taking all into account, your code should look something like this:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class setCustoms {
static JPanel panel = new JPanel(new GridLayout(0,1));
static JFrame frame2 = new JFrame("Settings");

public static void main (String[] args){
    JLabel app1l = new JLabel("");
    JLabel app2l = new JLabel("");
    JLabel app3l = new JLabel("");

    app1l.setText("Set  Application ONE name and path");
    panel.add(app1l);
    JTextField app1t = new JTextField();
    app1t.setBounds(100, 100, 100, 25);
    app1t.addActionListener(new Action());
    panel.add(app1t);

    app2l.setText("Set  Application TWO name and path");
    panel.add(app2l);
    JTextField app2t = new JTextField();
    app2t.addActionListener(new Action());
    panel.add(app2t);

    app3l.setText("Set  Application THREE name and path");
    panel.add(app3l);
    JTextField app3t = new JTextField();
    app3t.addActionListener(new Action());
    panel.add(app3t);

    frame2.add(panel);
    frame2.setBounds(500, 300, 500, 200);
    frame2.setVisible(true);   
}

static class Action implements ActionListener{
    public void actionPerformed (ActionEvent e){


    }
}

}

Hope that helps.

Community
  • 1
  • 1
HQCasanova
  • 1,158
  • 1
  • 10
  • 15