I have a class that is extending JInternalFrame which also holds a JDesktopPane that should hold another JInternalFrame which can be added on a button click.
I can log that the JInternalFrame is constructed, but it is not shown on its parent JInternalFrame and I dont know why.
Here are the relevant sections:
Parent class
this.add(topPanel,BorderLayout.NORTH);
this.add(tableScrollpane, BorderLayout.CENTER);
this.add(desktop,BorderLayout.SOUTH); //The parents Desktop pane to which I want to add internalframes on button click
The method that should add a new JInternalFrame to the desktop (JDesktopPane of parent)
private void addChart(Market market){
this.desktop.add(new Chart(market.getSymbol(),this.quoteMarket, Indicators.convertToTa4JBarSeries(market.getSymbol(),market.getCandles())));
}
The JInternalFrame class that should be created and visible on the parents desktop pane. The constructor gets called, I can see that in the log section.
public class Chart extends JInternalFrame {
public Chart(String title, String quoteMarket,BarSeries barSeries){
System.out.println("Chart created");
setTitle(title + " " + quoteMarket);
setSize(500,500);
setMaximizable(true);
setIconifiable(true);
setClosable(true);
setResizable(true);
setLayout(new GridLayout(0,1));
this.displayChart(title,quoteMarket,barSeries);
this.pack();
this.show();
this.setVisible(true);
}
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.
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 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
I want a button to change the contentPane in a frame. An admin panel and a user panel in two different JPanel classes that gets called in a MainWindow class with a frame.
My code looks as follows:
The method the button calls:
public void ChangeContent() throws Exception{
MainWindow mw = new MainWindow();
AdminUI admin = new AdminUI ();
mw.frame.getContentPane().removeAll();
mw.frame.setContentPane(admin);
mw.frame.revalidate();
mw.frame.repaint();
}
This is the method being called in my "public MainWindow()" method in the MainWindow class (the one with the frame) to show the contentPane in the beginning:
public void Admin() throws Exception {
frame = new JFrame("Admin Panel");
frame.setResizable(false);
frame.setBounds(100, 100, 256, 457);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AdminUI Admin = new AdminUI();
frame.setContentPane(Admin);
}
An equal method exists for the userUI.
I have tried just called the userUI() method in the MainWindow when I press a button, but that doesn't refresh the window, and neither does the ChangeConten() method. I need some help. I used the search feature on Stackoverflow to no success.
I need to write a simple tennis game.
To move between different windows(panel with main menu, panel with game, panel with settings) I decided to use inner classes extends JPanel and replace it when some events like start new game occurs.
but the problem is - it doesn't see my inner class. I mean I add it to JFrame
mainframe.add(new MainMenuPanel());
but there is nothing on the screen when I run program. What's the problem?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainFrame{
JFrame mainframe;
public static void main(String[] args){
new MainFrame();
}
public MainFrame() {
mainframe = new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(300, 400);
mainframe.setTitle("X-Tennis v0.1");
mainframe.add(new MainMenuPanel());
mainframe.getContentPane().setLayout(new GridLayout());
mainframe.getContentPane().setBackground(Color.WHITE);
mainframe.setVisible(true);
}
public class MainMenuPanel extends JPanel {
JPanel mainmenupanel;
JLabel label1;
JButton btnNewGame,btnJoinGame;
ImageIcon iconNewGame,iconJoinGame;
public MainMenuPanel(){
mainmenupanel = new JPanel();
label1 = new JLabel("X-TENNIS");
label1.setFont(new Font("Comic Sans MS",Font.ITALIC,20));
label1.setForeground(Color.BLUE);
btnNewGame = new JButton("New Game", iconNewGame);
btnNewGame.setFocusPainted(false);
btnNewGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(mainframe, "New game");
//delete current panel and add another to mainframe
}
}
);
btnNewGame.setPreferredSize(new Dimension(140,30));
btnJoinGame = new JButton("Join game",iconJoinGame);
mainmenupanel.add(label1);
mainmenupanel.add(btnNewGame);
}
}
}
There is no need for mainmenupanel within the MainMenuPanel class as MainMenuPanel is a JPanel itself
Simple add all the components in MainMenuPanel directly to itself
You create a new JPanel, mainmenupanel, inside MainMenuPanel but never add that to the container itself. You could do
add(mainmenupanel);
If you intend for this JPanel to occupy the full area of the parent, then you can simply add your components directly to your instance of MainMenuPanel as indicated by #Mad
First you should add your component to the ContentPane. In Swing, all the non-menu components displayed by the JFrame should be in the ContentPane.
mainframe.getContentPane().add(new MainMenuPanel());
Edit: I was wrong about the content pane, see #MadProgrammer comment.
Then you have to add the JPanel that you create in MainMenuPanel to the MainMenuPanel instance itself.
add(mainmenupanel);
But you should probably get rid of that intermediary container itself and add your labels to the MainMenuPanel instance itself:
add(label1);
add(btnNewGame);
mainmenupanel.add(label1);
mainmenupanel.add(btnNewGame);
try this :
super.add(label1);
super.add(btnNewGame);