How should I create a "panel" outside JFrame? - java

I am trying to create a GUI with a main window for input, and a separate box to show output below the main window. However, the width of the output box will be different from the main window and I don't want the difference in width to be part of the frame (meaning that the difference should be invisible and user can click through the empty space to the back of the application).
But I also want to output box to be shown as 1 window with the main window. I can set the position of the output box with a listener to make it stick to the main window, but I don't want it to be shown as a separate window in the Windows(Microsoft) task bar. I also don't want the output box to be selectable
as a result of being a separate window.
Therefore is there any method to create a JPanel outside JFrame, or is there a way I can do this with JFrame and make it work with my constraints? Any other methods?

Show the separate JPanel in a window as a non-modal JDialog. You can make it undecorated if you don't want the menu buttons on the top right.
I'm not sure what you mean by:
(meaning that the difference should be invisible and user can click through the empty space to the back of the application).
Regarding
I also don't want the output box to be selectable as a result of being a separate window.
Make sure that you have no focusable components in the dialog, or if focusable, have the focusable property set to false.
Edit
To prevent the dialog window from being focused, add a line:
dialog.setFocusableWindowState(false);
For example:
import java.awt.Dimension;
import javax.swing.*;
public class DialogNoFocus {
public DialogNoFocus() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
DialogNoFocus mainPanel = new DialogNoFocus();
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(Box.createRigidArea(new Dimension(400, 300)));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "JDialog", false);
dialog.getContentPane().add(Box.createRigidArea(new Dimension(500, 100)));
int x = frame.getLocation().x;
int y = frame.getLocation().y + frame.getHeight();
dialog.setLocation(x, y);
dialog.pack();
dialog.setFocusableWindowState(false);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

JDialog
How about to put 2 internal Frames(one is decorated, another one - undecorated und not selectable) or 1 internal Frame and panel in one big translucent undecorated Frame?
At first you will get only one icon in the Windows(Microsoft) task bar. Internal frames wont show up in the task bar.
The main frame should be translucent : how to set JFrame background transparent but JPanel or JLabel Background opaque?

Related

How can I make a JFrame to go fullscreen after clicking the default fullscreen button?

I want to build a JFrame with a default size. But After the default fullscreen button is clicked, the frame will cover the full screen of monitor & what's inside the frame will readjust itself with the new frame size.
My question is how can I do that?
Here is the code i have written so far.
public class ApplicationFrame extends JFrame{
private static ApplicationFrame ref;
private ApplicationFrame(){
ref=this;
this.setTitle("Business Management");
this.setBounds(50, 50, 1400, 800);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static ApplicationFrame getRef(){
if(ApplicationFrame.ref==null)
new ApplicationFrame();
return ref;
}
}
**Here is the picture of the button I am talking about
JFrame will do this automatically when you click the button, unless you prevent it from doing it (for example, by set frame.setMaximumSize).
However, for the contents of the JFrame to render properly, you need to correctly use the LayoutManagers (FlowLayout, BoxLayout, etc).
By default, JFrame uses BorderLayout, which allows you to add components to the borders (left, right, top, bottom, center).
A good practice is to add a JPanel on the border, and add your components inside this JPanel.

Closing a JFrame Opened in Another JFrame

I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.
The problem is when I want to close the second JFrame I need to enable the first JFrame.
I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them. I need to reach both goals:
Disabling the first Frame when the second Frame is opened
Enabling it when the second Frame is closed.
Here is part of my code:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
if (!pjf.isActive()){
this.setEnabled(true);
}
}
This code dose not enable the First Frame at all.
I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:
//Class in first Frame
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
}
//Class in second Frame
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
FinancialDocumentsJFrame.setEnableValue(true);
}
Dose any one know how can I reach these goals?
The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.
I ma using netBeans IDE designer.
First of all, a Swing application should only have one JFrame, other windows can be JDialog or somethign else. As for your question, use this code as an example. It uses a listener to detect the closing event of the second window. The following code should be in the (first) JFrame (it looks like you have a button there)
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
JDialog paymentDialog = new JDialog();
MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);
paymentDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
MyFirstFrame.this.setEnabled(true);
}
});
}
You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code. Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.
And further still, there is the setAlwaysOnTop(boolean) for Swing windows.
use this.dispose()method JFrame has dispose() method for JFrame Close
I decided to use jDialog as it was recommended by many people on the net.
I used a jDialog and copied all the objects I had used in the second jFrame.
It worked the way I wanted it to do.
My only problem is that why java hasn't prepared a different way to answer this need.
I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ActivityJDialog ajdf = new ActivityJDialog(this,true);
ajdf.setVisible(true);
}
And I have copied everything I wanted to have into this jDialog.
public class ActivityJDialog extends java.awt.Dialog {
//Document Attributes
int documentStatus = -1;
int documentType = -1;
/**
* Creates new form AcrivityJDialog
* #param parent
* #param modal
*/
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
/*if(false) // Full screen mode
{
// Disables decorations for this frame.
//this.setUndecorated(true);
// Puts the frame to full screen.
this.setExtendedState(this.MAXIMIZED_BOTH);
}
else // Window mode
{*/
// Size of the frame.
this.setSize(1000, 700);
// Puts frame to center of the screen.
this.setLocationRelativeTo(null);
// So that frame cannot be resizable by the user.
this.setResizable(false);
//}
}
Thank you all for helping.

Modal dialog not always on top of an undecorated JFrame when another JFrame is visible

I have a weird problem with modal dialogs and undecorated JFrame.
If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.
But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.
This problem doesn't happen:
if the main frame is decorated
or if the second frame is not visible
EDIT
I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32
The code I used to test:
public static void main(String[] args) {
// creates main frame and set visible to true
final JFrame mainFrame = new JFrame();
mainFrame.setUndecorated(true); // if I comment this line, everything goes well
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(500, 500));
mainFrame.setVisible(true);
// creates a dialog and set visible to true
JFrame anotherFrame = new JFrame();
anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
// display a modal dialog
JOptionPane.showMessageDialog(mainFrame, "A message");
}
But, if create another JFrame (or another JDialog), the modal dialog
still prevent me to interact with the main frame, but now the modal
dialog is not always on top and go underneath the main frame when I
click on it.
not true at all, both are not accesible untill JOptioPane is visible
JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM
there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS
code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
private JFrame mainFrame = new JFrame();
private JFrame anotherFrame = new JFrame();
public Main() {
mainFrame.setUndecorated(true);
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(100, 60));
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anotherFrame.setVisible(true);
anotherFrame.setLocation(110, 0);
anotherFrame.setSize(new Dimension(100, 60));
anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(mainFrame, "A message");
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
}

Change JPanel contents in JDialog

I'm trying to add components to a JDialog after it has been created and displayed. Nothing I try makes the changes actually update to the screen, and I've read and applied every question I could find related to this.
This example code creates a modal JDialog showing the word "test". I cannot get it to display "test2". Almost exactly the same code but with a JFrame instead of a JDialog behaves as I expect, so I don't understand. I'm new to Java and especially to swing.
import javax.swing.*;
public class DialogTester {
public static void main(String[] args) {
new DialogTester();
}
public DialogTester() {
JFrame jframe = new JFrame();
jframe.setVisible(true);
JDialog jdialog = new JDialog(jframe,true);
JPanel jpanel = new JPanel();
jpanel.add(new JLabel("test"));
jdialog.add(jpanel);
jdialog.setVisible(true);
jpanel.add(new JLabel("test2"));
jpanel.revalidate();
jdialog.getContentPane().validate();
jdialog.pack();
}
}
I also tried calling
jdialog.repaint();
which did nothing.
You created a modal dialog. So, as soon as you call setVisible(true), the following instructions wait for the dialog to be closed to be executed.
Put the code adding a label before the dialog is made visible, or put it in an event handler called after the dialog is shown, for example, when you click on a button in this dialog.

Problem with setVisible(boolean)

I have a JFrame with some component on it. I want that the frame disappears when i click on a special button for example exit button.
I wrote this code in exit button
this.setvisible(false);
but it only hides the component on it and frame doesn't disappear.
What can I do that when I click on exit button the frame disappears?
Here's an example of a button that hides the frame:
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
final JButton hideButton = new JButton("hide frame");
frame.add(hideButton);
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.setVisible(true);
frame.pack();
In your call this.setVisible(false), this probably refers to the button and not the frame.
You need to call setVisible() on the Frame not the Button.
Also make sure you are calling dispose() on the frame to clean up all resources.
Additionally you should also use
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
during creation of the frame, to make sure the windows is properly closed and disposed when the user clicks the "standard" close button in the upper right corner (on Windows).
This tutorial might also help you understand what's going on better:
http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
call it on JFrame object.
example:
// when exit is pressed
fr.setVisible(false); // fr is a reference to object of type JFrame `

Categories

Resources