Trouble making a GUI. Labels, spinners and buttons not appearing - java

I've put quite a few objects into this JFrame. however many of them do not appear until I hover them mainly the button as well as the JTextField. I only included the JSpinner components so that it wasn't so long. Is there anything in my spinners or lacking with my spinners that would make them not appear?
I've stripped it down alot so that it is just one textfield. Still nothing shows up. I've put everything in panels, however still nothing shows up.
public static void screen() {
JFrame myFrame = new JFrame("Yore");
myFrame.setAlwaysOnTop(true);
myFrame.setResizable(false);
myFrame.setForeground(Color.BLACK);
myFrame.setSize(300,365);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setBackground(Color.WHITE);
JPanel panel = new JPanel();
myFrame.setContentPane(panel);
JTextField Username = new JTextField();
Username.setText("Username");
Username.setColumns(10);
panel.add(Username);
}

Sorry, i forgot pne statement.
public static void screen() {
JFrame myFrame = new JFrame("Yore");
myFrame.setAlwaysOnTop(true);
myFrame.setResizable(false);
myFrame.setForeground(Color.BLACK);
myFrame.setSize(300,365);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setBackground(Color.WHITE);
JPanel panel = new JPanel();
myFrame.setContentPane(panel);
JTextField Username = new JTextField();
Username.setText("Username");
Username.setColumns(10);
panel.add(Username);
panel.validate();
}

Try using a layout manager for the frame. Also, add a JPanel to the frame and add all of the Spinners to the Panel.
Using the correct layout manager, you can programatically add the Spinners to the Panel, so that you do not need to hard code the bounds. You will also have the ability to have them resize as the window resizes, for example.
Take a look here to get started with layout managers.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

myFrame.getContentPane() returns an AWT container, not a Swing component. To change this you have to use
JPanel panel = new JPanel();
panel.add(..);
myFrame.setContentPane(panel);
panel.validate();
Add your spinners to the panel and your problem is solved.

Add spinners to the panel. and then add panel to your JFrame.
panel.add(spinner_5);

Related

Why can I not edit the location of my JButton?

Code:
public class launcher implements ActionListener {
private static JFrame window;
private static JPanel panel;
private JButton createPassword;
private JButton seePassword;
public launcher() {
window = new JFrame();
panel = new JPanel();
window.setTitle("Password Vault");
window.setSize(400, 260);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setVisible(true);
window.getContentPane().add(panel); // Adds panel to JFrame
createPassword = new JButton("Create Password");
createPassword.setBounds(20, 100, 150, 100);
createPassword.addActionListener(this);
seePassword = new JButton("View Password");
seePassword.setLocation(20, 50);
seePassword.addActionListener(this);
panel.add(createPassword);
panel.add(seePassword);
}
public static void main(String[] args) {
new launcher();
}
Why can I not change the location of my JButtons? I have tried the setBounds and setLocation function but my buttons still stay on the top middle part of the JFrame window. I have also tried declaring my buttons inside the launcher() method and declaring them as a static variable.
You should NOT attempt to set the size/location of your buttons.
Swing was designed to be used with layout managers. The layout manager will set the size/location of the button based on the rules of the layout manager.
The default layout manager for a JPanel is the FlowLayout, which is why you see the button centered.
If you want to position the buttons differently, then you need to change the layout manager.
Read the section from the Swing tutorial on Layout Managers for more information and examples.
It looks like you want the buttons displayed vertically, so maybe a BoxLayout or GridLayout can be used depending on your exact requirement.
Other issues with your code:
Class names should start with an upper case character
There is no need to use static variables. That is not how the static keyword should be used
Components should be added to the frame BEFORE the frame is made visible.
You should be invoking pack() on the frame, BEFORE invoking setVisible(...) so the components can be displayed at their preferred size.
While you really shouldn't change the place of your components without a layout manager, there is a way. If you put this line of code into your code, it should work with setBounds:
window.setLayout(null);

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());
}
}

Why the button on the JFrame with background image cannot show properly

i recently code a system homepage that include the background image. After i set the background, the button that i created cannot show properly. It just appear after i use the mouse and point to the buttons' location. Can someone teach me how to fix this problem? Your help are appreciated. The codes are as below:
public class HomePage extends JFrame{
private JPanel button = new JPanel();
private JButton time = new JButton("Bus Schedule");
private JButton reserve = new JButton("Booking");
private JButton info = new JButton("About Us");
Container con = getContentPane();
public HomePage(){
setTitle("Bus Reservation System");
setSize(650,500);
setLocation(360,100);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground();
setButton();
}
public void setBackground(){
JLabel background = new JLabel(new ImageIcon("C:/User/Desktop/Assignment/bg.jpg"));
con.add(background);
background.setLayout(new FlowLayout());
con.add(button);
}
public void setButton(){
Font but = new Font("Serif" , Font.ITALIC , 20);
info.setFont(but);
time.setFont(but);
reserve.setFont(but);
button.add(info);
button.add(time);
button.add(reserve);
con.add(button);
}
After you call setVisible(true), if you perform certain actions, you must manually validate() or revalidate() your window.
It is simpler to just call setVisible() after you have initialized all of the desired settings and membership of the window.
/* setVisible(true); -- wrong place */
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground();
setButton();
setVisible(true); // proper place
Simple mistake (probably a typo?), you are setting the layout of the JLabel, you intended to set the layout of the Container. Do con.setLayout(new FlowLayout()); instead of background.setLayout(...)
Also, I believe your path to the file is incorrect. For testing, just put the file in the project and do a path like "bg.jpg", if that works, we can verify this. For me that works. The reason I believe this is the issue is because you specify C:/Users, but then never give the folder for the specific User. A correct path would be C:/Users/Your_name/Desktop/Assignment/bg.jpg
I don't know for sure on that path part, since I'm not on your system. However, for me, if I run your code in my eclipse, this fixes it.
edit:
One last thing, the image isn't really going to be a "background" image with the current code because it will move the buttons underneath it instead of on top of it with the FlowLayout. You may want to use a different Layout.
First of all as already noted, all components should be added to the frame BEFORE invoking setVisible(true) on the JFrame.
JLabel background = new JLabel(new ImageIcon("C:/User/Desktop/Assignment/bg.jpg"));
con.add(background);
background.setLayout(new FlowLayout());
con.add(button);
Based on the above code you are adding two components to the content pane so the hierarchy of your GUI looks like this:
- frame
- content pane
- background
- button
It seems to me you want your GUI to look like this:
- frame
- content pane
- background
- button
So your code should be:
JLabel background = new JLabel(new ImageIcon("C:/User/Desktop/Assignment/bg.jpg"));
con.add(background);
background.setLayout(new FlowLayout());
//con.add(button);
background.add( button );

How to make a Jframe containing a Jpanel scrollable?

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();

Categories

Resources