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.
Related
I am a beginner in coding and I am learning Java. I'm busy making a log in system, and I have made a JFrame, but when I add a JButton, it takes up the whole JFrame.
public class LogInSystem extends Application{
#Override
public void start(Stage primaryStage)
{
// Setting the JFrame
JFrame frame = new JFrame("Log in System");
frame.setSize(2000, 2000);
frame.setVisible(true);
// Setting the button
JPanel panel = new JPanel();
Button btn1 = new Button();
btn1.setText("1");
btn1.setBounds(50, 150, 100, 30);
frame.add(btn1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
It seems like you declare a panel and not adding button into it as well as not adding the panel to the frame. You can try to add your button into panel and panel into frame,
panel.add(btn1);
frame.add(panel);
You can also use some useful layout for a particular panel. For example, BoxLayout, GridLayout and etc. By default, everything is set to be FlowLayout.
A quick and simple(?) question I can't seem to find an answer to.
Is it possible to draw a shape (rectangle, oval etc) and add to the JPanel, then add this JPanel to the JFrame? The examples of drawing with Graphics I found online added the shape directly to the JFrame. Example:
public class DShape extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GREEN);
g.drawRect(10,10,100,30);
}
public class Test {
public static void main(String[] args)
{
JFrame frame = new JFrame();
DShape shape = new DShape();
JPanel panel = new JPanel();
panel.add(shape);
frame.add(panel);
frame.setSize(200,200);
frame.setVisible(true);
}
}
This code will simply display a blank JFrame. A green rectangle will be displayed if you add a DShape class object directly to the JFrame. Is it possible to add the shape to JPanel first, then add JPanel to JFrame? Thank you.
The examples of drawing with Graphics I found online added the shape directly to the JFrame.
When you add the panel to the frame directly the default layout of the content pane is a BorderLayout, so the component gets resized to the size of the frame.
Is it possible to draw a shape (rectangle, oval etc) and add to the JPanel, then add this JPanel to the JFrame?
JFrame frame = new JFrame();
DShape shape = new DShape();
JPanel panel = new JPanel();
panel.add(shape);
frame.add(panel);
The default layout manager for a JPanel is the FlowLayout. A FlowLayout respects the preferred size of a component added to it. Your component doesn't have a preferred size so the size is (0, 0) so there is nothing to paint.
You need to override the getPreferredSize() method of your DShape panel to return the appropriate size.
Read the section from the Swing tutorial on Custom Painting for more information and working example.
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 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'm trying to open a window that has both an image and buttons in it. But I can't seem to figure out how to add the button. The image displays great and the menu works fine, but no matter where I add the button (into the JLabel, JPanel, or JFrame), it doesn't ever show...
Main:
public static void main(String[] args) {
GUI myGUI = new GUI();
myGUI.show();
}
GUI class: openImage is called when using the menu. The image then displays, but no button.
private JFrame myFrame;
private JPanel myPanel;
private JLabel myLabel;
public GUI()
{
myFrame = new JFrame();
initializePanel();
}
public void show()
{
myFrame.setSize(600,600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addMouseListener(this);
setupMenu(myFrame);
myFrame.setVisible(true);
}
private void initializePanel()
{
myPanel = new JPanel();
myPanel.setPreferredSize(new Dimension(500,500));
//myPanel.setLayout(new BorderLayout());
}
private void openImage(String fileName)
{
try {
myImage = ImageIO.read(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myLabel = getJLabel();
JButton button = new JButton("ButtonClick");
button.addActionListener(this);
myFrame.setContentPane(myLabel);
myPanel.add(button);
myFrame.getContentPane().add(myPanel);
myFrame.pack();
myFrame.setSize(600,600);
}
private void setupMenu(JFrame window) {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
file.add(open);
menubar.add(file);
window.setJMenuBar(menubar);
}
Your main issue is your setting the contentPane to be a JLabel -- don't do this! The contentPane needs to be opaque, needs to be built to be easily used as a Container and in your case, really should be a JPanel. JLabel I believe uses a null layout so it's no surprise that your code shows no button. If you want to show a background image, make have myPanel constructed from an anonymous class that extends JPanel, override the paintComponent method in this class (calling super.paintComonent first in the method), and draw the image in this method. Then you can add components to the contentPane which will now use a FlowLayout (the default for a JPanel) and it will be opaque by default.
Also, if your goal is to swap items displayed in your GUI, use a CardLayout to do the swapping for you as this layout makes swapping components a breeze.
really don't know, depends of method(s) how you are added picture to the JLabel, JPanel, or JFrame, but maybe for simle Container that contains a few, only one-two JComponents is there crazy idea, without side effects, with idea to display picture and to add there JButton:
JLabel is very similair JComponent to the JPanel, and is by default translucent and very simple implements Icon/ImageIcon, then you'll only to call myLabel.setIcon(myPicture)
to the all of JComponents you are/could be able to add another JComponent by using some of LayoutManager (Box, Flow, GridBagLayout)
You tried to set the label as the content pane and then tried to add the panel to that image which doesn't make sanse at all.
Change it so you add the label to the panel and have the panel as content pane:
Like this:
You have this line which is the problem. It doesn't make much sense:
myFrame.setContentPane(myLabel);
Try instead:
myFrame.getContentPane().add(myLabel);