java swing - remove a jpanel from the frame - java

It's strange i don't know why but i cannot remove my jpanel from my frame.
I tried everything but nothing after this instrunctions i still continue to see the jpanel:
frame.getContentPane().remove(myPanel);
i have also tried to do:
frame.remove(...);
frame.add(...);
frame.revalidate();
frame.repaint();
but still i continue to see the panel in the frame.
This is my code (im developing a little app about the student note) and now i would like to remove my first panel just to do an experiment:
package StudApp;
import java.awt.BorderLayout;
public class StudApp {
private JPanel homeFirstRun;
private ArrayList<Corso> corsi = new ArrayList<Corso>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new StudApp();
}
});
}
/**
* Create the frame.
*/
public StudApp() {
JFrame frame = new JFrame("Student Note");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menuHelp = new JMenu("Help");
menuBar.add(menuHelp);
JMenuItem menuIstrStud = new JMenuItem("Istruzioni Student Note");
menuHelp.add(menuIstrStud);
homeFirstRun = new JPanel();
homeFirstRun.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(homeFirstRun);
homeFirstRun.setLayout(null);
JLabel welcomeMessage = new JLabel("Welcome to Student Note");
welcomeMessage.setBounds(5, 5, 424, 18);
welcomeMessage.setForeground(Color.DARK_GRAY);
welcomeMessage.setHorizontalAlignment(SwingConstants.CENTER);
welcomeMessage.setFont(new Font("Verdana", Font.BOLD, 14));
homeFirstRun.add(welcomeMessage);
JLabel welcomeCit = new JLabel("\"Software is like sex, it's better when it's free.\"");
welcomeCit.setFont(new Font("Verdana", Font.ITALIC, 11));
welcomeCit.setHorizontalAlignment(SwingConstants.CENTER);
welcomeCit.setBounds(35, 199, 361, 14);
homeFirstRun.add(welcomeCit);
JTextArea welcomeTextArea = new JTextArea();
welcomeTextArea.setFont(new Font("Verdana", Font.PLAIN, 13));
welcomeTextArea.setText(" I think it's your first time here.\n\n"
+ " So the first step is to create a new course to\n insert your grades.\n\n");
welcomeTextArea.setEditable(false);
welcomeTextArea.setBounds(27, 34, 381, 184);
homeFirstRun.add(welcomeTextArea);
frame.setVisible(true);
frame.remove(homeFirstRun); //here im doing a test because i wanna see if the homeFirstRun panel go out from the frame
// but not it remains.
}
}

Basically, because you used frame.setContentPane(homeFirstRun);, frame.remove(homeFirstRun); is being delegated to the content pane, so it's like saying...
homeFirstRun.remove(homeFirstRun);
Which obviously makes no sense...
Instead trying using something like...
frame.add(homeFirstRun);
//...
frame.remove(homeFirstRun);
Or a CardLayout, or in fact, any layout manager at all...

Related

When I run my code in Eclipse, my JLabel's disappear until I resize the window

So when I run this code:
public class Game
{
// DECLARING Java objects to later use
JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, signaturePanel;
JLabel titleNameLabel, signatureLabel;
Font titleFont = new Font("Britannic Bold", Font.BOLD, 80);
Font signature = new Font("Arial", Font.ITALIC, 15);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 30);
JButton startButton;
public static void main(String[] args)
{
// calling constructor
new Game();
}
public Game() {
// GAME WINDOW creation
window = new JFrame();
window.setSize(800,600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
// TITLE NAME PANEL for title text
titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, 600, 150);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("GAME");
titleNameLabel.setForeground(Color.white);
titleNameLabel.setFont(titleFont);
// START BUTTON PANEL for button housing
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
// START BUTTON customization
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
// SIGNATURE PANEL for text and customization
signaturePanel = new JPanel();
signaturePanel.setBounds(640, 525, 150, 40);
signaturePanel.setBackground(Color.black);
signatureLabel = new JLabel("coded by connor");
signatureLabel.setForeground(Color.white);
signatureLabel.setFont(signature);
// ADDING both labels to panels and retrieving content pane information
titleNamePanel.add(titleNameLabel);
signaturePanel.add(signatureLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
con.add(signaturePanel);
}
}`
My code works but my window just displays black until I maximize the window and then return it back to normal like this: black window with JLabels gone-----------------JLabels back when I maximize the screen and then minimize it back to it's original dimensions
I'm just wondering if anyone knows a fix for this that I'm overlooking. Thank you so much for your time.

Adding a JLabel under a JPanel not working (JAVA)

I'm designing a long and signup page as a test for some java projects and whilst trying to append the JLabel under the existing panel, the text does not show up.
Here is the code:
//Setting Panel Color
int r1 = 172;
int g1 = 50;
int b1 = 50;
Color myFgColor = new Color(r1,g1,b1);
JPanel panel = new JPanel();
panel.setBounds(750,60,375,420);
panel.setBackground(myFgColor);
//Login and Sign Up Text
JLabel label = new JLabel("LOGIN");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(Color.white);
panel.add(label);
gui.getContentPane().add(panel);
Is this what you are trying to see?
window
I didn't change anything, I just created a new JFrame and put your components...
The difference I see is that JFrame creates the contentPane, and you didn't:
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window frame = new Window();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Window() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 575);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//Setting Panel Color
Color myFgColor = new Color(172,50,50);
JPanel panel = new JPanel();
panel.setBounds(750,60,375,420);
panel.setBackground(myFgColor);
//Login and Sign Up Text
JLabel label = new JLabel("LOGIN");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(Color.white);
panel.add(label);
getContentPane().add(panel);
//Setting Background Color
Color myBgColor = new Color(30,30,30);
getContentPane().setBackground(myBgColor);
//Centering Window
setLocationRelativeTo(null);
}
I recommend you using WindowBuilder to create frames, it's a visual designer and it makes it very easy

Can't add scroll bar to JTextarea

Hello Everyone I am new to Java so I am sure I am doing something obviously wrong here but I just keep going in circles.
I am trying to add a scroll to a JTextarea. Here is what I have tried but it doesn't show up.
textarea1 = new JTextArea();
textarea1.setBounds(251,26,795,345);
textarea1.setBackground(new Color(255,255,255));
textarea1.setForeground(new Color(0,0,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText("");
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
JScrollPane ScrollPane1 = new JScrollPane(textarea1);
ScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//adding components to contentPane panel
contentPane.add(browseFileOne);
contentPane.add(browseOutput);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button4);
contentPane.add(fileOneText);
contentPane.add(fileTwoText);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(outputTextFile);
contentPane.add(textarea1);
see working example:
https://repl.it/repls/DimpledDefensiveSourcecode
Code:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(400, 400);
this.setLocation(0, 0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5, 5, this.getWidth() - 120, 20);
tf1.setColumns(10);
tf1.setText("Omg");
// resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
painel.add(scr);// Add you scroll pane to container
this.add(painel);
this.setVisible(true);
}
}

Java Panel Not Displaying

public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
});
}
}
The Panel code is dictates how this MyPanel should look.
public class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JTextField txtUsername;
public MyPanel()
{
setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setBackground(SystemColor.control);
panel.setBounds(0, 0, 500, 500);
add(panel);
ImageIcon icon= new ImageIcon("C:/Users/Admin/Desktop/testPic.jpg");
JLabel wlabel = new JLabel(icon);
wlabel.setBounds(20, 10, 400, 222);
panel.add(wlabel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(36, 244, 614, 159);
panel.add(panel_1);
panel_1.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(40, 40, 100, 20);
panel_1.add(lblUsername);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 18));
txtUsername = new JTextField();
txtUsername.setBounds(179, 52, 195, 30);
panel_1.add(txtUsername);
txtUsername.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.setBounds(424, 65, 145, 44);
panel_1.add(btnSubmit);
btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
}
}
Why don't I see the actual panel? The code compiles and runs but I don't see anything on my screen.
You've got to add your JPanel to your JFrame. It's the JFrame that is the top level window that displays the entire GUI. Without a top level window either created directly, as described above, or indirectly, such as when you create a JOptionPane, the JPanel will never be seen.
So, rather than this:
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
Do this:
public void run()
{
Test test = new Test();
test.setVisible(true);
}
And then create your MyPanel in the Test constructor, and add it to Test there via a call to add(...).
Next we'll talk about why null layouts and setBounds(...) is a very bad thing.
Key tutorial links:
Using Swing Components
Using Top Level Windows
Laying Out Components in a Container

Frame Won't Show?

I broke my code, but I cannot figure out how. At all.
At one point, compiling and running the code rendered a window every single time. Then I worked for about 15 minutes, and the frames no longer appear upon running. I've tried undoing the work, adding run methods, adding a main method, and even copying and pasting code directly from my textbook. Nothing has worked.
I tried to paste as little code as possible. Forgive me if my code is sloppy; this is my first time working with swing. Thanks!
public class LibraryFrame extends JFrame implements ActionListener
{
private JScrollPane studentScroller = new JScrollPane();
private JPanel addStudent = new JPanel();
private JTextField UID = new JTextField();
private JTextField name = new JTextField();
private JTextField email = new JTextField();
private JButton okButton = new JButton("OK");
private JButton closeButton = new JButton("Close");
private JPanel buttonPane = new JPanel();
public LibraryFrame()
{
setTitle("Student Enrollment");
setSize(500,200);
setLocationByPlatform(true);
addStudent.setLayout(new GridLayout(0, 2, 0, 0));
addStudent.add(new JLabel("Enter Student UID:"));
addStudent.add(UID);
addStudent.add(new JLabel("Enter Student Name:"));
addStudent.add(name);
addStudent.add(new JLabel("Enter Student Email:"));
addStudent.add(email);
addStudent.add(Box.createRigidArea(new Dimension(0, 5)));
addStudent.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
okButton.setActionCommand(null);
closeButton.addActionListener(new ActionListener() //implement window close only
{
#Override
public void actionPerformed(ActionEvent e)
{ dispose(); }
});
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPane.add(Box.createRigidArea(new Dimension(0, 0)));
buttonPane.add(okButton);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(closeButton);
Container studentPane = getContentPane();
studentPane.add(addStudent, BorderLayout.CENTER);
studentPane.add(buttonPane, BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
You show no main method, so we don't know how you try to run this.
You add all to studentPane. Where do you add studentPane to the JFrame, to the this object? If it's not added to the GUI, it will not show.

Categories