I have a dialog with a wizard containing one wizard page. The wizard page appears differently for different screen resolution. Some contents of wizardpage is missed if the it is executed in laptop or desktop with different screen resolution.
I have set the wizard page size in the dialog:
dialog.setPageSize(700, 700);
But eventhough this is not working properly. Please let me know is there any way so that wizard page gets adjusted with screen resolution changes.
Thanks in advance.
In your WizardPage you can reset the size of the dialog to match the preferred size of the contents using something like this:
private void recalcSize()
{
Composite dialogAreaComp = (Composite)getControl();
Shell shell = getShell();
Point shellSize = shell.getSize();
dialogAreaComp.layout(true, true);
Point newSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
if (newSize.x != shellSize.x || newSize.y != shellSize.y)
shell.setSize(newSize);
}
Put a call to recalcSize in the setVisible method of the page.
Related
I need to get current active window.
I have used KeyboardFocusManager, for getting active window. But i am getting active window is null.
below is the code.
please provide any way to get current active window.
KeyboardFocusManager currentManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Window activeWindow = currentManager.getActiveWindow();
This single line of code should work:
Window activeWindow = javax.swing.FocusManager.getCurrentManager().getActiveWindow();
from the JavaDoc:
Returns the active Window, if the active Window is in the same context
as the calling thread. Only a Frame or a Dialog can be the active
Window. The native windowing system may denote the active Window or its
children with special decorations, such as a highlighted title bar.
The active Window is always either the focused Window, or the first
Frame or Dialog that is an owner of the focused Window.
to get the GlobalActiveWindow, call:
javax.swing.FocusManager.getCurrentManager().getGlobalActiveWindow();
JavaDoc:
Returns the active Window, even if the calling thread is in a different context than the active Window. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.
Note:
When your application does not have the focus, this method returns null!
Cheers!
Found the following code and it worked fine for me:
Window getSelectedWindow(Window[] windows) {
Window result = null;
for (int i = 0; i < windows.length; i++) {
Window window = windows[i];
if (window.isActive()) {
result = window;
} else {
Window[] ownedWindows = window.getOwnedWindows();
if (ownedWindows != null) {
result = getSelectedWindow(ownedWindows);
}
}
}
return result;
}
And you can call it like this using the static method of class Window:
Window w = getSelectedWindow(Window.getWindows());
Good Luck.
Almost forgot to mention, I've found the recursive method in this site.
I took the rendered page from the SWT Browser and exported it to an image. My problem is that I am not able to get it to export properly when the shell is not visible. How can I go about hiding the browser and have the image export properly?
I have tried setting shell.Visible() to false but that messes up the image export.
This is how I export the image (not sure if this is necessary to the question):
GC source = new GC (shell);
Image image = new Image(display, browser.getClientArea());
source.copyArea(image, 0, 0);
ImageLoader io = new ImageLoader ();
io.data = new ImageData[] { image.getImageData() };
File f = new File (currentDir+"/workpng.png");
io.save (f.getAbsolutePath(), SWT.IMAGE_PNG);
This might be impossible because the X server/Windows will throw away all rendering commands when the window isn't visible (no point in rendering what you can't see).
Also what is the client area of the browser in this case?
To make this work, you'll need to allow the shell to open be visible (i.e. not hidden by some other window). Tools like Jenkins use a plugin that starts an X session with Xvfb or Xvnc. After setting the env variable DISPLAY, all UI rendering goes to these sessions.
I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?
A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.
Drag and drop a JLabel into the Form Builder.
Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon.
Select External Image and click the ellipsis.
Select the image of choice. In my case it's a .png.
Notice that the image appears in the icon preview.
Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder.
Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.
I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.
Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.
package com.example.resource.icons;
import javax.swing.ImageIcon;
public class IconFetch {
private static IconFetch instance;
private IconFetch(){
}
public static IconFetch getInstance() {
if (instance == null)
instance = new IconFetch();
return instance;
}
public ImageIcon getIcon(String iconName) {
java.net.URL imgUrl = getClass().getResource(iconName);
if (imgUrl != null) {
return new ImageIcon(imgUrl);
} else {
throw new IllegalArgumentException("This icon file does not exist");
}
}
public static final String MINESWEEPER_ONE = "one.png";
}
Usage:
IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);
If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).
Hope this helps even though it's a long shot.
I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.
I do have a same problem also. But I found the solution.
I create the package in project and put the images inside there.
When I build the project, Netbeans will create 'target' folder and build .class files.
I found that the images that I copied to the package, did not transfer to the 'target' folder.
Interim solution.
4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans.
5. Incase you clean the project. Do no.4 again.
I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
Old post, but I was struggling with this.
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
Firstly the old JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
Windows is blocking this operation since XP.
The scenario before was like:
Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
The come to front call in java is only working for java windows.
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
Correction the post above..
I have resolve my problem as below:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
You might think about using a JFrame instead. It may give you a little more flexibility.
If you are using a JFrame and you want it to popup on top of the other windows use:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.
I'm working on an Eclipse RCP application. Today I experienced some troubles when displaying images in the context menu. What I wanted to do is to add a column to my table containing images of stars for representing a user rating. On Windows, this causes some problems, since the star images are squeezed up on the left corner of the table cell instead of expanding on the whole cell, but I'll solve that somehow. In addition I have a context menu on the table, with an entry called "rate" where again the different stars from 1 to 5 (representing the rating level) are shown, such that the user can click on it for choosing different ratings. That works fine on Windows.
Now I switched to Linux (Ubuntu) to see how it works out there, and strangely, the stars in the table cell are layed out perfectly, while the stars on the context menu don't even show up.
Rating inside the table cell works http://img187.imageshack.us/img187/4427/starsratingho4.png
star images don't show up http://img514.imageshack.us/img514/8673/contextmenuproblemgt1.png
On the context menu I'm using an action class where I'm setting the image descriptor for the star images:
public class RateAction extends Action {
private final int fRating;
private IStructuredSelection fSelection;
public RateAction(int rating, IStructuredSelection selection) {
super("", AS_CHECK_BOX);
fRating = rating;
fSelection = selection;
setImageDescriptor(createImageDescriptor());
}
/**
* Creates the correct ImageDescriptor depending on the given rating
* #return
*/
private ImageDescriptor createImageDescriptor() {
ImageDescriptor imgDescriptor = null;
switch (fRating) {
case 0:
return OwlUI.NEWS_STARON_0;
case 1:
return OwlUI.NEWS_STARON_1;
case 2:
return OwlUI.NEWS_STARON_2;
case 3:
return OwlUI.NEWS_STARON_3;
case 4:
return OwlUI.NEWS_STARON_4;
case 5:
return OwlUI.NEWS_STARON_5;
default:
break;
}
return imgDescriptor;
}
/*
* #see org.eclipse.jface.action.Action#getText()
*/
#Override
public String getText() {
//return no text, since the images of the stars will be displayed
return "";
}
...
}
Does somebody know why this strange behaviour appears?
Thanks a lot.
(For some strange reason, the images don't appear. Here are the direct URLs:
http://img187.imageshack.us/img187/4427/starsratingho4.png
http://img514.imageshack.us/img514/8673/contextmenuproblemgt1.png)
//Edit:
I did some tries and it seems as if the images just don't appear when using a Checkbox style for the context menu (see constructor of the RateAction). When I switched to a PushButton style, the images appeared, although not correctly scaled, but at least they were shown.
When SWT-images have not shown up for me it has been because:
I've used capital letters in the image filename, but not in the source code. Works on Windows, not Linux.
I've tried to run a x64 version of SWT before it was supported.
I've used VNC. Not sure why it doesn't work, color depth problems?
I've used Ubuntu. The images have shown up fine with Red Hat.
Not sure if this will help you in anyway, but it could perhaps give you a hint where to look.
Maybe this is simply a bug, in which case there would be no real answer to your question.
Look if someone has had a similar problem before in Eclipse Bugzilla
Otherwise, try to make a test case as small as possible that works in Windows but not in Linux (or the other way around) and submit a new bug.
You can enable icons in menus in your Gnome configuration:
Open a terminal
Run gnome-appearance-properties
Select Interface tab
Enable Show icons in menus check box
Now you can see icons in your RCP menus.
See this Eclipse Bug for details: Bug 293720 - [GTK2.18] Menu icons missing