How to minimize JFrame from another JPanel? - java

How can I minimize a JFrame if I am in another JPanel that I add to this JFrame?
Here I add the JPanel that is in another class to the JFrame:
panel = new Panel_Processos("Caixa de SaĆ­da", 3);
setContent(pane, panel);
public void setContent(JPanel pane, JPanel panel){
pane.removeAll();
pane.setVisible(true);
pane.validate();
pane.add(panel);
contentPane.validate();
}
But the problem is: How can I do something to the JFrame after an event that happens in the JPanel?

Related

I try to figure out how to write a program, when I press the button on top and display some information on the bottom panel

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class InitialPanel extends JPanel implements ActionListener{
private JButton but;
TopPanel top= new TopPanel();
BottomPanel bot = new BottomPanel();
public InitialPanel(){
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
top.getDisplayPosition().addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String a=top.getFp1().getPosition();
but=new JButton(a);
bot.setB1(but);
add(bot, BorderLayout.CENTER);
}
}
here is the code, the top panel will have a button and when I press it, It should change the information in the button for bottom panel and display it. But I cannot make the bottom panel change
But I cannot make the bottom panel change
When you add/remove components from a panel the basic structure is:
panel.add(....);
panel.revalidate();
panel.repaint();
The revalidate() will invoke the layout manager otherwise the newly added component has a size of (0, 0) so there is nothing to paint.

java: Why does frame get the event of a component

I have a JFrame, which contains just a JPanel.
I add an mouse event to the JPanel.
But the whole JFrame gets the same mouse event.
Here is my code:
public class TestSwing extends JPanel {
public TestSwing() {
super.setSize(20, 20);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new TestSwing());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
I set the size of the JPanel (20, 20), but no matter where I click, the mouse event will always be triggered.
You're adding that JPanel to the JFrame's contentPane, a component that uses BorderLayout, a layout which does not respect size or even for the most part preferred size, and so the JPanel will fill the entire JFrame's contentPane. Give the contentPane a FlowLayout and set the JPanel's preferred size and you'll see a difference. In the future, give the JPanel a Border to see its boundaries. This will make debugging this easy.
If you don't tell it otherwise, your JFrame will have BorderLayout like this:
Now if you furthermore don't tell your Panel where to go, it wil go into CENTER and thus be resized to fill whole content area.
For checking MouseEvent on only one JPanel I suggest you add another JPanel (maybe with different background?) to other area:
frame.getContentPane().add(new TestSwing(), BorderLayout.CENTER);
JPanel left = new JPanel();
left.setBackground(Color.red);
frame.getContentPane().add(left, BorderLayout.LEFT);
Size set by setSize() is only valid until it's recalculated based on Layout and other properties.

Can't align JFileChooser to the left of BorderLayout.NORTH

I have a window with a JFileChooser and a JTextArea.
The JFileChooser is in the NORTH part of the BorderLayout.
The JTextArea is in the CENTER part of the BorderLayout.
I would like to align to the left ALL my JFileChooser, but it won't move like I want and stay CENTERED.
Furthermore, I would like my JFileChooser take all the length of my window.
EDIT
Here is the main code
public class MainServer
{
public static void main(String[] args)
{
ServerBoard frame=new ServerBoard(1000, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Here is the window code
public class ServerBoard extends JFrame
{
private JButton startserver;
private JButton senddata;
private JButton sendgps;
private JTextArea messagearea;
public ServerBoard(int l, int h)
{
super("ServerBoard");
this.initialize();
this.setSize(l,h);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initialize()
{
// Define a panel
Container c=this.getContentPane();
this.messagearea=new JTextArea(40,60);
c.add(this.createNorth(), BorderLayout.NORTH);
c.add(messagearea, BorderLayout.CENTER);
}
public JPanel createNorth()
{
JPanel panelnorth=new JPanel();
JToolBar toolbarnorth=new JToolBar();
panelnorth.add(toolbarnorth);
this.startserver=new JButton("START SERVER");
startserver.addActionListener(new ServerBoardListener());
toolbarnorth.add(startserver);
this.senddata=new JButton("SEND DATA");
senddata.addActionListener(new ServerBoardListener());
toolbarnorth.add(senddata);
this.sendgps=new JButton("SEND GPS FRAME");
sendgps.addActionListener(new ServerBoardListener());
toolbarnorth.add(sendgps);
return panelnorth;
}
}
Here is my window
I really really want to use this JFileChooser. Can you help me please ?
In advance thank you a lot for the answers.
Simply nest your JPanels. Create a new JPanel, say called northPanel, that uses a BorderLayout, and add it to the main window in the BorderLayout.NORTH position, and then add your JFIleChooser to this northPanel JPanel in its BorderLayout.WEST position.
Option 2: give the northPanel a BoxLayout that is oriented along the line axis, add the JFileChooser, and add glue.

jpanel as inner class

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);

JPanel on JWindow, adding components

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.

Categories