I am making a graphic program with swing that involves placing various panels with components on the frame. One of them is a panel with buttons that, when an option in an options menu is chosen, is entirely replaced with a different one that has different buttons. We've been thinking and it seems that the best way to do this would be to, each time the option is selected, to rebuild the frame from scratch.
This is the snippet of code in which I'm building the main panels:
import java.awt.Color;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import com.jtattoo.plaf.graphite.GraphiteLookAndFeel;
import com.nacher.calc.Main;
import com.nacher.calc.controller.Controller;
import net.miginfocom.swing.MigLayout;
/**
*
* #author chema && Sergio
* This Panel contains buttons and textbox to perform the calculator actions
*/
public class PanelCalculator extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = -8377215712645516042L;
private PanelNumbers panelNumbers;
private PanelScreen panelScreen;
private PanelOptions panelOptions;
private PanelScientific panelScientific;
public Controller controller;
public PanelCalculator(Controller controller) {
this.controller = controller;
//Initialize JTattoo Library
try{
UIManager.setLookAndFeel(new GraphiteLookAndFeel());
}catch(UnsupportedLookAndFeelException ulafe){
System.out.println("JTattoo Graphite failed to set");
}
this.setLayout(new MigLayout("wrap"));
buildComponents();
situateComponents();
//controller = new Controller();
controller.addPropertyChangeListener(panelScreen);//panelScreen escucha al controller
panelOptions.addPropertyChangeListener(controller);//controller escucha al panelOptions
panelNumbers.addPropertyChangeListener(controller);//controller escucha al panelNumbers
panelScreen.addPropertyChangeListener(controller);//controller escucha al panelScreen
}
private void buildComponents(){
this.setPreferredSize(new Dimension(210, 280));
this.setBorder(new EmptyBorder(0, 0, 0, 0));
panelOptions = new PanelOptions();
panelOptions.setBorder(new EmptyBorder(0, 0, 0, 0));
panelOptions.setPreferredSize(new Dimension(200, 50));
panelScreen = new PanelScreen();
panelScreen.setBorder(BorderFactory.createEtchedBorder());
panelScreen.setPreferredSize(new Dimension(200, 40));
panelScreen.setBackground(new Color(255, 255, 255));
panelNumbers = new PanelNumbers();
panelNumbers.setBorder(new EmptyBorder(0, 0, 0, 0));
panelNumbers.setPreferredSize(new Dimension(200, 185));
panelOptions.addPropertyChangeListener(panelScreen);//panelScreen escucha al panelOptions
panelNumbers.addPropertyChangeListener(panelScreen);//panelScreen escucha al panelNumbers
}
private void situateComponents(){
this.add(panelOptions, "span");
this.add(panelScreen, "span");
this.add(panelNumbers, "span" );
}
/**
* To build the Scientific Calculator
*/
private void buildScientific() {
//Instantiate the panelScientific
panelScientific = new PanelScientific();
panelScientific.setBorder(new EmptyBorder(0, 0, 0, 0));
panelScientific.setPreferredSize(new Dimension(200, 185));
//Reordering the Panel Calculator
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
String c = evt.getPropertyName();
if (c.equals(Constants.SET_CALC_SCIENTIFIC)) {
System.out.println("Set the Scientific Calculator");
buildScientific();
}
}
}
And this is the main class where we build the frame:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.jtattoo.plaf.bernstein.BernsteinLookAndFeel;
import com.jtattoo.plaf.fast.FastLookAndFeel;
import com.jtattoo.plaf.graphite.GraphiteLookAndFeel;
import com.jtattoo.plaf.hifi.HiFiLookAndFeel;
import com.jtattoo.plaf.luna.LunaLookAndFeel;
import com.nacher.calc.controller.Controller;
import com.nacher.calc.ui.Constants;
import com.nacher.calc.ui.ImageConstants;
import com.nacher.calc.ui.PanelCalculator;
public class Main extends JFrame implements PropertyChangeListener {
private static final long serialVersionUID = -4136829057174783241L;
private static Main mySelf;
private static PanelCalculator panelCalc;
public static Controller controller;
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
controller = new Controller();
panelCalc = new PanelCalculator(controller);
controller.addPropertyChangeListener(panelCalc);
mySelf = new Main();
try {
mySelf.setTitle("Calculator");
mySelf.setLocationRelativeTo(null);
mySelf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mySelf.setIconImage(new ImageIcon(mySelf.getClass().getResource(ImageConstants.CALC_IMAGE_THREE)).getImage());
mySelf.getContentPane().add(panelCalc);
mySelf.setResizable(false);
mySelf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.out.println("Quitting the application.");
}
});
mySelf.pack();
mySelf.setVisible(true);
controller.addPropertyChangeListener(mySelf);
}catch(Exception ex) {
System.out.println("The application could not run.");
System.out.println(ex.getMessage());
System.exit(-1);
}
}
What commands should I use to 'unbuild' the frame, then rebuild it from scratch? Is there a more efficient way to replace one panel with another?
The Card Layout was designed for this type of functionality. It allows you to define multiple panels to share the same space of the frame. You then just specify which panel is currently displayed.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
I don't know if I totally get your problem, but if, that would be my solution (just a simple example):
public class Main extends JFrame{
public Main(){
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
Panels panels = new Panels();
add(panels, BorderLayout.CENTER);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
}
.
public class Panels extends JPanel{
public JPanel p1, p2;
public Panels(){
p1 = new JPanel();
add(p1);
p2 = new JPanel();
add(p2);
//p1 and p2 might be more complex Components
//place them using your preferred LayoutManager
//(set their preferredSize etc.)
}
public void updateP1(JPanel np1){
remove(p1);
p1 = np1;
add(p1);
}
public void updateP2(JPanel np2){
remove(p2);
p2 = np2;
add(p2);
}
}
I think what you're looking for is the method remove(Component comp) of a JComponent, i.e. a JPanel. Hope that helps.
Related
I was working on an overlay for a JPanel but I have a slight problem, the transparency of the overlay sees straight through to the JFrame.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class GUI extends JFrame {
private GUI() {
super("Recorder Part");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JLayeredPane layers = new Overlay();
add(layers);
pack();
getContentPane().setBackground(Color.GREEN);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new GUI());
}
private class Overlay extends JLayeredPane {
JPanel base;
JPanel overlay;
private Overlay() {
setPreferredSize(new Dimension(800, 100));
createBase();
createOverlay();
add(base, new Integer(0));
add(overlay, new Integer(1));
}
private void createBase() {
base = new JPanel();
base.setPreferredSize(new Dimension(800, 100));
base.setBounds(0, 0, 800, 100);
base.setBackground(Color.BLUE);
base.setOpaque(true);
base.add(new JLabel("Hello"));
}
private void createOverlay() {
overlay = new JPanel();
overlay.setPreferredSize(new Dimension(800, 100));
overlay.setBounds(0, 0, 800, 100);
overlay.setBackground(new Color(255, 0, 0, 0));
}
}
}
How can I fix this so that when the JPanel called overlay is transparent, the Jpanel called base can be seen and not the JFrame?
Edit
I found that this problem only occurs when the overlay panel has one dimension that is greater than or equal to the dimensions of the base panel. This can be seen by adjusting the size of the frame in this example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class GUI extends JFrame {
private GUI() {
super("Recorder Part");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JLayeredPane layers = new Overlay();
add(layers);
pack();
getContentPane().setBackground(Color.GREEN);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new GUI());
}
private class Overlay extends JLayeredPane {
JPanel base;
JPanel overlay;
private Overlay() {
addComponentListener(new Resize());
setPreferredSize(new Dimension(800, 100));
createBase();
createOverlay();
add(base, new Integer(0));
add(overlay, new Integer(1));
}
private void createBase() {
base = new JPanel();
base.setLocation(0, 0);
base.setBackground(Color.BLUE);
base.setOpaque(true);
base.add(new JLabel("Hello"));
}
private void createOverlay() {
overlay = new JPanel();
overlay.setLocation(0, 0);
overlay.setSize(new Dimension(800, 100));
overlay.setBackground(new Color(255, 0, 0, 128));
}
private class Resize extends ComponentAdapter {
#Override
public void componentResized(ComponentEvent e) {
System.out.println("Resized");
base.setSize(new Dimension(getParent().getWidth(), getParent().getHeight()));
}
}
}
}
This is undesirable as the overlay panel needs to be the exact same size of the base panel. I would appreciate any help.
I have the felling that this is a bug. Change the bounds so it doesn't fully overlap the base to o see what happens:
overlay.setBounds(0, 0, 799, 100); // or (1, 0, 800, 100)
probably some kind of optimization, like ignoring a component if it is being completely obscured by another component, but without considering transparency in that optimization [:-|
EDIT:
that is definitively the problem - paintChildren of JComponent does some kind of optimization based on children being totally obscured by other children.
I found two solutions, the first one is probably the correct one- set the overlay to non-opaque:
overlay.setOpaque(false);
this has the drawback that the background color is not being used at all.
Second is more a workarround - make the JLayeredPane return true for optimizedDrawingEnabled, this will stop the JComponent from doing it:
private class Overlay extends JLayeredPane {
/// ...
#Override
public boolean isOptimizedDrawingEnabled() {
return true;
}
}
but not sure what may stop working by doing that, so I would prefer the first solution!
list is to accept input from Action1 this works, however, whenever a new element is added to the list, the list's position moves back to the default top-middle position.
This also occurs when the frame is resized, so as a temporary fix I the line frame.setResizable(false) but I do not want that to be permanent.
How would I fix both of these issues?
import static java.lang.String.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class lists
{
static String newUrl;
static DefaultListModel<String> model = new DefaultListModel<String>();
static int listXCoord = 650;
static int listYCoord = 10;
public static void createGUI()
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
frame.add(panel);
JButton addurl = new JButton("Add URL");
panel.add(addurl);
addurl.addActionListener(new Action1());
JButton remurl = new JButton("Remove URL");
panel.add(remurl);
//model.addElement("one");
//model.addElement("two");
//model.addElement("three");
JList list = new JList<String>(model);
list.setCellRenderer(new DefaultListCellRenderer());
list.setVisible(true);
list.setLocation(listXCoord, listYCoord);
list.setBackground(new Color(186, 203, 250));
//list.setLocation(650, 10);
panel.add(list);
list.setSize(130, 540);
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
newUrl = JOptionPane.showInputDialog("Enter the URL to be Launched");
model.addElement(newUrl);
}
}
public static void main(String[] args)
{
createGUI();
}
}
Basically, you're fighting the layout manager (Flowlayout) and losing. When you add a new element to the JList, the container hierarchy is been revalidated which is causing the layout managers to re-layout the contents of their containers
The basic solution would be to use a different layout, but, JFrame uses a BorderLayout, so instead of adding the JList to the JPanel, you could simply add it to the EAST position of the frame instead
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Lists {
static String newUrl;
static DefaultListModel<String> model = new DefaultListModel<String>();
static int listXCoord = 650;
static int listYCoord = 10;
public static void createGUI() {
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton addurl = new JButton("Add URL");
panel.add(addurl);
addurl.addActionListener(new Action1());
JButton remurl = new JButton("Remove URL");
panel.add(remurl);
//model.addElement("one");
//model.addElement("two");
//model.addElement("three");
JList list = new JList<String>(model);
list.setCellRenderer(new DefaultListCellRenderer());
list.setVisible(true);
list.setLocation(listXCoord, listYCoord);
list.setBackground(new Color(186, 203, 250));
//list.setLocation(650, 10);
frame.add(new JScrollPane(list), BorderLayout.EAST);
frame.setVisible(true);
}
static class Action1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
newUrl = JOptionPane.showInputDialog("Enter the URL to be Launched");
model.addElement(newUrl);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
createGUI();
}
});
}
}
See Laying Out Components Within a Container, How to Use BorderLayout and How to use FlowLayout for more details.
You should also be calling setVisible last, after all the components have been added to the frame, this reduces the possibilities that some of your components won't be displayed when you think they should be.
JList will also benefit from been contained within a JScrollPane. See How to Use Lists and How to Use Scroll Panes for more details
How can I close a JTabbedPane with an JInternalFrame included when I click a button inside this form?
My project has 3 JForms, 1 TabbedPane, 1 JInternalFrame and 1 Main window (to register data). To create my JTabbedPanes I use the method addTab() and from that form I have the button to close which calls another window (Main registry), I used the dispose() and setVisible(false) methods but they don't do anything.
An example (there are 3 clases -Administrador, -MenuPrincipal, -ventanaRegistro) :
First Class a JFrame and on top a JTabbedPane (this is where I create my tabs)
public class MenuPrincipal extends javax.swing.JFrame
{
Administrador ventanaAdministrador = new Administrador();
void showTabs()
{
JTabbedPane.addTab("Administrador", ventanaAdministrador);
}
public MenuPrincipal()
{
initComponents();
showTabs();
}
}
Second Class a JInternalFrame with a single button
public class Administrador extends javax.swing.JInternalFrame
{
void showAdminWindow()
{
ventanaRegistro ventanaRegistro = new ventanaRegistro();
ventanaRegistro.setVisible(true);
}
void closeThisWindow()
{
this.dispose();
}
public Administrador()
{
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
showAdminWindow();
closeThisWindow();
}
}
Third Class a simple JFrame. This is just code to register data.
The real problem is that my method closeThisWindow() doesn't close the second Jtabbed window, and I want to make it that when I click a button (which is on the JTabbedPane) make the third class visible and the Second Class invisible/Close it.
See if the following code does what you expect. If it doesn't please elaborate so I can improve my answer.
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
public class MenuPrincipal extends javax.swing.JPanel {
Administrador ventanaAdministrador = new Administrador();
public MenuPrincipal()
{
setPreferredSize(new Dimension(290, 200));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
ventanaAdministrador.setPreferredSize(new Dimension(350, 100));
tabbedPane.addTab("Admin", ventanaAdministrador);
tabbedPane.addTab("2nd Tab", new JPanel());
add(tabbedPane);
}
/**
* #param args
*/
public static void main(String[] args) {
JFrame testFrame = new JFrame("Main Test Window");
testFrame.setDefaultCloseOperation(WindowConstants.
DISPOSE_ON_CLOSE);
testFrame.setPreferredSize(new Dimension(450, 350));
MenuPrincipal menuP = new MenuPrincipal();
testFrame.getContentPane().add(menuP);
testFrame.pack();
testFrame.setVisible(true);
}
}
And:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Administrador extends javax.swing.JInternalFrame {
void showAdminWindow()
{
JFrame ventanaRegistro = new JFrame("Admin Window");
ventanaRegistro.
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
ventanaRegistro.setPreferredSize(new Dimension(350, 200));
ventanaRegistro.pack();
ventanaRegistro.setVisible(true);
}
void closeThisWindow()
{
Window window = SwingUtilities.getWindowAncestor(this);
window.dispose();
}
public Administrador()
{
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
JButton closeWindowBttn = new JButton("Close Window and open a new one");
closeWindowBttn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
closeWindowActionPerformed();
}
});
panel.add(closeWindowBttn);
}
private void closeWindowActionPerformed() {
showAdminWindow();
closeThisWindow();
}
}
I've stumbled upon this complications and have spent more than 4 hours debugging and googling but to no avail..
Basically what I have here is 1 JFrame, 2 JPanels.
I had my JFrame setContentPane to 1 of the JPanel, and when I run the Application, the JFrame will appear with the JPanel inside.
Now this JPanel have 1 JButton inside it, when I click it I want it to switch to another JPanel. As you can see from the code, when I click the JButton(Add Product), I want the OnlineShopAdPane to switch to AddProduct. I tried using CardLayout but it only has NSEW formatting.
package OnlineShop.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OnlineShopMainFrame extends JFrame {
/**
* Launch the application.
*/
AddProduct Add;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OnlineShopMainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
OnlineShopAdPane AdPanel = new OnlineShopAdPane();
setContentPane(AdPanel);
}
}
package OnlineShop.ui;
import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
public class OnlineShopAdPane extends JPanel {
/**
* Create the panel.
*/
public OnlineShopAdPane() {
JLabel lblWhatDoYou = new JLabel("What do you want to do?");
lblWhatDoYou.setBounds(28, 26, 160, 26);
add(lblWhatDoYou);
JButton btnAddProduct = new JButton("Add Product");
btnAddProduct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.removeAll();
MainFrame.add(new AddProduct());
MainFrame.revalidate();
MainFrame.repaint();
}
});
btnAddProduct.setBounds(46, 75, 115, 23);
add(btnAddProduct);
}
}
package OnlineShop.ui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddProduct extends JPanel {
private JTextField textField;
/**
* Create the panel.
*/
public AddProduct() {
JLabel lblProductName = new JLabel("Product Name:");
lblProductName.setBounds(35, 26, 77, 24);
add(lblProductName);
JLabel lblProductDescription = new JLabel("Product Description:");
lblProductDescription.setBounds(10, 50, 106, 24);
add(lblProductDescription);
textField = new JTextField();
textField.setBounds(116, 28, 141, 20);
add(textField);
textField.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setBounds(116, 66, 141, 112);
add(textArea);
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnClose.setBounds(223, 244, 89, 23);
add(btnClose);
}
}
I tried using CardLayout but it only has NSEW formatting.
What does that mean? A CardLayout simply contains two or more panels. Only one panel is visible at a time. Each panel can use whatever layout it wants to layout the components on the panel.
when I click it I want it to switch to another JPanel.
That is exactly what CardLayout does. See the Swing tutorial on How to Use Card Layout for a working example and explanation.
Whenever I see code like remove/add/revalidate/repaint it should almost always be replaced with a CardLayout
I think that with CardLayout you can resolve it, but another way is using for example a Handler to switch your panels.
private JComponent container; // this could be your Frame
private JComponent loadedComponent;
public void loadContent(JComponent component, Object object ) {
if (loadedComponent != null) {
loadedComponent.setVisible(false);
container.remove(loadedComponent);
loadedComponent = null;
}
//TODO may check layout
container.add(component,object);
component.setVisible(true);
loadedComponent = component;
container.validate();
}
The problem is pobably located at the following lines in class OnlineShopAdPane.java
OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.removeAll();
MainFrame.add(new AddProduct());
MainFrame.revalidate();
MainFrame.repaint();
your not referring to the frame where your JPanel is nested. instead your creating a new OnlineShopMainFrame
I am making a simple game using a JFrame. I have made a simple "Start" screen which basically consists of a String and a JButton. I am picking up the button click with the actionPerformed(ActionEvent e) method. I don't know how to change the cards using a button click. This may seem like a simple problem to solve, but the twist comes with this: My main JFrame, my StartScreen and my JPanel where the game takes place are all in separate files. My main file, Virus.java, is where I create the JFrame. My file VirusGamePanel.java is where the game takes place. My file StartScreen.java is the screen with the button. I want to change 'cards' to the game screen when the player clicks the button. How can I do this?
My StartScreen.java file:
package virus;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
public class StartScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton start = new JButton("Start");
public StartScreen(){
start.addActionListener(this);
start.setBounds(new Rectangle(400,300,100,30));
this.add(start);
}
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("Impact",Font.BOLD,72));
g.setColor(Color.MAGENTA);
g.drawString("Virus",275,300);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
}
}
}
My Virus.java file:
package virus;
import javax.swing.*;
import java.awt.CardLayout;
import virus.StartScreen;
public class Virus extends JFrame{
private static final long serialVersionUID =1L;
JFrame jf = new JFrame("Virus");
static JPanel thegame = new JPanel(new CardLayout());
JPanel game = new VirusGamePanel();
JPanel start = new StartScreen();
public Virus(){
jf.setResizable(false);
jf.setSize(600,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setVisible(true);
jf.add(thegame);
thegame.add(start);
thegame.add(game);
}
public static void main(String[] args) {
new Virus();
}
}
You simply have to right this in your actionPerformed(...) method :
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
cardLayout.next(Virus.thegame);
}
}
As very much pointed out by #kleopatra (THE EMPRESS) herself, don't override paint() instead do your painting stuff inside paintComponent(Graphics g) method of any JPanel/JComponent. Moreover, first add the components to your JFrame, once it's size is realized, then only set it to Visible, not before that. Instead of setting sizes for the JFrame simply override the JPanel's method getPreferredSize(), make it return some valid Dimension Object.
Do watch this sequence, as you write your code the next time :
public Virus(){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
thegame.add(start);
thegame.add(game);
jf.add(thegame);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
Here is your full code :
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
public class Virus extends JFrame{
private static final long serialVersionUID =1L;
JFrame jf = new JFrame("Virus");
static JPanel thegame = new JPanel(new CardLayout());
JPanel game = new VirusGamePanel();
JPanel start = new StartScreen();
public Virus(){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
thegame.add(start);
thegame.add(game);
jf.add(thegame);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
public static void main(String[] args) {
new Virus();
}
}
class StartScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton start = new JButton("Start");
public StartScreen(){
start.addActionListener(this);
start.setBounds(new Rectangle(400,300,100,30));
this.add(start);
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("Impact",Font.BOLD,72));
g.setColor(Color.MAGENTA);
g.drawString("Virus",275,300);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(600, 600));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
cardLayout.next(Virus.thegame);
}
}
}
class VirusGamePanel extends JPanel
{
public VirusGamePanel()
{
JLabel label = new JLabel("I am ON", JLabel.CENTER);
add(label);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(600, 600));
}
}
Your StartScreen class has to have access to the instance of the CardLayout of the JFrame and the instance of the VirusGamePanel class. You can pass these instances in the constructor or a setLayout method and setVirusGamePanel method of your StartScreen class.
Something like:
layout.next(virusGamePanel);
should work.