I do have main application JFrame window which can include different components. I open a self implemented OnScreenKeyboard when the user select a editable textfield. The OSK is also a JFrame window.
When the user drag the main window to another monitor, the OSK should also be shown on the same monitor. For this i have to detect the monitor the main JFrame is shown.
I try to find a method in
Toolkit.getDefaultToolkit()
but was not able to find someting.
Do you know how i can detect the monitor where a JFrame is shown?
Java-Version 1.4
Windows XP
Thanks
Answer, if the solution of all available monitors are the same.
For AWT:
Every Control does have the method getMonitor() from which the screen position get can calculated from like:
Monitor widgetMonitor = mTextWidget.getMonitor();
Rectangle monitorRect = widgetMonitor.getBounds();
if(monitorRect.x < 0){
// shown in left monitor, starting from the main monitor
}
if(monitorRect.x > monitorRect.width){
// shown in right monitor, starting from the main monitor
}
For SWT:
It is just a snip at my origial code. you should ask if return values are not null ans something like this!
int monitorWidth = 0;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screenDevices = ge.getScreenDevices();
if(screenDevices.length > 0){
monitorWidth = screenDevices[0].getDisplayMode().getWidth();
}
Point ownerLocationOnScreen = owner.getLocationOnScreen();
int screenMovingX = 0;
if(ownerLocationOnScreen.x < 0){
screenMovingX = -monitorWidth;
}
if(ownerLocationOnScreen.x > monitorWidth){
screenMovingX = monitorWidth;
}
Related
I am having issues with my java application and I can't find a suitable reply.
In summary a right click triggered default pop up menu changes my chart's background color behind the pop up.
You can find images below. I am happy to keep the default popup without the "buggy" behaviour or develop my own if required.
A click of a button starts a stream of data and adds a chart to a JInternalFrame component:
If I right click on the image a default pop up comes up:
If I then click away the rectangle area covered by the popup will overlay the chart like this:
TimeseriesMonitorModel model = new DefaultTweetMonitorModel();
jif.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
jif.setContentPane(new TweetSeriesChartPane(model, TweetMonitor.keywords, tkc));
jif.setSize(jif.getWidth(), jif.getHeight());
} catch (InterruptedException ex) {
Logger.getLogger(TweetMonitor.class.getName()).log(Level.SEVERE, null, ex);
}
jif.setVisible(true);
where jif is the Jinternalframe and
public TweetSeriesChartPane(TimeseriesMonitorModel model, String[] seriesNames, TweetKeywordCount tkc) throws InterruptedException {
this.seriesNames = seriesNames;
this.tkc = tkc;
this.model = model;
XYChartTimeseries myRealTimeChart = new XYChartTimeseries();
chart = myRealTimeChart.getChartWithTitle();
List[] tweetData = model.getFrequencyCount(new AtomicIntegerArray(seriesNames.length)); // we are starting from 0
int i = 0;
for (String keyword : seriesNames) {
List<Integer> yData = (List<Integer>) tweetData[1].get(i);
chart.addSeries(keyword, tweetData[0], yData); // adding first value
i++;
}
setLayout(new BorderLayout());
XChartPanel<XYChart> chartPane = new XChartPanel<>(chart);
add(chartPane);
UpdateWorker worker = new UpdateWorker(this, seriesNames, this.tkc);
worker.execute();
}
I've managed to temporary solve the above.
I've checked specifically this line of code
XChartPanel<XYChart> chartPane = new XChartPanel<>(chart);
which extends Chart and Jpanel. The code is from the knowm-chart dependency https://github.com/knowm/XChart/blob/develop/xchart/src/main/java/org/knowm/xchart/XChartPanel.java)
Apparently it adds a listener and the customized PopUpMenu. After reviewing it, it looks like it didn't do any repainting when the mouse was clicked outside the PopUpMenu area.
So I created a new class and tried to customise it. However, the repaint was flickering the screen and I couldn't get it to work only in the PopUpMenu area.
I ended up disabling the .addMouseListener call so now I don't get any popUpMenu. I sad compromise, but oh well.
BTW:
Thanks to both, regardless of the last unneeded comment which didn't add any value.
I did read the link and I though I provided enough information.
In any case, posting to code helped me troubleshoot it
I'm currently programming a plugin for IntelliJ, but have lost the basic of how to center a new JDialog in front of IntelliJ window. That is that I create and open a new JDialog and it should initially center in the IDE, even if I change the monitor. Atm I run "setLocationRelativeTo(null);" and it's just opening on my primary screen all the time and not in the IDE. Anyone got any idea what I'm missing?
Edit
I tried to get the parents position and use the coordinate to place where the JDialog should be. But it always displays on the upper left corner of my primary screen and not where I have my IDE.
Point parentPoint = getParent().getLocation();
Dimension parentDimension = getParent().getSize();
Dimension dialogDimension = getSize();
int x = (int) (parentPoint.getX() + (parentDimension.getWidth() - dialogDimension.getWidth()) / 2);
int y = (int) (parentPoint.getY() + (parentDimension.getHeight() - dialogDimension.getHeight()) / 2);
setLocation(x, y);
Solved it!
The problem was that I never assigned the parent window for the JDialog. Since I created the dialog through AnAction impl, I could get the project from AnActionEvent.
I used that project variable to create this code in my JDialog implementation.
private Window getParentWindow(Project project) {
WindowManagerEx windowManager = (WindowManagerEx) WindowManager.getInstance();
Window window = windowManager.suggestParentWindow(project);
if (window == null) {
Window focusedWindow = windowManager.getMostRecentFocusedWindow();
if (focusedWindow instanceof IdeFrameImpl) {
window = focusedWindow;
}
}
return window;
}
Then just calling this method to
setLocationRelativeTo(getParentWindow(project));
I have developed an application in java swing where two instances have to be run, and I have specified in the code to run a particular instance on a specific screen via configurations. The issue I'm facing is that since it is set as full screen I cannot keep two windows active.
As per requirement I need to keep two windows active in two screens.
The function I used to achieve this is given below.
public static void showOnScreen( int screen, JFrame frame )
{
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
DisplayMode newDisplayMode;
if( screen > -1 && screen < gs.length )
{
gs[screen].setFullScreenWindow( frame );
}
else if( gs.length > 0 )
{
gs[0].setFullScreenWindow( frame );
}
else
{
throw new RuntimeException( "No Screens Found" );
}
}
Please can someone assist me to solve this issue.
I need to fix the screen location of the Open Source Java Application "Angry IP Scanner" on OSX (https://sourceforge.net/projects/ipscan/) at startup.
If you start the application and move it to the extended space of a secondary monitor (Non mirrored) and do one of two things: 1) Unplug your secondary monitor with the app running or 2) quit the app while it is located on the secondary monitor.
If you unplug the second monitor and then start the app, the app loads and places the screen on the now nonexistent 2nd monitor, even though it is not plugged in.
To get to the screen back to the main screen area you MUST plug in a 2nd monitor and move it back to the main screen. Then you can unplug the second monitor and have no problems.
When the app is starting, I think the app needs to check the current screen size verses the previous screen size and if it has changed, place the screen near the 10,10 location so it will show up on the main, hopefully active screen.
In my searching on Stack Overflow, it appears you can find the current screen information like this:
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
How could I save this to a file and recall/compare the previous to the current data, if it has changed, move the application's window to the 10,10 location?
I am a beginner writing iOS and Ruby on OSX, but have not done any Java. I love the program, but this bug is killing me and there doesn't appear to be anyone actively working on the code.
Anyone want to help?
BTW ... It is bug 84 at the Angry IP Scanner website.
Maybe someone could explain how I could run the following code snippet from the cli?
int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
Thanks,
padapa
Something like:
public class ShowMeTheScreenSize {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
System.out.println("VirtualBounds = " + getVirtualBounds());
for (int index = 0; index < getScreenDeviceCount(); index++) {
System.out.println("[" + index + "] Device bounds = " + getScreenDeviceBounds(index));
}
System.exit(0);
}
});
}
public static int getScreenDeviceCount() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
return ge.getScreenDevices().length;
}
public static Rectangle getVirtualBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
for (GraphicsDevice gd : lstGDs) {
bounds.add(gd.getDefaultConfiguration().getBounds());
}
return bounds;
}
public static Rectangle getScreenDeviceBounds(int index) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
return lstGDs[index].getDefaultConfiguration().getBounds();
}
}
Which outputs...
VirtualBounds = java.awt.Rectangle[x=0,y=0,width=3840,height=1200]
[0] Device bounds = java.awt.Rectangle[x=0,y=0,width=1920,height=1200]
[1] Device bounds = java.awt.Rectangle[x=1920,y=0,width=1920,height=1200]
On my machine
As discussed here, Mac OS X helpfully prevents a visible window from being moved offscreen. One expedient would be a command to move the window to a forbidden location, which will force it into reach.
polo.setLocation(Short.MIN_VALUE, Short.MIN_VALUE);
I'm doing some Swing GUI work with Java, and I think my question is fairly straightforward; How does one set the position of the mouse?
As others have said, this can be achieved using Robot.mouseMove(x,y). However this solution has a downfall when working in a multi-monitor situation, as the robot works with the coordinate system of the primary screen, unless you specify otherwise.
Here is a solution that allows you to pass any point based global screen coordinates:
public void moveMouse(Point p) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
// Search the devices for the one that draws the specified point.
for (GraphicsDevice device: gs) {
GraphicsConfiguration[] configurations =
device.getConfigurations();
for (GraphicsConfiguration config: configurations) {
Rectangle bounds = config.getBounds();
if(bounds.contains(p)) {
// Set point to screen coordinates.
Point b = bounds.getLocation();
Point s = new Point(p.x - b.x, p.y - b.y);
try {
Robot r = new Robot(device);
r.mouseMove(s.x, s.y);
} catch (AWTException e) {
e.printStackTrace();
}
return;
}
}
}
// Couldn't move to the point, it may be off screen.
return;
}
You need to use Robot
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events...
Robot.mouseMove(x,y)
Check out the Robot class.
The code itself is the following:
char escCode = 0x1B;
System.out.print(String.format("%c[%d;%df",escCode,row,column));
This code is incomplete by itself, so I recommend placing it in a method and calling it something like 'positionCursor(int row, int column)'.
Here is the code in full (method and code):
void positionCursor(int row, int column) {
char escCode = 0x1B;
System.out.print(String.format("%c[%d;%df",escCode,row,column));
}