i writing gui for my program in java, and i want to show some jlabel in multiply jlabels. im using gridlayout.
parts of code:
protected JLabel myLbl = new JLabel("1");
protected JLabel first = new JLabel("2");
protected JLabel second = new Jlabel("3");
in the constructor:
this.first.add(myLbl);
this.second.add(myLbl);
I saw a few questions on the subject, here in StackOverFlow, but I could not find a solution there without creating a duplicate JLabels.. And without creating another layout.
Is there a way to do this without creating a new layout or duplicate JLabels?
Thanks
Related
This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 6 years ago.
I created one JLabel as a heading and customised it, which displayed with no issue. The problem came when I attempted to create a second JLabel, as only the second JLabel would display. When I created a third one, only that would display but not the first and second.
Basically, only the latest JLabel is being displayed. How can I have multiple JLabel's displayed simultaneously.
Here is my code.
public class MainForm extends JFrame {
EmployeeDAO dao = new EmployeeDAO();
private static JLabel label, name;
MainForm()
{
super("Employee Database");
GUI();
}
private void GUI()
{
JLabel label = new JLabel("Enter Employee Information");
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(new Font("Serif", Font.PLAIN, 20));
label.setForeground(Color.red);
add(label);
JLabel name = new JLabel("Name: ");
add(name);
}
}
Usually, when you want to add more than one component to a container (as you're doing with the JLabels in the JFrame) you must decide which layout manager do you want to use so that components are displayed in the positions you want.
You should probably take a look at the tutorials related to laying out components in containers.
You will need to select a Layout Manager, and call setLayout accordingly.
Use .setBounds for each element you are adding to your scene, or use another layout such as grid layout to position your UI elements. The problem stems from when you add a new element, it goes on top of previous one.
how to add a JLabel simple text over/above a JTextField.
I tried many commands as you can see in my code but nothings works.
this is a snapshot of my code
private JPanel createTextPanel() {
int panelWidth = PANEL_SIZE.width;
int panelHeight = PANEL_SIZE.height/3;
Dimension panelSize = new Dimension(panelWidth,panelHeight);
JPanel textPanel = new JPanel();
textPanel.setPreferredSize(panelSize);
//textPanel.setLayout(null);
/* Add text */
JLabel Text_RED = new JLabel();
Text_RED.setText("Red");
//Text_RED = new JLabel("\nRED\n");
//Text_RED.setHorizontalTextPosition(SwingConstants.TOP);
//Text_RED.setVerticalAlignment(SwingConstants.TOP);
Red = new JTextField(3);
//Red.setVerticalAlignment(JTextField.TRAILING );
Red.setLocation(100,100);
//Red.setLocation(50, 50);
JLabel Text_Green = new JLabel("Green");
Green = new JTextField(3);
JLabel Text_Blue = new JLabel("Blue");
Blue = new JTextField(3);
//setLayout(new GridLayout(2,2,10,10));
textPanel.add(Text_RED);
textPanel.add(Red);
textPanel.add(Text_Green);
textPanel.add(Green);
textPanel.add(Text_Blue);
textPanel.add(Blue);
return textPanel;
}
Suggestions:
Don't use null layouts and absolute positioning. While it seems initially that using these tools is the easiest way to create complex GUI's, it's really a newbie fallacy, as the more you understand and use the layout managers, the more you'll find that they make the job of creating GUI's much easier, and the results much more attractive.
Learn about and use the layout managers. Tutorial link.
Consider using a BorderLayout and adding your JLabel BorderLayout.CENTER and the JTextField at BorderLayout.PAGE_END. As a side note, I generally avoid placing JTextFields in a BorderLayout.CENTER position since this will cause horizontal stretching of the field if the GUI changes size, which I don't think is aesthetically pleasing.
I already know that a JLabel can use an ImageIcon in the declaration of the JLabel, like this.
JLabel stick = new JLabel(new ImageIcon("stickPicture.gif"));
My question is, how to assign the picture to the JLabel after declaration. For example (this isn't actually the code that works, this is just the technique that I thought might have worked)
JLabel stick = new JLabel();
stick = new ImageIcon("stickPicture.gif"));
Kind of an odd question, just wondering if it can be done.
Use JLabel#setIcon(Icon).
JLabel stick = new JLabel();
stick.setIcon(new ImageIcon("stickPicture.gif"));
I am trying to align the bottom of 3 JLabels that contain an image. The 3 JLabels are held in one big JPanel.
I found a tutorial about GUI using Java Swing here. But for some reason if i apply the example code (that is given for buttons) it doesn't work on the JLabels or JPanel.
This is the example code from the Oracle website:
button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
Any idea what went wrong? I could send my code, but I thought maybe that would make it too confusing for what might be a simple answer too an easy question for most of you out here.
Thanks in advance.
EDIT:
public class LayoutOef_01 extends JFrame{
JPanel paneel;
JLabel label1, label2, label3;
ImageIcon pic1, pic2, pic3;
Border panelBord, labelBord;
public Layout_01(String titel){
super(titel);
paneel = new JPanel();
pic1 = new ImageIcon("images/simon1.png");
pic2 = new ImageIcon("images/simon2.png");
pic3 = new ImageIcon("images/simon3.png");
label1 = new JLabel(pic1);
label2 = new JLabel(pic2);
label3 = new JLabel(pic3);
paneel.add(label1);
paneel.add(label2);
paneel.add(label3);
panelBoord = BorderFactory.createLineBorder(Color.WHITE, 30);
paneel.setBorder(panelBord);
paneel.setBackground(Color.WHITE);
labelBoord = BorderFactory.createLineBorder(Color.BLACK, 2);
label1.setBorder(labelBord);
label2.setBorder(labelBord);
label3.setBorder(labelBord);
this.getContentPane().add(paneel);
this.pack();
}
public static void main(String[] args) {
Layout_01 lay1 = new LayoutOef_01("Layout_01");
lay1.setVisible(true);
}
}
So i tried placing the following code -in different places- inside the code above, but nothing changes:
label1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label3.setAlignmentY(Component.BOTTOM_ALIGNMENT);
Check this sample: http://www.java2s.com/Code/JavaAPI/java.awt/ComponentBOTTOMALIGNMENT.htm
Remember to:
- set the layout on the panel.
- set the alignment on the button
- add the button to the panel.
which is the best way to add formated string in jframe.
Previously I was trying to add jlabel
If you want to display some text in a window, yes, adding a JLabel to your JFrame is fine.
Just create an instance of the font you want and assign it to the JLabel using setFont.
Here is a code samle (taken from here):
Font font = new Font("Jokerman", Font.PLAIN, 35);
JLabel textLabel = new JLabel(textMessage);
textLabel.setFont(font);