JDialog shows empty - java

I was trying to replace the use of JOptionPane by a new custom dialog here is what I did:
package pk;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.border.LineBorder;
public class SIMessage extends JDialog implements ActionListener{
private static final long serialVersionUID = 1L;
public JButton oui=new JButton("Oui"),btnClose=new JButton(new ImageIcon("images\\logo\\delete.gif")),
non=new JButton("Non"),annuler=new JButton("Annuler"),ok=new JButton("OK");
public JLabel lblImgErr=new JLabel(new ImageIcon("images\\logo\\msgErreur.png")),
lblImgConf=new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
lblImgWarning=new JLabel(new ImageIcon("images\\logo\\msgWarning.png")),
lblImgInfo=new JLabel(new ImageIcon("images\\logo\\msgInformation.png")),
lblImgQuestion=new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
lblImgIconApp=new JLabel(new ImageIcon("images\\logo\\clntIco.ico"));
public JLabel title=new JLabel(),message=new JLabel();
public enum TypeMessage{
ERROR_MESSAGE,
CONFIRMATION_MESSAGE,
WARNING_MESSAGE,
INFORMATION_MESSAGE,
VALIDATION_MESSAGE
}
public SIMessage(JFrame parent,String title,TypeMessage type,String message) {
super(parent,true);
setUndecorated(true);
getContentPane().setLayout(new GridLayout(1, 1));
JPanel mainDgPanel = new JPanel();
mainDgPanel.setBorder(new LineBorder(new Color(255, 255, 255), 3, true));
mainDgPanel.setBounds(0, 0, 444, 156);
getContentPane().add(mainDgPanel);
mainDgPanel.setLayout(null);
mainDgPanel.setBackground(Color.decode(EcranPrincipal.blueThemeCP));
JTextArea txtrTextarea = new JTextArea(message);
txtrTextarea.setRows(2);
txtrTextarea.setBounds(123, 62, 340, 80);
txtrTextarea.setFont(new Font("Iskoola Pota", Font.PLAIN, 18));
txtrTextarea.setEditable(false);
txtrTextarea.setFocusable(false);
txtrTextarea.setOpaque(false);
txtrTextarea.setBorder(null);
txtrTextarea.setWrapStyleWord(true);
txtrTextarea.setLineWrap(true);
txtrTextarea.setForeground(Color.decode(EcranPrincipal.blueThemeBT));
mainDgPanel.add(txtrTextarea);
JPanel panelButtons = new JPanel();
panelButtons.setBounds(47, 115, 344, 30);
mainDgPanel.add(panelButtons);
switch(type)
{
case ERROR_MESSAGE:
{
JLabel lblNewLabel =lblImgErr;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case CONFIRMATION_MESSAGE:
{
JLabel lblNewLabel =lblImgConf;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOui = oui;
panelButtons.add(btnOui);
break;
}
case WARNING_MESSAGE:
{
JLabel lblNewLabel =lblImgWarning;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case INFORMATION_MESSAGE:
{
JLabel lblNewLabel =lblImgInfo;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case VALIDATION_MESSAGE:
{
JLabel lblNewLabel =lblImgConf;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOui = oui;
panelButtons.add(btnOui);
JButton btnNon = non;
panelButtons.add(btnNon);
JButton btnAnnuler = annuler;
panelButtons.add(btnAnnuler);
break;
}
default:
}
ok.addActionListener(this);
oui.addActionListener(this);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 444, 27);
mainDgPanel.add(panel);
panel.setBackground(Color.WHITE);
panel.setLayout(null);
JButton btnCloseDf = btnClose;
btnCloseDf.setBounds(411, 0, 39, 23);
panel.add(btnCloseDf);
JLabel lblIconApp =lblImgIconApp;
lblIconApp.setBounds(10, 4, 77, 14);
panel.add(lblIconApp);
JLabel lblTitle = new JLabel(title);
lblTitle.setBounds(190, 4, 46, 14);
panel.add(lblTitle);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source=e.getSource();
if(source==oui||source==ok)
{
this.dispose();
}
}
Then I call:
SIMessage sm=new SIMessage(this, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
callMethode2();
The problem is that it executes the call to Methode2 before showing any dialog while it is supposed to force the user to respond before continuing.
I see an empty window side by side with the window generated by callMethod2!, so what is wrong?

You should set the modality for Dialog.
A modal window is a graphical control element subordinate to an application's main window. It creates a mode that disables the main window but keeps it visible with the modal window as a child window in front of it. Users must interact with the modal window before they can return to the parent application.
So, Set the modal flag of the dialog when initializing it.
setModal(True)
edit:
I don't know what you exactly changed in your code, but the code below works fine for me:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Emad
*/
import java.awt.BorderLayout;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;
import javax.swing.border.LineBorder;
public class SIMessage extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
public JButton oui = new JButton("Oui"), btnClose = new JButton(new ImageIcon("images\\logo\\delete.gif")),
non = new JButton("Non"), annuler = new JButton("Annuler"), ok = new JButton("OK");
public JLabel lblImgErr = new JLabel(new ImageIcon("images\\logo\\msgErreur.png")),
lblImgConf = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
lblImgWarning = new JLabel(new ImageIcon("images\\logo\\msgWarning.png")),
lblImgInfo = new JLabel(new ImageIcon("images\\logo\\msgInformation.png")),
lblImgQuestion = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
lblImgIconApp = new JLabel(new ImageIcon("images\\logo\\clntIco.ico"));
public JLabel title = new JLabel(), message = new JLabel();
#Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if (source == oui || source == ok) {
this.dispose();
}
}
public enum TypeMessage {
ERROR_MESSAGE,
CONFIRMATION_MESSAGE,
WARNING_MESSAGE,
INFORMATION_MESSAGE,
VALIDATION_MESSAGE
}
public SIMessage(JFrame parent, String title, TypeMessage type, String message) {
super(parent, true);
setUndecorated(true);
getContentPane().setLayout(new GridLayout(1, 1));
JPanel mainDgPanel = new JPanel();
mainDgPanel.setBorder(new LineBorder(new Color(255, 255, 255), 3, true));
mainDgPanel.setBounds(0, 0, 444, 156);
getContentPane().add(mainDgPanel);
// mainDgPanel.setBackground(Color.decode(EcranPrincipal.blueThemeCP));
JTextArea txtrTextarea = new JTextArea(message);
txtrTextarea.setRows(2);
txtrTextarea.setBounds(123, 62, 340, 80);
txtrTextarea.setFont(new Font("Iskoola Pota", Font.PLAIN, 18));
txtrTextarea.setEditable(false);
txtrTextarea.setFocusable(false);
txtrTextarea.setOpaque(false);
txtrTextarea.setBorder(null);
txtrTextarea.setWrapStyleWord(true);
txtrTextarea.setLineWrap(true);
// txtrTextarea.setForeground(Color.decode(EcranPrincipal.blueThemeBT));
mainDgPanel.add(txtrTextarea);
JPanel panelButtons = new JPanel();
panelButtons.setBounds(47, 115, 344, 30);
mainDgPanel.add(panelButtons);
switch (type) {
case ERROR_MESSAGE: {
JLabel lblNewLabel = lblImgErr;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case CONFIRMATION_MESSAGE: {
JLabel lblNewLabel = lblImgConf;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOui = oui;
panelButtons.add(btnOui);
break;
}
case WARNING_MESSAGE: {
JLabel lblNewLabel = lblImgWarning;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case INFORMATION_MESSAGE: {
JLabel lblNewLabel = lblImgInfo;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOk = ok;
panelButtons.add(btnOk);
break;
}
case VALIDATION_MESSAGE: {
JLabel lblNewLabel = lblImgConf;
lblNewLabel.setBounds(10, 69, 79, 14);
mainDgPanel.add(lblNewLabel);
JButton btnOui = oui;
panelButtons.add(btnOui);
JButton btnNon = non;
panelButtons.add(btnNon);
JButton btnAnnuler = annuler;
panelButtons.add(btnAnnuler);
break;
}
default:
}
ok.addActionListener(this);
oui.addActionListener(this);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 444, 27);
mainDgPanel.add(panel);
panel.setBackground(Color.WHITE);
panel.setLayout(null);
JButton btnCloseDf = btnClose;
btnCloseDf.setBounds(411, 0, 39, 23);
panel.add(btnCloseDf);
JLabel lblIconApp = lblImgIconApp;
lblIconApp.setBounds(10, 4, 77, 14);
panel.add(lblIconApp);
JLabel lblTitle = new JLabel(title);
lblTitle.setBounds(190, 4, 46, 14);
panel.add(lblTitle);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
SIMessage sm=new SIMessage(null, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
System.out.println("hello");
}
}

Related

GUI goes blank when i try to define the public void actionPerformed(ActionEvent arg0)

When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(56, 56, 56, 16);
contentPane.add(lblName);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(56, 85, 77, 16);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(56, 114, 56, 16);
contentPane.add(lblPassword);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(56, 143, 56, 16);
contentPane.add(lblAge);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(56, 172, 56, 16);
contentPane.add(lblGender);
textField = new JTextField();
textField.setBounds(130, 53, 116, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(130, 82, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(130, 111, 116, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(130, 140, 116, 22);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(130, 169, 116, 22);
contentPane.add(textField_4);
textField_4.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSubmit.setBounds(169, 215, 97, 25);
contentPane.add(btnSubmit);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(330, 56, 56, 16);
contentPane.add(lblNewLabel);
}
}
EDIT 1: Added String name to action just to get name.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Name;
}
});
and This happened
GUI went blank:
You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.
Register frame = new Register();
frame.validate();
frame.setVisible(true);
Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.

how should i navigate from one window that uses an application window to another frame in another application window?

i am developing an application for which i have created different application windows. Now i'm in a confusion on how i can link one to another using a button, that is when i click a button, it has to navigate to the other application window. I have tried it with frames from same file, but that doesnt satisfy my need.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.UIManager;
import java.awt.event.*;
public class Start {
private JFrame frame;
private JTextField txtLogInTo;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Start window = new Start();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaption);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("USER");
btnNewButton.setBackground(SystemColor.activeCaption);
btnNewButton.setBounds(152, 81, 132, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("BUS ADMIN");
btnNewButton_1.setBackground(SystemColor.activeCaption);
btnNewButton_1.setBounds(152, 131, 132, 23);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("SYSTEM ADMIN");
btnNewButton_2.setBackground(SystemColor.activeCaption);
btnNewButton_2.setBounds(152, 179, 132, 23);
frame.getContentPane().add(btnNewButton_2);
txtLogInTo = new JTextField();
txtLogInTo.setBounds(95, 27, 252, 36);
txtLogInTo.setBackground(SystemColor.inactiveCaption);
txtLogInTo.setHorizontalAlignment(SwingConstants.CENTER);
txtLogInTo.setFont(new Font("Tekton Pro Cond", Font.BOLD | Font.ITALIC, 20));
txtLogInTo.setText("LOG IN TO ENTER THE SYSTEM");
frame.getContentPane().add(txtLogInTo);
txtLogInTo.setColumns(10);
class handler implements ActionListener
{
//must implement method
//This is triggered whenever the user clicks the login button
public void actionPerformed(ActionEvent ae)
{
if(btnNewButton.isEnabled())
{User u;
}
}//if
}//method
}//inner class
}
and this above program is where i have to navigate when i click a button from the below program
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class User {
private JFrame frame,frame2;
private JTextField txtWelcomeUser;
private JTextField txtEnterYourDetails;
private JTextField txtName;
private JTextField txtAge;
private JTextField txtPhoneNo;
private JTextField txtMailId;
private JTextField txtStart;
private JTextField txtDestination;
private JTextArea textArea_4;
private JTextArea textArea_5;
private JTextField txtEn;
private ActionListener action;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
User window = new User();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public User() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 750, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtWelcomeUser = new JTextField();
txtWelcomeUser.setBackground(SystemColor.inactiveCaption);
txtWelcomeUser.setFont(new Font("Tahoma", Font.PLAIN, 26));
txtWelcomeUser.setText("Welcome user");
txtWelcomeUser.setBounds(176, 11, 173, 52);
frame.getContentPane().add(txtWelcomeUser);
txtWelcomeUser.setColumns(10);
txtEnterYourDetails = new JTextField();
txtEnterYourDetails.setBackground(SystemColor.activeCaption);
txtEnterYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 20));
txtEnterYourDetails.setText("Enter your details");
txtEnterYourDetails.setBounds(176, 74, 173, 36);
frame.getContentPane().add(txtEnterYourDetails);
txtEnterYourDetails.setColumns(10);
txtName = new JTextField();
txtName.setBackground(SystemColor.inactiveCaption);
txtName.setText("Name");
txtName.setBounds(93, 136, 86, 20);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
txtAge = new JTextField();
txtAge.setBackground(SystemColor.inactiveCaption);
txtAge.setText("Age");
txtAge.setBounds(93, 167, 86, 20);
frame.getContentPane().add(txtAge);
txtAge.setColumns(10);
txtPhoneNo = new JTextField();
txtPhoneNo.setBackground(SystemColor.inactiveCaption);
txtPhoneNo.setText("Phone No");
txtPhoneNo.setBounds(93, 198, 86, 20);
frame.getContentPane().add(txtPhoneNo);
txtPhoneNo.setColumns(10);
txtMailId = new JTextField();
txtMailId.setBackground(SystemColor.inactiveCaption);
txtMailId.setText("Mail id");
txtMailId.setBounds(93, 229, 86, 20);
frame.getContentPane().add(txtMailId);
txtMailId.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setBounds(236, 134, 124, 20);
frame.getContentPane().add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(236, 165, 124, 20);
frame.getContentPane().add(textArea_1);
JTextArea textArea_2 = new JTextArea();
textArea_2.setBounds(236, 196, 124, 20);
frame.getContentPane().add(textArea_2);
JTextArea textArea_3 = new JTextArea();
textArea_3.setBounds(236, 227, 124, 20);
frame.getContentPane().add(textArea_3);
txtStart = new JTextField();
txtStart.setBackground(SystemColor.inactiveCaption);
txtStart.setText("Start ");
txtStart.setBounds(93, 260, 86, 20);
frame.getContentPane().add(txtStart);
txtStart.setColumns(10);
txtDestination = new JTextField();
txtDestination.setBackground(SystemColor.inactiveCaption);
txtDestination.setText("Destination");
txtDestination.setBounds(93, 291, 86, 20);
frame.getContentPane().add(txtDestination);
txtDestination.setColumns(10);
textArea_4 = new JTextArea();
textArea_4.setBounds(236, 258, 124, 20);
frame.getContentPane().add(textArea_4);
textArea_5 = new JTextArea();
textArea_5.setBounds(236, 289, 124, 20);
frame.getContentPane().add(textArea_5);
JPanel panel = new JPanel();
panel.setForeground(new Color(0, 0, 0));
panel.setBackground(SystemColor.inactiveCaption);
panel.setBounds(433, 213, 124, 115);
frame.getContentPane().add(panel);
txtEn = new JTextField();
txtEn.setBackground(SystemColor.inactiveCaption);
txtEn.setText("Enter Bus No");
txtEn.setBounds(93, 322, 86, 20);
frame.getContentPane().add(txtEn);
txtEn.setColumns(10);
JTextArea textArea_6 = new JTextArea();
textArea_6.setBounds(236, 320, 124, 20);
frame.getContentPane().add(textArea_6);
JButton btnBookTicket = new JButton("Book Ticket");
btnBookTicket.setBackground(SystemColor.activeCaption);
btnBookTicket.setBounds(176, 376, 116, 23);
frame.getContentPane().add(btnBookTicket);
JPopupMenu popupMenu_1 = new JPopupMenu();
popupMenu_1.setBounds(327, 376, 200, 50);
frame.getContentPane().add(popupMenu_1);
if(!textArea.isEnabled()||!textArea_2.isEnabled()||!textArea_3.isEnabled()||!textArea_3.isEnabled()||!textArea_4.isEnabled()||!textArea_5.isEnabled()||!textArea_6.isEnabled())
{
JOptionPane.showMessageDialog(null, "You have not entered a few details","UnSuccessful",
JOptionPane.INFORMATION_MESSAGE);
}
if(btnBookTicket.isEnabled())
{
btnBookTicket.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "You have booked successfully","Success",
JOptionPane.INFORMATION_MESSAGE);
JButton button = (JButton) e.getSource();
if (button == btnBookTicket)
{
frame2 = new JFrame("FRAME 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLocationByPlatform(true);
JPanel contentPane2 = new JPanel();
contentPane2.setBackground(Color.DARK_GRAY);
JButton btnBookTicket1 = new JButton("Cancel Ticket");
btnBookTicket1.setBackground(SystemColor.activeCaption);
btnBookTicket1.setBounds(0, 30, 35, 23);
contentPane2.add(btnBookTicket1);
frame2.getContentPane().add(contentPane2);
frame2.setSize(600, 600);
frame2.setVisible(true);
frame.setVisible(false);
}
btnBookTicket.addActionListener(action);
}
});
}
}
}

Transferring of data from one JPanel to another

I am curious to ask that is it possible to transfer a data from one JPanel to another. I'm currently doing a swing java project which has a profile page and a profile edit page. I wanted to try out creating a method to get the text in a textfield in the profile edit page and then return the textfield.getText();. Then I will follow by calling the exact method from the profile page and setting it as a JLabel. However, I wasn't sure how to do this...
I updated the codes below.
Here is the Profile Edit Page:
package Project;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ProfileEdit extends MasterPanel {
private JTextField txtbxFullName;
private JTextField txtAdmissionNo;
private JTextField txtbxContactNo;
private JTextField txtFieldDiploma;
/**
* Create the panel.
*/
public ProfileEdit(JFrame mf) {
super(mf);
JLabel lblUserProfile = new JLabel("");
lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE));
lblUserProfile.setForeground(Color.WHITE);
lblUserProfile.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblUserProfile.setBounds(60, 103, 514, 297);
add(lblUserProfile);
JLabel lblDisplayPic = new JLabel("");
lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
lblDisplayPic.setIcon(new ImageIcon(ProfileEdit.class.getResource("/javaProject/images/default-avatar.png")));
lblDisplayPic.setBounds(100, 132, 90, 100);
add(lblDisplayPic);
JLabel lblFullName = new JLabel("Full Name:");
lblFullName.setForeground(Color.WHITE);
lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblFullName.setBounds(235, 132, 76, 14);
add(lblFullName);
JLabel lblGender = new JLabel("Gender: ");
lblGender.setForeground(Color.WHITE);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblGender.setBounds(235, 168, 60, 14);
add(lblGender);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBackground(Color.DARK_GRAY);
rdbtnMale.setFont(new Font("Tahoma", Font.PLAIN, 13));
rdbtnMale.setForeground(Color.WHITE);
rdbtnMale.setBounds(326, 164, 68, 23);
add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBackground(Color.DARK_GRAY);
rdbtnFemale.setFont(new Font("Tahoma", Font.PLAIN, 13));
rdbtnFemale.setForeground(Color.WHITE);
rdbtnFemale.setBounds(396, 164, 77, 23);
add(rdbtnFemale);
ButtonGroup genderRdBtn = new ButtonGroup();
genderRdBtn.add(rdbtnFemale);
genderRdBtn.add(rdbtnMale);
JLabel lblAdmissionNo = new JLabel("Admission No:");
lblAdmissionNo.setForeground(Color.WHITE);
lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblAdmissionNo.setBounds(235, 203, 97, 14);
add(lblAdmissionNo);
JLabel lblContactNo = new JLabel("Contact No:");
lblContactNo.setForeground(Color.WHITE);
lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblContactNo.setBounds(235, 240, 77, 14);
add(lblContactNo);
JLabel lblDiploma = new JLabel("Diploma In:");
lblDiploma.setForeground(Color.WHITE);
lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblDiploma.setBounds(235, 275, 76, 14);
add(lblDiploma);
txtbxFullName = new JTextField();
txtbxFullName.setBounds(326, 130, 147, 20);
add(txtbxFullName);
txtbxFullName.setColumns(10);
txtAdmissionNo = new JTextField();
txtAdmissionNo.setBounds(326, 201, 147, 20);
add(txtAdmissionNo);
txtAdmissionNo.setColumns(10);
txtbxContactNo = new JTextField();
txtbxContactNo.setBounds(326, 238, 147, 20);
add(txtbxContactNo);
txtbxContactNo.setColumns(10);
txtFieldDiploma = new JTextField();
txtFieldDiploma.setBounds(326, 273, 147, 20);
add(txtFieldDiploma);
txtFieldDiploma.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
btnSubmit.setBounds(235, 336, 114, 23);
add(btnSubmit);
JButton btnCancel = new JButton("CANCEL");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
btnCancel.setBounds(359, 336, 114, 23);
add(btnCancel);
JButton btnBrowse = new JButton("Browse");
btnBrowse.setBounds(101, 247, 89, 23);
add(btnBrowse);
}
}
Here is the Profile Page :
package Project;
import java.awt.Color;
import Project.ProfileEdit;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Project.ProfileEdit;
public class Home extends MasterPanel {
/**
* Create the panel.
*/
public Home(JFrame mf) {
super(mf);
JLabel lblUserProfile = new JLabel("");
//createTitledBorder(border, string, int, int, font, color)
lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE));
//setBounds(left, top, right, bottom)
lblUserProfile.setBounds(60, 103, 510, 267);
add(lblUserProfile);
JLabel lblDisplayPic = new JLabel("");
lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
lblDisplayPic.setIcon(new ImageIcon(Home.class.getResource("/javaProject/images/default-avatar.png")));
lblDisplayPic.setBounds(108, 142, 90, 100);
add(lblDisplayPic);
JLabel lblFullName = new JLabel("Full Name:");
lblFullName.setForeground(Color.WHITE);
lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblFullName.setBounds(251, 131, 76, 14);
add(lblFullName);
JLabel lblGender = new JLabel("Gender: ");
lblGender.setForeground(Color.WHITE);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblGender.setBounds(251, 151, 60, 14);
add(lblGender);
JLabel lblAdmissionNo = new JLabel("Admission No:");
lblAdmissionNo.setForeground(Color.WHITE);
lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblAdmissionNo.setBounds(251, 176, 97, 14);
add(lblAdmissionNo);
JLabel lblContactNo = new JLabel("Contact No:");
lblContactNo.setForeground(Color.WHITE);
lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblContactNo.setBounds(251, 195, 77, 14);
add(lblContactNo);
JLabel lblDiploma = new JLabel("Diploma In:");
lblDiploma.setForeground(Color.WHITE);
lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblDiploma.setBounds(251, 218, 76, 14);
add(lblDiploma);
JButton btnEdit = new JButton("Edit ");
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ProfileEdit ProfileEdit = new ProfileEdit(mf);
mf.setContentPane(ProfileEdit);
mf.setVisible(true);
}
});
btnEdit.setBounds(251, 309, 89, 23);
add(btnEdit);
}
}
and Lastly, here is the Master Panel if you guys need it :
package Project;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import java.awt.Color;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MasterPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
protected JFrame myFrame = null;
/**
* Create the panel.
*/
public MasterPanel(JFrame mf) {
myFrame = mf;
setBackground(Color.DARK_GRAY);
setLayout(null);
//set a new menubar for home
JMenuBar menuBarHome = new JMenuBar();
menuBarHome.setForeground(Color.BLACK);
menuBarHome.setBackground(Color.WHITE);
// x,y,width,height
// >x = right, <x = left
// >y = bottom, <y = up
menuBarHome.setBounds(80, 40, 97, 21);
//length and height
menuBarHome.setSize(48, 20);
add(menuBarHome);
JMenu home = new JMenu("HOME");
home.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
menuBarHome.add(home);
//set a new menubar for calendar
JMenuBar menuBarCalendar = new JMenuBar();
menuBarCalendar.setForeground(Color.BLACK);
menuBarCalendar.setBackground(Color.WHITE);
menuBarCalendar.setBounds(145, 40, 97, 21);
menuBarCalendar.setSize(75, 20);
add(menuBarCalendar);
JMenu calendar = new JMenu("CALENDAR");
calendar.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
Calendar calendar = new Calendar(mf);
mf.setContentPane(calendar);
mf.setVisible(true);
}
});
menuBarCalendar.add(calendar);
JMenuBar menuBarAcademic = new JMenuBar();
menuBarAcademic.setForeground(Color.BLACK);
menuBarAcademic.setBackground(Color.WHITE);
menuBarAcademic.setBounds(235, 40, 220, 21);
menuBarAcademic.setSize(72, 20);
add(menuBarAcademic);
JMenu academic = new JMenu("ACADEMIC");
menuBarAcademic.add(academic);
//add sub menu into the academic
JMenuItem lectureNotes = new JMenuItem("Lecture Notes");
lectureNotes.setForeground(Color.BLACK);
lectureNotes.setBackground(Color.WHITE);
lectureNotes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LectureNotes LectureNotes = new LectureNotes(mf);
mf.setContentPane(LectureNotes);
mf.setVisible(true);
}
});
academic.add(lectureNotes);
JMenuItem resulttracker = new JMenuItem("Result Tracker");
resulttracker.setForeground(Color.BLACK);
resulttracker.setBackground(Color.WHITE);
resulttracker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ResultTracker resulttracker = new ResultTracker(mf);
mf.setContentPane(resulttracker);
mf.setVisible(true);
}
});
academic.add(resulttracker);
JMenuItem studyplan = new JMenuItem("Study Planner");
studyplan.setForeground(Color.BLACK);
studyplan.setBackground(Color.WHITE);
studyplan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StudyPlanner studyplanner = new StudyPlanner(mf);
mf.setContentPane(studyplanner);
mf.setVisible(true);
}
});
academic.add(studyplan);
JMenuItem timetable = new JMenuItem("Timetable");
timetable.setForeground(Color.BLACK);
timetable.setBackground(Color.WHITE);
timetable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TimeTable timetable = new TimeTable(mf);
mf.setContentPane(timetable);
mf.setVisible(true);
}
});
academic.add(timetable);
JMenuBar menuBarEmail = new JMenuBar();
menuBarEmail.setForeground(Color.BLACK);
menuBarEmail.setBackground(Color.WHITE);
menuBarEmail.setBounds(320, 40, 97, 21);
menuBarEmail.setSize(47, 20);
add(menuBarEmail);
JMenu email = new JMenu("EMAIL");
menuBarEmail.add(email);
JMenuItem composeemail = new JMenuItem("Compose");
composeemail.setForeground(Color.BLACK);
composeemail.setBackground(Color.WHITE);
composeemail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Email email = new Email(mf);
mf.setContentPane(email);
mf.setVisible(true);
}
});
email.add(composeemail);
JMenuItem inboxemail = new JMenuItem("Inbox");
inboxemail.setForeground(Color.BLACK);
inboxemail.setBackground(Color.WHITE);
email.add(inboxemail);
JMenuBar menuBarContactus = new JMenuBar();
menuBarContactus.setForeground(Color.BLACK);
menuBarContactus.setBackground(Color.WHITE);
menuBarContactus.setBounds(380, 40, 97, 21);
menuBarContactus.setSize(85, 20);
add(menuBarContactus);
JMenu contactus= new JMenu("CONTACT US");
contactus.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
ContactUs contactus = new ContactUs(mf);
mf.setContentPane(contactus);
mf.setVisible(true);
}
});
menuBarContactus.add(contactus);
JMenuBar menuBarLogout = new JMenuBar();
menuBarLogout.setForeground(Color.BLACK);
menuBarLogout.setBackground(Color.WHITE);
menuBarLogout.setBounds(480, 40, 97, 21);
menuBarLogout.setSize(60, 20);
add(menuBarLogout);
JMenu logout = new JMenu("LOGOUT");
logout.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
Login login = new Login(mf);
mf.setContentPane(login);
mf.setVisible(true);
}
});
menuBarLogout.add(logout);
JSeparator separatorTop = new JSeparator();
separatorTop.setBounds(50, 23, 524, 2);
add(separatorTop);
JSeparator separatorBottom = new JSeparator();
separatorBottom.setBounds(50, 79, 524, 2);
add(separatorBottom);
}
}

Why isn't my JScrollPane working?

I'm trying to make my JPanel scrollable but it's not working. I made a JPanel, added components to it, then added my JPanel to a JScrollPane. This is what you're supposed to do, right? What am I doing wrong?
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;
public class RegisterPane extends JPanel {
private JTextField txtJohnDoe;
private JTextField txtExampledomaincom;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
/**
* Create the panel.
*/
public RegisterPane() {
setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(6, 36, 300, 450);
panel.setLayout(null);
JLabel lblFirstName = new JLabel("First Name");
lblFirstName.setBounds(28, 6, 92, 16);
panel.add(lblFirstName);
txtJohnDoe = new JTextField();
txtJohnDoe.setText("John Doe");
txtJohnDoe.setBounds(124, 0, 251, 28);
panel.add(txtJohnDoe);
txtJohnDoe.setColumns(10);
txtExampledomaincom = new JTextField();
txtExampledomaincom.setText("Example#domain.com");
txtExampledomaincom.setColumns(10);
txtExampledomaincom.setBounds(124, 40, 251, 28);
panel.add(txtExampledomaincom);
JLabel lblEmail = new JLabel("Email");
lblEmail.setBounds(28, 46, 92, 16);
panel.add(lblEmail);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(28, 89, 55, 16);
panel.add(lblGender);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(124, 85, 92, 23);
panel.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(283, 85, 92, 23);
panel.add(rdbtnFemale);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(28, 157, 55, 16);
panel.add(lblPassword);
JLabel passDirections = new JLabel();
passDirections.setHorizontalAlignment(SwingConstants.CENTER);
passDirections.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
passDirections.setBackground(SystemColor.window);
passDirections.setText("Password's Must be at Least 6 characters long and contain 1 non letter character");
passDirections.setBounds(29, 117, 346, 28);
panel.add(passDirections);
passwordField = new JPasswordField();
passwordField.setBounds(124, 151, 251, 28);
panel.add(passwordField);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(124, 195, 251, 28);
panel.add(passwordField_1);
JLabel lblRetypePassword = new JLabel("Password Again");
lblRetypePassword.setBounds(28, 201, 92, 16);
panel.add(lblRetypePassword);
JCheckBox chckbxSubscribeToNews = new JCheckBox("Subscribe to News Letter");
chckbxSubscribeToNews.setSelected(true);
chckbxSubscribeToNews.setHorizontalAlignment(SwingConstants.CENTER);
chckbxSubscribeToNews.setBounds(29, 244, 346, 23);
panel.add(chckbxSubscribeToNews);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(28, 280, 55, 16);
panel.add(lblNewLabel);
JScrollPane scroll = new JScrollPane(panel);
scroll.setSize(new Dimension(450,300));
panel.setAutoscrolls(true);
add(scroll);
}
}
Here's my JFrame class
import java.awt.BorderLayout;
public class MainFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setResizable(false);
setContentPane(new RegisterPane());
//RegisterPane isn't scrolling ^
}
}
Thanks in Advance for your help!
I'm seeing setLayout(null) alot in your code.
The JViewport uses the component's/view's preferred size as a bases for determining if the view expands beyond the visible bounds of the JScrollPane, because you've seen fit to ignore this feature, the components have begun to break down.
Swing is designed to work layout managers and it makes it much easier to develop complex user interfaces that can work across a multitude of platforms and environments

How do I remove a component from a JPanel? and then redisplay the frame?

Some context for this code,
what I am trying to do is create a validation warning if the input containts "" as a value,
so if an error is displayed it displays the message.
If it is valid it does not display a message,
so how do I remove the message when the textField is valid?
The message is a JPanel that contains a JLabel with the text in it,
I add this this JPanel to the frame when it is not valid,
and I am trying to remove it when it is valid.
So what am I doing wrong here?
I am at a basic level with Swing.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 401, 232);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 330, 94);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Firstname :");
lblNewLabel.setBounds(10, 11, 104, 14);
panel.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(76, 8, 244, 20);
panel.add(textField);
textField.setColumns(10);
JLabel lblLastname = new JLabel("Lastname :");
lblLastname.setBounds(10, 42, 78, 14);
panel.add(lblLastname);
textField_1 = new JTextField();
textField_1.setBounds(76, 39, 244, 20);
panel.add(textField_1);
textField_1.setColumns(10);
JButton btnValidate = new JButton("Validate");
btnValidate.addMouseListener(new MouseAdapter() {
#SuppressWarnings("deprecation")
#Override
public void mousePressed(MouseEvent arg0) {
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
if(textField.getText().equals("")) {
panel_1.setBackground(new Color(30, 144, 255));
panel_1.setBounds(100, 116, 330, 26);
JLabel lblMessage = new JLabel("0 :");
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));
panel_1.add(lblMessage);
frame.getContentPane().add(panel_1);
frame.revalidate();
frame.repaint(10);
frame.revalidate();
}
else if(textField_1.getText().equals("")) {
panel_2.setBackground(new Color(50, 200, 255));
panel_2.setBounds(10, 134, 330, 26);
JLabel lblMessage = new JLabel("1 :");
lblMessage.setBounds(50, 50, 50, 50);
lblMessage.setAlignmentX(50);
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));
panel_2.add(lblMessage);
frame.getContentPane().add(panel_2);
frame.remove(panel_1);
frame.revalidate();
frame.repaint(10);
frame.revalidate();
}
}
});
btnValidate.setBounds(231, 71, 89, 23);
panel.add(btnValidate);
}
}
The easiest way is to simply adjust the visibility (JComponent#setVisible( false ) ).
If you really want to remove the component completely, you have to remove and revalidate, as documented in the Container#remove method
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.
which results in code like
panel.remove( componentToRemove );
panel.revalidate();
panel.repaint();
As a side note: please replace the null layout and those setBounds call by a proper LayoutManager. You might want to take a look excellent 'Nested layout example' available on SO to see what is possible with layout managers. The Swing tag info on SO contains some extra useful links when starting to work with layout managers
yourpanel.setVisible(false); should hide your panel, where "yourPanel" is your JPanel instance

Categories

Resources