B4J Question How to show form in extended desktop

bjfhs

Active Member
Licensed User
Longtime User
I need to show my form in extended desktop,anyone can help me?

I know in java
B4X:
public class test
{
    public static void main(String[] args)
    {
        JFrame jf = new JFrame();
        jf.setSize(400, 400);
        jf.setDefaultCloseOperation(3);
        jf.setVisible(true);
        test.showOnScreen2(1, jf);
    }
   //screen  
    public static void showOnScreen2(int screen, JFrame frame)
    {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gd = ge.getScreenDevices();
        if (screen > -1 && screen < gd.length)
        {
            frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY());
        } else if (gd.length > 0)
        {
            frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY());
        } else
        {
            throw new RuntimeException("No Screens Found");
        }
    }
}
 

Daestrum

Expert
Licensed User
Longtime User
fx.Screens should list out the bounding rectangles for the screens.
You could probably just position a form so that it is in the second or subsequent screen bounding rectangle.

This is what I get if I extend my screen on to my tv
 
Last edited:
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
fx.Screens should list out the bounding rectangles for the screens.
You could probably just position a form so that it is in the second or subsequent screen bounding rectangle.

This is what I get if I extend my screen on to my tv
Thank you for your reply!
Can you give me a demo?I can't find anything about it(fx) from the forums.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Uses javaobject to read the values
B4X:
 Dim sc As List = fx.Screens
 For Each o As JavaObject In sc
  Dim bounds As JavaObject = o.RunMethodjo("getBounds",Null)
  Dim minX As Double = bounds.RunMethod("getMinX",Null)
  Dim minY As Double = bounds.RunMethod("getMinY",Null)
  Dim width As Double = bounds.RunMethod("getWidth",Null)
  Dim height As Double = bounds.RunMethod("getHeight",Null)
  Log($"Screen is ${width} x ${height} pixels"$)
  Log($"minimum X: ${minX}  minimum Y: ${minY}"$)
 Next
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Better full example with comments (uses javaobject library)
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim desktop(fx.Screens.Size) As Double
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
 MainForm.Show ' form will show on original screen (0) but get moved later
 Dim sc As List = fx.Screens
 Dim cnt As Int = 0
 For Each o As JavaObject In sc
  Dim bounds As JavaObject = o.RunMethodjo("getBounds",Null)' required
  Dim minX As Double = bounds.RunMethod("getMinX",Null)'required
  Dim minY As Double = bounds.RunMethod("getMinY",Null)' optional
  Dim width As Double = bounds.RunMethod("getWidth",Null)' optional
  Dim height As Double = bounds.RunMethod("getHeight",Null)'optional
  Log($"Screen is ${width} x ${height} pixels"$)' optional
  Log($"minimum X: ${minX}  minimum Y: ${minY}"$)' optional
  desktop(cnt) = minX ' required
  cnt = cnt + 1  ' required
 Next
 showOn(1,MainForm)' form must be shown before this or has no values for left,top etc
End Sub
Sub showOn(Screen As Int,what As Form)
 Dim maxScreen As Int = desktop.Length-1 ' maximum number of screens
 If Screen > maxScreen Then ' if trying to display on non existant screen show on last one
  Screen = maxScreen
 End If
 what.WindowLeft = what.WindowLeft + desktop(Screen) ' modify form co-ords to go on new screen
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
Thank you ! You are so kindly
I have two screen ,all 1920*1080,but it return
Waiting for debugger to connect...
Program started.
Screen is 1920.0 x 1080.0 pixels
minimum X: 0.0 minimum Y: 0.0
Screen is 2400.0 x 1350.0 pixels
minimum X: 2400.0 minimum Y: 0.0
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
What resolution does windows use for the second screen? (display settings > screen 2)
Although my 2nd screen is 4k windows uses 1280 x 720 on it (unless I change it)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you Log(o) in the for each loop, it will give more details of each display it has found
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…