Java Swing JFrame Layout - java

I just wrote a simple code where I want a textfield and a button to appear on the main frame, but after running all I see is the textfield.
If I write the code of the button after the textfield then only the button is displayed.
Any idea why?
JFrame mainframe=new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setBounds(0,0,200,200);
JButton jb=new JButton();
jb.setText("Leech");
mainframe.add(jb);
JTextField link=new JTextField(50);
mainframe.add(link);
mainframe.pack();
mainframe.setVisible(true);

The default layout on a JFrame is a BorderLayout. Calling the add method on a Container with such a layout is equivalent to a call add(..., BorderLayout.CENTER). Each of the locations of the BorderLayout can contain only one element. Hence making two calls
mainframe.add(jb);
mainframe.add(link);
results in a CENTER containing the last component you added. If you want to avoid this you can either add it to different locations, or use another layout manager (for example a FlowLayout) by calling JFrame#setLayout

Instead of adding directly Components to the JFrame, use a JPanel as container with the desired LayoutManager.
Here you can find several tutorials on layout managers.
Basically in Swing the LayoutManager is responsible for laying out the children Components (establishing their position and their size), so every container component you use inside your app, should be configured with the appropiate LayoutManager.

Add your components to a JPanel and then add that panel to the ContentPane of JFrame.
JFrame window = new JFrame();
JPanel mainframe = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(0,0,200,200);
JButton jb = new JButton();
jb.setText("Leech");
mainframe.add(jb);
JTextField link = new JTextField(50);
mainframe.add(link);
window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);

You can also use something like Flow Layout which is the default layout used by JPanel. It is used to arrange components in a line or a row. For example from left to right or from right to left:
Flow layout arranges components in line and if no space left all remaining components goes to next line. Align property determines alignment of the components as left, right, center etc.
To use it you will need to set JFrame layout by using JFrame.setLayout(layout) and to pass flow layout as a parameter.
Following example shows components arranged in flow layout:
package example.com;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutExample {
FlowLayoutExample(){
JFrame frame = new JFrame("Flow Layout");
JButton button, button1, button2, button3, button4;
button = new JButton("button 1");
button1 = new JButton("button 2");
button2 = new JButton("button 3");
button3 = new JButton("button 4");
button4 = new JButton("button 5");
frame.add(button);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.setLayout(new FlowLayout());
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
Check out to learn more about JFrame layouts.

JFrame's default Layout Manager is BorderLayout. If you want the automatic layout, you may use the FlowLayout:
mainframe.setLayout(new FlowLayout());
If you want to specify coordinates by setBounds() method, you have to cancel the setting of layout manager on JFrame:
mainframe.setLayout(null);
jb.setBounds(10,10,100,50);
link.setBounds(10,70,180,100);

if you see BorderLayout Documentation
mainframe.add(jb); is equals to mainframe.add(jb,BorderLayout.CENTER);
mainframe.add(link); is equals to mainframe.add(jb,BorderLayout.CENTER);
so it just show the last one

Related

How to make embedded Jpanel go to WEST

Im creating an application in java swing and in it Im making what I think is called an embedded Jpanel which is to my understanding a jpanel inside a jpanel. To make this simpler we will use the panel names there is content, sidebar, and content sidebar
Sidebar is just the sidebar for the application with buttons
Content is the main content of the app
Content sidebar is a side bar inside of content, I use this so my settings page has its own side bar apart from the normal side bar.
I make sidebar aligned to WEST and content CENTER
after content.add(contentSidebar, BorderLayout.WEST); it won't make content sidebar go to west and im not sure why.
this is my source code
package Assets.Settings;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Security implements ActionListener{
JFrame Security;
JPanel sideBar;
JPanel content;
//for sidebar
JButton volume;
JButton security;
JButton contactUs;
JPanel contentSidePanel;
// JButton twoStep;
// JButton changePassword;
public void security(){
Security = new JFrame();
sideBar = new JPanel();
content = new JPanel();
contentSidePanel = new JPanel();
sideBar.setPreferredSize(new Dimension(125, 700));
content.setPreferredSize(new Dimension(1000, 700));
content.setBackground(Color.YELLOW);
Security.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Security.setTitle("Anonyomail Security Settings");
Security.setSize(1152, 700);
Security.setLayout(new java.awt.BorderLayout());
contentSidePanel.setLayout( new java.awt.BorderLayout());
volume = new JButton();
security = new JButton();
contactUs = new JButton();
// twoStep = new JButton();
// changePassword = new JButton();
volume.addActionListener(this);
contactUs.addActionListener(this);
volume.setText("Volume");
security.setText("Security");
contactUs.setText("Contact Us");
// changePassword.setText("Change Password");
// twoStep.setText("2-Step");
security.setBackground(Color.decode("#24a0ed"));
contentSidePanel.setPreferredSize(new Dimension(100, 700));
contentSidePanel.setBackground(Color.BLACK);
// contentSidePanel.add(changePassword);
// contentSidePanel.add(twoStep);
content.add(contentSidePanel, BorderLayout.WEST);
sideBar.add(volume);
sideBar.add(security);
sideBar.add(contactUs);
Security.add(sideBar, BorderLayout.WEST);
Security.add(content, BorderLayout.CENTER);
Security.pack();
Security.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == volume){
//open volume
}else if(e.getSource() == contactUs){
//open contact us
}
}
}
EDIT
The issue is that I didn't give content a border layout
You call content.add(contentSidePanel, BorderLayout.WEST) however you never set content's layout manager to BorderLayout. I believe JPanel's default layout manager is FlowLayout and thus BorderLayout.WEST implies nothing.
On another note, it seems like you're only adding one thing, contentSidePanel, to content. I believe that BorderLayout will size content to the same size as contentSidePanel and that specifying BorderLayout.WEST will have no effect since there is nothing in the center or to the east. Try adding a test JPanel to content and setting it's layout to BorderLayout.CENTER to see if I'm correct.
Note: BorderLayout's position constants where updated in JDK 1.4 from compass positions (NORTH, EAST, ...) to wordier constants (PAGE_START, PAGE_END, LINE_START, LINE_END and CENTER). Therefor the use of BorderLayout.WEST should be replaced with BorderLayout.LINE_START. - from How to use BorderLayout

JFrame turns blue when adding tabs

I currently have a Jframe that I want to add to a tab instead.
(I used a frame just for testing purposes, to make sure the look and feel is correct, but when trying to add it to a JTabbedPane, the frame starts to look blue (weird top aswell).
I tried copying my settings from my original frame to the new frame but that did not help.
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("1", frame.getContentPane());
JFrame FinalFrame = new JFrame();
FinalFrame.setDefaultLookAndFeelDecorated(true);
FinalFrame.setSize(WIDTH, HEIGTH);
FinalFrame.setLocation(100, 150);
FinalFrame.setTitle("Primal-Pvm Notification center");
FinalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FinalFrame.add(tabs);
Side by side view of the problem and the frame before adding it to the tab:
Edit: Answer by George Z. helped me out a lot.
Like he said to solve the problem:
Don't add things to your main frame but add them to a Jpanel and add that to a JTabbedPane.
If you have a Jpanel that you are adding to a tab that contains an override in the paintComponent, you have to create that class as the Jpanel so:
JPanel panel = new LineDrawer([Enter parameters]);
panel.setLayout([Enter Layout]);
The way you are approaching this seems to be pretty complex hence this weird behavior. (Looks like a look and feel problem? - show the part of the code that sets it)
However, I suggest you to create only one JFrame (this question explains why you should do that), set the layout of its content pane to BorderLayout and keep it like this. Its a rare situation to mess up with content panes. After that create independent JPanels representing the tab(s) you would like to have. Finally create a JTabbedPane with these panels and add it to the content frame of the JFrame.
A small example would be:
public class TabbedPanelExample extends JFrame {
public TabbedPanelExample() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTabbedPane pane = new JTabbedPane();
pane.addTab("FirstTab", createFirstTab());
pane.addTab("SecondTab", createSecondTab());
add(pane);
setSize(400, 400);
setLocationRelativeTo(null);
}
private Component createFirstTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JTextField("Some Other Component"));
return panel;
}
private Component createSecondTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JButton("Some Other Component"));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TabbedPanelExample().setVisible(true);
});
}
}
Post edit based on this comment:
Well I do have a Jframe with a lot of elements added to it so it kinda
is a hassle to switch it all to panels;
A JFrame cannot have a lot of elements. You take a look on how to use root panes. The container that "has a lot of elements" of a JFrame is its rootpane which is mostly completed by its contentpane. When you frame.add(component), you add the component to its content pane. Guess what? A JFrame's content pane is a JPanel. So are a already to panels.
Now in order to make this work, try to do as i said and frame.setLayout(new BorderLayout(); //changes layout to contentpane. Assuming you have a bunch of components (lets say comp1,comp2) and you are adding them like:
frame.add(comp1);
frame.add(comp2);
You must do the following in order to make it clear. Create a JPanel and instead of frame.add(comp1), do panel.add(comp1). So this JPanel has all the components you added in JFrame. After that create your JTabbedPane:
JTabbedPane pane = new JTabbedPane();
pane.addTab("tab", panel);
and finally add this pane to the content pane of your JFrame:
frame.add(pane);
In conclusion, you will move all the components you have added to your frame into a JPanel, add this JPanel to a JTabbedPane, and finally add this JTabbedPane to the frame (contentpane). Frame has only one component.

How to make a JButton that will create other buttons within a JPanel?

I am creating a user system to hold multiple details of multiple users. so i would like to create a button that would be able to create another button. when the second button is pressed a form will open for the user to fill. I have already created the form for the user to fill but i cannot manage to make the button to create more buttons to work. I have coded this but it does not show the button on the Jpanel.
I have created the following code:
private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
jPanel3.add(b);
b.setVisible(true);
}
This doesn't seem to create a new button within jPanel3. Have I typed up the code incorrectly or is there an alternative correct way of doing this?
I would like 3 buttons in a row and then a new row of buttons.
Your code and question is missing too much information to be answered completely or well. About all I can say is
Always call jPanel3.revalidate() and jPanel3.repaint() on the container after adding or removing components from it as this tells the container's (here jPanel3) layout managers to re-layout all components and then re-draw them.
The container's layout manager is key for this to work well -- we have no idea what it is at the moment, and some layout managers will allow you to do this easily (e.g., FlowLayout, GridLayout) while others won't (e.g., GroupLayout).
There's no need for b.setVisible(true); since your newly created JComponent (JButton here) is already visible by default.
You appear to assume that it's using null layouts since you're calling setBounds(...), and this is a Bad Idea™. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
When asking such questions, try to create and post with the question a small but complete program that we can test and run, and that illustrates your problem, a minimal example program (please click on the link).
For example, my MCVE that shows how your code can work:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class AddButton extends JPanel {
private JPanel jPanel3 = new JPanel(); // panel to hold buttons
public AddButton() {
// create JButton that will add new buttons to jPanel3
JButton addMoreButtonsBtn = new JButton("Add More Buttons");
// give it an ActionListener
addMoreButtonsBtn.addActionListener(e -> {
final JButton newButton = new JButton("Click Here");
// when you click it, it removes itself (just for grins)
newButton.addActionListener(e2 -> {
jPanel3.remove(newButton);
// again revalidate and repaint
jPanel3.revalidate();
jPanel3.repaint();
});
// add to jPanel3, the "container"
jPanel3.add(newButton);
// revalidate and repaint the container
jPanel3.revalidate();
jPanel3.repaint();
});
// create a JPanel and put the add more buttons button to it
JPanel bottomPanel = new JPanel();
bottomPanel.add(addMoreButtonsBtn);
// give jPanel3 a layout that can handle new buttons
// a gridlayout with 1 column and any number of rows
jPanel3.setLayout(new GridLayout(0, 1));
// add it to the top of another JPanel that uses BorderLayout
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(jPanel3, BorderLayout.PAGE_START);
// and add that to a JScrollPane, so we can add many buttons and scroll
JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
// make the vert scrollbar always visible
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// force GUI to be larger
setPreferredSize(new Dimension(400, 200));
// give the main JPanel a BorderLayout
setLayout(new BorderLayout());
// and add scrollpane to center
add(scrollPane, BorderLayout.CENTER);
// add bottom panel to the bottom
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
AddButton mainPanel = new AddButton();
JFrame frame = new JFrame("AddButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

Aligning JButton

I have to align a button on my program to the exact middle, the current code I have runs it but displays the button as large as the program, I am wanting a center button that is a specific size, here is what I tried
/**
* Created by Timk9 on 11/04/2016.
*/
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
{
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.setSize(600, 600);
window.setVisible(true);
window.setLocationRelativeTo(null);
JPanel p = new JPanel(new GridBagLayout());
//Button does not appear until I resize the program?
JButton b1 = new JButton("Click here");
GridBagConstraints c = new GridBagConstraints();
p.add(b1);
window.add(p);
}
public static void main(String[] args) {
new Test();
}
}
JPanel p = new JPanel(new GridBagLayout());
You create a panel with a GridBagLayout which is a good layout manager to use to center the component.
p.add(b1);
But then you add the button to the panel without using any contraints.
The code should be:
p.add(b1, c);
//Button does not appear until I resize the program?
All components should be added to the frame BEFORE the frame is made visible. The setVisible(...) statement should be the last statement of the constructor.
Also could you point out which part is an instance initializer block, I thought I was using a constructor
See the FrameDemo example from the Swing tutorial on How to Make Frames for a better way to structure your code so you follow Swing conventions. Start with the working code and make the changes to add your panel containing the button, instead of using the JLabel. Note you no longer need to use the getContentPane() method, you can just add the panel directly to the frame.
It is the LayoutManager that defines how components are layed out where and how big. GridLayout which you are using e. g. divides the available space in equal grid fields and makes the components completely fill this space which is why your button is as big as your application. See here for more info about LayoutManagers: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Btw. your code is not compilable: new JButton("he"),JButton.ALIGN_CENTER)

Java Swing - JLabel Location

I have problem while setting the Jlabel location.
I set the content pane to some JPanel, I created and tried to add my JLabel.
JLabel mainTitle = new JLabel("SomeApp");
mainTitle.setFont(new Font("Arial",2 , 28));
mainTitle.setBounds(0,0, 115, 130);
getContentPane().add(mainTitle);
I want that my JPanel will be on the top left corner on my application and what I am getting is "SomeApp" on the top center.(and not top left).
btw I tried to add JButton the and the I can`t change the width,height,x,y of the JButton.
Swing uses Layout Managers to place the components.
You have to understand how they work to use them effectively. You can set the layout manager to null, and do the layout your self, but is not recommendable because you'll have to keep track of new components each time, and perform layout computation your self when the window moves shrink etc.
Layout managers are a bit hard to grasp at first.
Your windows could be like this:
Using this code:
import javax.swing.*;
import java.awt.Font;
import java.awt.FlowLayout;
class JLabelLocation {
public static void main( String [] args ) {
JLabel mainTitle = new JLabel("SomeApp");
mainTitle.setFont(new Font("Arial",2 , 28));
//mainTitle.setBounds(0,0, 115, 130); //let the layout do the work
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));// places at the left
panel.add( mainTitle );
frame.add( panel );// no need to call getContentPane
frame.pack();
frame.setVisible( true );
}
}
Where a particular widget ends up in its container depends on the layout manager that it's using. The layout manager determines how to resize and arrange the widgets to make them fit appropriately. Obviously, the default layout for the content pane decided that the top center was the best place to put the JLabel.
If you want to get to not use a layout manager and just place everything yourself (which generally isn't the best way to lay things out btw), then add:
getContentPane().setLayout(null);
Using layouts is usually a better idea since they allow for dynamic resizing of components. Here's how you'd do it with a BorderLayout:
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add (new JLabel ("Main title"), BorderLayout.NORTH);
If you want to add something to the right of the label you could create an additionnal panel with it's own layout :
// Create a panel at the top for the title and anything else you might need
JPanel titlePanel = new JPanel (new BorderLayout());
titlePanel.add(new JLabel ("Main title"), BorderLayout.WEST);
// Add the title panel to the frame
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(titlePanel, BorderLayout.CENTER);
Here are some usefull links to get started with layouts:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/visual.html
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html

Categories

Resources