import java.awt.*;
import javax.swing.*;
public class JFrameGUI extends JFrame
{
JLabel item1;
public JFrameGUI(int l, int b , String Title)
{
setTitle(Title);
setLayout(new FlowLayout());
setSize(l, b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
item1 = new JLabel("This is a Sentence.");
item1.setToolTipText("This is gonna show up on hover.");
add(item1);
}
public static void main(String[] args)
{
JFrameGUI g = new JFrameGUI(1280,720,"The End Of The Line");
JPanel p = new JPanel();
p.setBackground(Color.BLUE);
g.add(p);
}
}
When I execute this , all i get is a tiny Blue square nest to the "This is a sentence" string . I've tried everything !
You need to set the layout of the frame to a layout that doesn't respect the preferred sizes of its children. FlowLayout does, and your JPanel has no preferred size without any components added to it, or specifying a preferred size.
A simple fix, set the layout of the frame to BorderLayout, or not set a layout at all, since JFrame already has a default BorderLayout. Note though that you probably want to add the JLabel to the JPanel and not the JFrame. Unless you do want to add it the JFrame and not the background JPanel, you need to specify a BorderLayout position for the one you don't want in the center.
You can see this answer to see which layout managers respect preferred sizes and which don't
See more at Layout out Components Withing a Container
Also, setVisible(true) shoul be the last thing you do after adding all components.
Related
Why is my JLabel text not aligned to the left or center? Is the FlowLayout the issue?
Both of the texts are just appearing at the top, next to each other and I can't fix this.
Main Class
public static void main(String[] args) {
simpleGui SG = new simpleGui();
SG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SG.setSize(1000,1000);
SG.setVisible(true);
}
Constructor class
public class simpleGui extends JFrame{
private JLabel label1;
private JLabel label2;
simpleGui(){
//title bar
super("Simple GUI");
setLayout(new FlowLayout());
label1 = new JLabel("I'm a label in the window");
label1.setVerticalTextPosition(SwingConstants.BOTTOM);
add(label1);
label2 = new JLabel("Label2");
label2.setHorizontalTextPosition(SwingConstants.LEFT);
add(label2);
}
}
Refer to Laying Out Components Within a Container which is a lesson in the Creating a GUI With Swing trail of Oracle's Java tutorials.
The javadoc for method setVerticalTextPosition, of class JLabel states the following:
Sets the vertical position of the label's text, relative to its image.
A JLabel can contain both text and an image. The image is also referred to as an icon. Since the JLabel in your code contains only text, calling this method does nothing. The same applies for method setHorizontalTextPosition.
A layout manager is responsible for placing a JComponent within its parent container. FlowLayout, by default, places the components in the top, center part of the container (which is JPanel in your code) and gives them their preferred size. (Refer to method getPreferredSize in class javax.swing.JComponent)
I suggest that you put a border around each component so that you can see how much space each one actually takes up. In the below screen capture, I added a red border to the [content pane of the] JFrame, a cyan border around label1 and a blue border around label2.
I assume that you want label1 to appear at the bottom of the content pane and label2 to appear at the left side. Therefore I think that the default layout manager is appropriate. The default layout manager is BorderLayout.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class SimpleGui extends JFrame {
private JLabel label1;
private JLabel label2;
public SimpleGui() {
super("Simple GUI");
((JPanel) getContentPane()).setBorder(BorderFactory.createLineBorder(Color.red));
label1 = new JLabel("I'm a label in the window", SwingConstants.CENTER);
label1.setBorder(BorderFactory.createLineBorder(Color.cyan));
add(label1, BorderLayout.PAGE_END); // changed
label2 = new JLabel("Label2");
label2.setBorder(BorderFactory.createLineBorder(Color.blue));
add(label2, BorderLayout.LINE_START); // changed
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SimpleGui sg = new SimpleGui();
sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sg.setSize(1000,1000);
sg.setVisible(true);
});
}
}
This is how it looks when I run the above code.
Note that BorderLayout gives the bottom component the same width as the content pane as well as its preferred height while the left component is given its preferred width but its height matches the height of the content pane. Refer to the tutorial I linked to at the start of this answer.
Since the width of label1 is greater than the width of the text it contains, I explicitly set the horizontal alignment of label1 to CENTER. By default it is LEFT. The alignment only affects the display when the width of the JLabel is larger than the width of its text.
I recommend that you go through the entire Creating a GUI With Swing trail in Oracle's Java tutorials.
I've updated your code a bit, I changed the classname to include java naming conventions and I've stuck with the default BorderLayout. I think this is what you're after judging by the names of your constants.
import javax.swing.*;
import java.awt.*;
public class SimpleGui extends JFrame{
private JLabel label1;
private JLabel label2;
SimpleGui(){
//title bar
super("Simple GUI");
//setLayout(new FlowLayout());
label1 = new JLabel("I'm a label in the window");
label1.setVerticalTextPosition(SwingConstants.BOTTOM);
add(label1, BorderLayout.SOUTH);
label2 = new JLabel("Label2");
label2.setHorizontalTextPosition(SwingConstants.LEFT);
add(label2, BorderLayout.WEST);
}
public static void main(String[] args) {
SimpleGui SG = new SimpleGui();
SG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SG.setSize(1000,1000);
SG.setVisible(true);
}
}
The FlowLayout is going to position things differently and you cannot just say, "you're at the top" and "you're at the left".
What you perceive: Regardless of the calls setVerticalTextPosition() or setHorizontalTextPosition() the text is always at the same location. But your calls actually had some effect that you just did not see.
The reason is that there are two positioning algorithms at the same time:
Positioning the label within the frame
You asked FlowLayout to take care. FlowLayout will simply ask the components about their preferred size, then place one besides the other just as you would expect characters or words to show up besides each other in an editor. Not that at this time each label was sized to it's preferred size.
Positioning the text within the label
When the label is asked about it's preferred size, it will give the minimum size necessary to render the configured text with the configured font. This way the information can be rendered comfortably without clipping off any information.
Later, when the label is asked to render the text, it will try follow your settings setVerticalTextPosition() or setHorizontalTextPosition(). But since there is no excess space within the label, doing it left-aligned or right-aligned or top-aligned or bottom aligned all ends up at the same coordinate.
Getting out of this
Either accept that the labels use their minimum size and you take care that the labels themselves are positioned correctly (GridBagLayout is very powerful in this respect), or resize the labels by setting a minimum and/or preferred size on them, then watch how your text positioning works.
The text positioning can be seen very nicely if you use BorderLayout and add the label in the center position. But this typically will work for one component only.
The question is that I cannot add JPanel and JLabel in the Frame at the same time.
When i using following code, only MyPanel will be visible. myFrame.add(myLabel);myFrame.add(myPanel);myFrame.setVisible(true);
when I execute: myFrame.add(myLabel);myFrame.setVisible(true);myFrame.add(myPanel);
only myLabel will be visible.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
MyLabel myLabel = new MyLabel();
MyPanel myPanel = new MyPanel();
myFrame.add(myLabel);
myFrame.add(myPanel);
myFrame.setVisible(true);
}
}
public class MyFrame extends JFrame {
MyFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of application.
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29
this.setSize(750, 750); //set the size.
this.setResizable(true);//resize the frame.
this.setTitle("Welocme to new world."); //set the Title.
ImageIcon imageIcon = new ImageIcon("logo.png");
this.setIconImage(imageIcon.getImage());
this.getContentPane().setBackground(new Color(217, 217, 217));
this.setBackground(Color.YELLOW);//this.setVisible(true);//Make the frame visible.
}}
import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel {
MyPanel() {
this.setBackground(Color.white);
this.setBounds(2,2,25,25);
}}
import javax.swing.*;
import java.awt.*;
public class MyLabel extends JLabel {
MyLabel(){
this.setText("<html>Heaven <br/>Heaven's body\"<br/> Whirl around me <br/>Make me wonder</html>");
//,SwingConstants.CENTER);
//https://stackoverflow.com/questions/1090098/newline-in-jlabel
//How to print multi line in java
ImageIcon image = new ImageIcon("Cosmogony_Björk_Cover.jpg");
this.setIcon(image);
//jLabel.setForeground(new Color(217,217,217));
this.setForeground(Color.BLACK);
this.setFont(new Font("helvetica",Font.PLAIN,18));
this.setBackground(Color.gray);
this.setOpaque(true);
//jLabel.setVerticalTextPosition(JLabel.TOP); Set the relative text position of the label.
//jLabel.setBorder();
this.setVerticalAlignment(JLabel.CENTER);
this.setHorizontalAlignment(JLabel.CENTER);
}}
The default Layout Manager of JFrame is the BorderLayout.
Since you did not change the layout manager of your JFrame this is also the current layout manager used in your snippet.
Usually, when using the BorderLayout, you specify which area of the BorderLayout should be populated when adding a component. This is usually done via
frame.add(component, BorderLayout.CENTER);
Notice the area specification in the add() method, which tells the BorderLayout where to place the component.
Here is the issue however. If you use the add() method in combination with the BorderLayout without specifying the placement of the component, it will always place the component in BorderLayout.CENTER. (Causing the component which is currently there to be replaced)
To work around this, do one of the following things:
Specify the placement explicitly, so both components will show up:
frame.add(component1, BorderLayout.PAGE_START);
frame.add(component2, BorderLayout.CENTER);
Or use a different Layout Manager, which will take care of the placement for you. E.g. FlowLayout
JPanel contentPanel = new JPanel(); // JPanel uses flowlayout by default!
contentPanel.add(component1);
contentPanel.add(component2);
myFrame.setContentPane(contentPanel);
You could also explicitly set the layout:
Container contentPane = myFrame.getContentPane();
// creates new FlowLayout and sets on content pane
contentPane.setLayout(new FlowLayout());
contentPane.add(component1);
contentPane.add(component2);
Sidenotes:
Look through the Laying out components within a container Oracle tutorial, which will give you more information on which layout managers there are and how to work with them.
When building your GUI, setVisible() on the JFrame should be the last thing you are doing after adding all components. Because if you add components after setting the frame visible, the changes will not immediately take effect without you telling swing that something changed.
When correctly working with the Swing Layout Managers, there should be no need to use things like setBounds(...) or setSize(). After adding all components, calling pack() on the JFrame is the preferred way to go. This will size the JFrame according to the preferred size of the components inside.
So I have this JFrame that contains a JPanel and in there I add JLabels with information I want but since I'll be adding labels all the time at some point the text is too long to appear so I want to add a scrollbar. Basically I want to make my JFrame with a JPanel in it scrollable. I have this code but my problem is that even though the scrollbar appears but it doesnt move and doesn't really work when the text is a lot, meaning the text still gets cut out and the scrollbar is there not moving. Does anyone know how to fix this?
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Bar {
JFrame info = new JFrame("Information");
JLabel ballinf = new JLabel();
JPanel contentPane = new JPanel();
JScrollPane scrolling = new JScrollPane();
public Bar(){
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
scrolling = new JScrollPane(contentPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
info.add(scrolling);
info.setSize(750, 600);
info.setLocationByPlatform(true);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public void adding(int pos){
ballinf = new JLabel("Something ",JLabel.CENTER);//assume the text will be bigger here and have more info
ballinf.setSize(700, 30);
ballinf.setForeground(Color.green);
ballinf.setLocation(5, 5+pos);
contentPane.add(ballinf);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public static void main(String[] args){
Bar stats = new Bar();
stats.adding(0);
stats.adding(20);//this will be done in a for loop for more than 2 times so the text ends up to be a lot
}
}
contentPane.setLayout(null);
Don't use a null layout!!!
You need to use an appropriate layout manager. Read the section from the Swing tutorial on Layout Managers for more information and working examples. The layout manager will then determine the preferred size of the panel as you add components to the panel.
The scrollpane will then display the scrollbars when necessary.
If you dynamically add components to the panel (after the GUI is visible) then the code should be something like:
panel.add(...);
panel.revalidate();
panel.repaint();
I can't make the pack() method work. I tried several things. My code looks like this at the moment:
Class 1:
public static void main( String[] args )
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
JavaGui mygui = new JavaGui();
// mygui.setSize(1154, 753);
mygui.setVisible(true);
mygui.pack();
Class 2:
public class JavaGui extends javax.swing.JFrame
{
public JavaGui()
{
getContentPane().setLayout(null);
..
getContentPane().add(panelLeft);
...
getContentPane().add(panelRight);
I tried putting the pack method in everywhere, but it's not going to work with this way of adding gui elements. Any suggestions why? I also tried adding everything to a JFrame instead of the getContentPane(), but I can't make that work either.
Don't use null layouts together with pack(). The pack method tells the layout managers and components to size themselves optimally, and if you instead use null layouts, then the gui risks shrinking to a minimal size, since there is no layout to hold it together.
Don't use null layouts at all for the most part. Using these risk your creating rigid GUI's that are almost impossible to extend, improve, debug.
Don't use setSize(...) and pack(). The layouts mostly respect the preferred sizes of components, not their sizes.
Instead:
Use a pleasing and sensible combination of nested JPanels, each using its own layout manager.
Let the components and the layout managers size themselves.
Then pack should help.
The general order that I do is to add all components to the GUI, then call pack(), then setLocationByPlatform(true) (I think), then setVisible(true).
For better help, please check out the Swing Layout Manager Tutorials.
Here are a couple examples to other questions on this site that use various layout managers:
A combination of BorderLayout and GridLayout to create a calculator
BorderLayout and BoxLayout Combination for labels and JTextFields
Using GridBagLayout to create flexible label/textfield grid
I would recommened beginners on building up swing guis to use a good ide with a builtin gui designer like eclipse and windowbuilder or netbeans with matisse. It will help you building up a prototype of your desired gui and gives you an insight how the layouting is done in the source code.
Experiment with the differenet layouts and what is happening when some values are changed.
one does not simply build up a well behaving gui without understanding how the layout works, so doing the recommended tutorials and looking at examples as already posted by Hovercraft Full Of Eels is absolutely necessary.
For your case i just guess what you were up to. Because youre mentioning left and right panels i suggest a JSplitPane which let you divide your screen in two areas which are customizable in size and orientation.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class JavaGui extends JFrame {
//SerialVersionId http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
//Calls to Gui Code must happen on the event dispatch thread that the gui does not get stuck
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new JavaGui().setVisible(true);
}
});
}
public JavaGui() {
// Set the desired size of the frame to determine the maximum size of its components
setPreferredSize(new Dimension(1024, 768));
// Set the default close operation, if press x on frame, destroy the frame and exit the application - others are just destroy the frame or just hide the
// frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// BorderLayout because we just need a centric gui with one component, here JSplitPane in full size
getContentPane().setLayout(new BorderLayout(0, 0));
// JsplitPane is a bit special as it depends on the divider location between both panels, for the sake of a small example we take the default -1,
JSplitPane splitPane = new JSplitPane();
// 0.5 divides extra space equally to left and right component when resizing the frame - so specifiying a size for the left and right component is not
// necessary
// use the divider location default -1 to let the width of the left component decide where the right component begins, in that case because of the
// resize weight half and half
splitPane.setDividerLocation(-1);
splitPane.setResizeWeight(0.5);
getContentPane().add(splitPane, BorderLayout.CENTER);
// For the panels the same layout as default as the intention is not stated in your question
JPanel leftPanel = new JPanel();
splitPane.setLeftComponent(leftPanel);
leftPanel.setLayout(new BorderLayout(0, 0));
JPanel rightPanel = new JPanel();
splitPane.setRightComponent(rightPanel);
rightPanel.setLayout(new BorderLayout(0, 0));
// Add a button Panel to the south for doing something - flow layout for letting the components flow to the right side
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// Close Button for closing the frame
JButton btnExit = new JButton("Destroy this frame, but let application run");
btnExit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
buttonPanel.add(btnExit);
// Set every component to its preferred size
pack();
// Make it visible
setVisible(true);
}
}
If you want your JFrame to work with a null layout, rearrange your code so that it looks like this:
public class JavaGui extends javax.swing.JFrame
{
public JavaGui()
{
setMinimumSize(1154, 753); // Make sure you do setMinimumSize() instead of setSize() when using pack() so that the JFrame does not shrink to 0 size
setLayout(null);
add(panelLeft);
add(panelRight);
pack();
}
// Next is main method
Main:
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new JavaGui().setVisible(true);
// Do not do any formatting for your JFrame here
}
});
Before, you were modifying the JFrame after it was set visible, so that usually does not work, except for pack(). All components and settings for your JFrame should not be in the main method if you are using an anonymous inner class.
You can also use other layouts. Null layouts are for getting pixels in precise locations, which is used for advanced GUI design such as creating a custom GUI, but it seems that you are making a generic GUI with JPanels. For this, I would recommend using a GridBagLayout, which keeps everything centered if the frame is resized and is easy to use. To use a GridBagLayout, you have to replace setLayout(null); with setLayout(new GridBagLayout()); and set GridBagConstraints. Here is some example code of making a panel with a component and a GridBagLayout:
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//For each component to be added to this container:
//...Create the component...
//...Set instance variables in the GridBagConstraints instance...
pane.add(theComponent, c);
// Source: Oracle Docs
I'm making a window application with Swing. I am using the setBounds() method for the JLabel spacing but it's not working.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FullScreenJFrame extends JFrame
{
public FullScreenJFrame( String title )
{
super(title);
//JFrame frame = new JFrame();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setUndecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
getContentPane()
.add(new JLabel(" HIGHCOURT OF JUDICATURE AT ALLHAHABAD"), BorderLayout.NORTH);
JLabel label = new JLabel("JJ");
label.setBounds(20, 20, 150, 20);
// label.setText(s);
add(label);
}
public static void main( String[] args )
{
FullScreenJFrame frame = new FullScreenJFrame("");
//JFrame frame1 = new JFrame();
//JLabel label = new JLabel("dd");
//label.setBounds(370, 340, 150, 20);
//frame1.add(label);
frame.setVisible(true);
}
}
One advice, It seems you are going to show your application using all screen so try to avoid the use of absolute positions like setBoundMethod.
You should fit your application within a layout that uses other layouts within it. Check this link.
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Don't use setBounds() to resize a component. Use a Layout Manager and you won't be worried about manually doing this.
Why?
Why are going for absolute positioning, when Layout Managers do it for you.
One more thing, If your JFrame does not contain any title so there is no need to add empty title as it is bad practice.
Replace
FullScreenJFrame frame = new FullScreenJFrame("");
By
FullScreenJFrame frame = new FullScreenJFrame();
And
public FullScreenJFrame( String title )
By
public FullScreenJFrame()
No need to call
super(title);
Or in order to set your layout as null right click on your form and click set layout then click null layout.
Are null layouts recommended? As other have suggested, the answer is NO.
See Laying out Components in a Container to get some practice in with LayoutManagers
Will I still answer your question? Sure. Just so you know.
"I am using the setBounds() method for the JLabel spacing but it's not working. Please, can any one tell me why it is not working?"
Yes, your bounds aren't working because the JFrame has a default Borderlayout. In order for setBounds to work, the layout needs to be null.
setLayout(null);
Also, keep in mind that when you do use a null layout, any components in which you don't setBounds for will not appear.
See Laying out Components in a Container to get some practice in with LayoutManagers