Java Swing: Force focus on child Panel/Frame - java

private void jBtnAboutActionPerformed(java.awt.event.ActionEvent evt) {
jPanel_About.setSize(300, 300);
jPanel_About.setVisible(true);
}
How can I make User unable to select the main jFrame where jBtn_About is located while the About Panel pop-up?
Also, the code does show a new panel but it's created at top left corner of display. How can i allign it to be created in middle of the main jFrame?
Thank you very much!

You can use JDialog (as modal window) instead of jPanel_About.
For JDialog you can use setLocation to place it in desired position.

Related

GUI components disappearing when resizing

I'm writing a program that trades out the main component of the GUI when a button is pressed. To do this, I have multiple classes extending a component (JScrollPane) that can then be placed into my main class, which extends JFrame.
JScrollPane menu;
public MenuSystem()
{
menu = new OpeningMenu(this);
setSize(500,500);
setLocationRelativeTo(null);
setTitle("Menu system");
setDefaultCloseOperation(EXIT_ON_CLOSE);
loadInfo();
pane = getContentPane();
pane.setLayout(null);
pane.add(menu);
setVisible(true);
}
public void changeMenuTo(JScrollPane x)
{
pane.remove(menu);
menu = x;
pane.add(menu);
pane.repaint();
}
These bits of code control which JScrollPane is displayed on the GUI. I face two problems that I believe are related.
When I start the program, some of the components in the initial JScrollPane do not appear. I have several buttons and a JLabel in this pane, but only the first button appears. The rest of the buttons will appear when my mouse passes over them, but until then, they are invisible. This seems strange, because it suggests that my constructor is somehow at fault. when bringing up the same pane with the changeMenuTo() method, it appears perfectly.
Whenever I manually resize the screen, all components in the current pane disappear. It happens almost exactly like when I first start the program, buttons are invisible until my mouse passes over them. In this case the first button is also invisible.
Changing the layout has fixed the issue. Thanks for the suggestions.

add image in Jpanel using canvas

i want to add image to my panel which is created by Netbeans GUI Designer.
here's my canvas class
private static class GraphicsClass extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Toolkit tkit;
tkit=getToolkit();
Image img=tkit.getImage("ampera.jpg");
g.drawImage(img,0,0,this);
}
}
and this is how i use it in JFrame class constructor
this.Jpanel1=new GraphicsClass();
but the image doesnt show. How to fix it?
this.Jpanel1=new GraphicsClass();
Won't work. Assuming you just drag and dropped a panel onto the frame and named it Jpanel1, then in the constructor you did the above code. When you first drag and drop the original panel, it gets initialized and laid out. When you do the above code, you are making a new component. The original panel is still the one added, and is not the same referenced panel and the new one you just created. So the new panel will never show, it is just the one (with no reference identifier). Like if you has something like
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
jPanel1 = new GraphicsClass();
}
private void initComponent() {
jPanel1 = new JPanel();
// add jPanel1 to frame
}
private class GraphicsClass extends JPanel {}
private JPanel jpanel1;
}
That's the basic netbeans setup. The code in the initComponents method is autogenerated and is not editable by default. You could edit it, but I would advise against it (unless you really know what you're doing). If you look at the code, you will see exactly what I wan talking about in the beginning.
Fix: Created a panel form from the designer (i.e. New->Swing Form->JPanel form). Then you can simply drag and drop the panel to the frame, as seen here
Other note:
Don't create your images in the paintComponent method. Make it a class member and create it in the constructor.
Instead of a panel, if you want to create an image (say for a background), you can use an ImageIcon and a JLabel and set the layout manager to the label, You can then start adding component onto the label.
Drag and drop an label
Go to the properties of the label and click the ... of the icon property. In the dialog, you can browse for an image.
Then right click the label from the design view, and set a layout manager for the label. You can then start adding components to the label.
You can fix this by using the BufferedImage class, or using the MediaTracker to track when your image has loaded

Disable maximise in jFrame and resizeable using mouse

JFrame with hidden or no maximize button but should be able to re-size using mouse(clicking and dragging on jFrame border). setResizable(false) is only disabling the minimize button but not able to re-size using mouse.
I personally can't think of a reason to allow resize and not allow maximize but here is an example of how to prevent maximizing a JFrame while still allowing resize and minimize. Tested in windows, untested on all other platforms. Full screen flash is minimized using setMaximizedBounds().
final JFrame jFrameNoMax = new JFrame() {
{
setMaximizedBounds(new Rectangle(0, 0));
addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(final WindowEvent e) {
if (e.getNewState() == MAXIMIZED_BOTH) {
setExtendedState(NORMAL);
}
}
});
}
};
// Tester
jFrameNoMax.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrameNoMax.setSize(300, 300);
jFrameNoMax.setLocation(300, 300);
jFrameNoMax.setVisible(true);
You can take the following steps:
-Right click on JFrame
-Select properties
-Uncheck the resizable checkbox
-Close properties
-Run the program
See the attached illustration:
One option could be to use a JDialog instead of a JFrame. This allows the window to be manually resizeable but not maximizeable. The only problem with doing this is that you lose both the minimize and maximize buttons. This may or may not be a problem for your application.

How to make a JFrame blurred in java?

In my java program, when the user clicks on a button in the main JFrame another JFrame becomes visible on top of the main frame. Now how to make the previous frame blurred?
Exactly like the background in the image :
Now how to make the previous frame blurred?
have look at JLayer(Java7) based on JXLayer(Java6),
similair effect is possible with GlassPane, or with JViewport (not proper of ways, but good Swing GUI contains JScrollPane as 1st. JComponent in JFrame / JDialog / JWindow)
I'm assuming that you are coding using Java Swing, and the picture is the effect you want.
You would use a JDialog to create the top window, rather than a JFrame. A JDialog is designed to be modal. In other words, lock the JFrame until the JDialog is closed.
The default behavior of a JDialog is to not blur or darken the underlying JFrame. In order to darken the underlying JFrame, you're going to have to paint a black image on the glass pane of the JFrame.
This tutorial shows you how to manipulate the root panes of a JFrame.
There is the setOpacity(float opacity) method that is available in JFrame (inherited from Frame actually).

How can I Change the default Icon of a JPanel based swing app (or window)

I've seen many answers for this question when it's a JFrame, but none for JPanel, and all that I've tried didn't work.
So basically I've written this simple class/app that extends JPanel, and all is working fine. Now I'd like to change the Default Icon.
Any ideas?
Just as guys are saying here in comments please reconsider what you are trying to do.
The only option to change an icon is to set it for the frame in which the panel is child, since the icon is a part/belongs to the frame.
If you want setting of the icon to be a functionality of a panel then in addNotify() method, which is called when a component receives a parent, look through the panel's parent and it parent and so on until you will reach the frame and set the icon for it.
Sample showing a number of parent you must go through to get to frame if a panel is its content pane.
JPanel p = new JPanel();
JFrame f = new JFrame();
f.setContentPane(p);
System.out.println(SwingUtilities.windowForComponent(p));

Categories

Resources