I need to add a notification to my swing application to display only for incomming messages, I try with JPanel inside the Jframe but when I this the main window is covered.
Here is a part of my code
public class MainUI extends JFrame {
private void constructUI() {
setLayout(new BorderLayout());
add(getDesktopPane(), BorderLayout.CENTER);
add(getStatusBar(), BorderLayout.SOUTH);
add(getNotificationsPanel());
}
}
And my idea of notification:
The notification is a JPanel but it is blocking the view of the JFrame.
The thing is: calling add(component); on a component with BorderLayout is the same as calling add(component, BorderLayout.CENTER);.
That's why your notifications panel is blocking the center view of the frame.
More info here: https://docs.oracle.com/javase/8/docs/api/java/awt/BorderLayout.html
Related
public class Create_JFrame extends JFrame{
public Create_JFrame(){
//Create a Frame
JFrame Frame = new JFrame("Bla-Bla");
JPanel Panel_1 = new JPanel();
JPanel Panel_2 = new JPanel();
JButton Option_1 = new JButton("Option-1");
//Layout management for Panels
Frame.getContentPane().add(BorderLayout.WEST, Panel_1);
//Add button to Panel
Panel_1.add(Option_1);
//Registering Listeners for all my buttons
Option_1.addActionListener(new ListenerForRadioButton(Panel_2));
//Make the frame visible
Frame.setSize(500, 300);
Frame.setVisible(true);
}//end of Main
}//end of Class
public class ListenerForRadioButton implements ActionListener{
JPanel Panel_2;
JButton browse = new JButton("Browse");
//Constructor, will be used to get parameters from Parent methods
public ListenerForRadioButton(JPanel Panel){
Panel_2 = Panel;
}
//Overridden function, will be used for calling my 'core code' when user clicks on button.
public void actionPerformed(ActionEvent event){
Panel_2.add(browse);
System.out.println("My listener is called");
}//end of method
}//end of class
Problem Statement:
I have 2 JPanel components in a a given JFrame. Panel_1 is having a Option_1 JButton. When user clicks on that I am expecting my code to add a JButton 'browse' in Panel_2 at runtime.
Runtime Output:
System is not adding any JButton in Panel_2. However, I see my debug message in output, indicating that system was successful in identifying user's click action on 'option-1'.
Question:
Why is JPanel not adding any component at Runtime?
Panel_2.add(browse);
Panel_2.revalidate();
adding a 'revalidate' will solve the problem.
There are some reasons. but:
usually it's because of using unsuitable LayoutManager.
sometimes it's because of adding the JPanel to it's root component in worng way. which any operation (add, remove,...) works but is not visible.
you must refresh the view when you make some changes on it, like adding or removing components to/from it.
try to use Panel_2.revalidate() to refresh.
if it doesn't work properly use it with Panel_2.repaint() method.
see Java Swing revalidate() vs repaint()
see Difference between validate(), revalidate() and invalidate() in Swing GUI
using setSize twice for your jframe is another way.
Frame.setSize(498, 300); then Frame.setSize(500, 300);
I'm writing a simple java game and I'm facing this problem:
My different layouts are in different JPanels (1 JPanel for the welcoming page, where I have to press 'start game' and another one with the actuall functionallity)
I start the game from a JFrame
import javax.swing.JFrame;
public class RType extends JFrame {
public RType() {
add(new Welcome());//first panel
add(new Board());//panel with the game
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new RType();
}
}
obviuosly, this launches the second panel right after the first, and I cant see the first one.
I've tried some stuff, trying to invoke the second panel in the main method, when the first panel is clicked that way:
RType rt=new RType();
rt.add(new Board()); //in this case add(new Board()); is removed from constructor
but it's doing nothing.
how can I solve it?
As #nachokk has said, you should be using a CardLayout instead. It lets you do things like tabs in a browser, but you don't need to make the tabs visible for your game if you don't want to. You make your welcome "card" visible at first, then when the user clicks you switch to the Board "card".
I don't recommend to add directly to the JFrame components, instead use another container as JPanel . JFrame default layout is BorderLayout, when you add in the way you are adding it always put in the center.
Make something like this:
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new CardLayout());
mainPanel.add(new Welcome(), "Welcome");
mainPanel.add(new Board(),"Board");
frame.add(mainPanel);
Here is a tutorial How to use CardLayout
on first panel of welcome add a button, and apply actionperformed like
JButton myButton = new JButton("Add Component ");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(welcome);
frame.add(Board, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
});
By default, both panels will fill up the entire Frame's area. To fix this, you will need to use another layout, such as a GridLayout to structure the areas in which the panels will take up the Frame's area.
You can also go with no layout to hard code the pixel values of where you want the panels to fit in your frame.
EDIT: Based on what you're looking to do, the CardLayout is probably what you'll want to use for your Frame's layout.
I have a layout with a main panel whose content is decided by me throught buttons in this way:
public void actionPerformed(ActionEvent e) {
mainPanel.removeAll(); //removing all current panel components
if(e.getActionCommand().equals("content1")){
mainPanel = new Content1Panel();
add(mainPanel,BorderLayout.CENTER);
validate();
}else if(e.getActionCommand().equals("content2")){
mainPanel = new Content2Panel();
add(mainPanel,BorderLayout.CENTER);
validate();
}
}
now, if I minimize the frame and then bring it to front I find all main panel's components disappeared! Anyway, when I hover on them with the mouse components show again.
What is wrong?
for JFrame / JDialog / JWindow
to use
validate(); (revalidate in Java7 )
repaint();
example about standard rulles
use CardLayout instead of remove and then add the JPanel to the container
I have a JFrame, inside the JFrame code I create a JWindow and on window I have created a JPanel. On JPanel is inserted a background image.
JButton btnImage= new JButton("My Button");
Image splashImg = Toolkit.getDefaultToolkit().getImage("images/image1.jpeg");
JPanel pnlSplashWindow= new JPanel(){
public void paint(Graphics g){
g.drawImage(splashImg,0,0,splashImg.getWidth(this),splashImg.getHeight(this),this);
}
};
pnlSplashWindow.setLayout(new BorderLayout());
pnlSplashWindow.add(BorderLayout.SOUTH,btnImage);
JWindo window= new JWindow(this); // this refers to my class which has extended JFrame
window.setContentPane(pnlSplashWindow);
window.setSize(688, 344);
btnImg.setVisible(true);
window.setLocationRelativeTo(this);
I am new to JWindow and have the following questions:
How to add elements like buttons and labels on JWindow (or JPanel which is on the JWindow)?
How to set my JFrame as the parent of this JWindow? I mean while JWindow is active, the JFrame should not be clickable.
An example of the desired end effect
To add components you should use:
pnlSplashWindow.add(btnImage, BorderLayout.SOUTH);
instead. And if you don't want your JFrame to be clickable, you should use a modal JDialog , by extending JDialog instead of JWindow.
But if you want to create a Splash Screen, you should read How to create a Splash Screen.
I'm creating an applet which consists of a class which extends JApplet, with a menubar and a class which extends a JPanel.(So there is a menubar and a JPanel shown in the applet).
In this class I add and remove some textfields to the JPanel. This all works fine. Here's where it gets tricky: it only works the first time. When I add some new textfields to the JPanel, they are added and visible in the JPanel, but the menubar in the JFrame stops working.
Since the code is too extensive I'll only post parts of it.
Here's the code where I add the JPanel to the JApplet:
public class Simulator extends JApplet implements ItemListener, ActionListener {
Container pane = getContentPane();
canvas = new DrawCanvas();
pane.add(canvas, BorderLayout.LINE_END);
}
Here's the code of the JPanel:
class DrawCanvas extends JPanel {
public void paintComponent(Graphics g) {
if(textfield != null)
remove(textfield);
textfield = new JTextField();
this.add(textfield);
}
}
This works the first time(when nothing is removed), but the second time the menubar stops working.
When I leave out the this.add(textfield); line, the menubar keeps working.
I once had similar problems with popup menus beeing painted behind other components.
Try calling static JPopupMenu.setDefaultLightWeightPopupEnabled(false); or the setLightWeightPopupEnabled on your specific submenu. This will make (all) popup menus (i.e. submenus) to heavy weight components that have a native peer.
I believe you are running into issues with threading. Adding and removing JComponents during painting might mess up the EDT (which is calling the paint method in the first place).