wxPython: widget positioned incorrectly when adding one widget to a frame - widget

When I use one Button in a Frame and position it using coordinates, the widget is not displayed correctly. It takes up the entire Frame. When I add another Button to the Frame, both buttons appear correctly. Why is this?
I have tried this on wxPython running on Mac OS 10.4, Mac OS 10.12, and Windows 10. Displaying one widget is done incorrectly every time.
# This will display the buttons incorrectly
import wx
app = wx.App()
frame = wx.Frame(None, wx.ID_ANY, "Main Window")
button1 = wx.Button(frame, wx.ID_ANY, "Button 1", (50, 50))
frame.Show()
app.MainLoop()
# This will display the buttons correctly
import wx
app = wx.App()
frame = wx.Frame(None, wx.ID_ANY, "Main Window")
button1 = wx.Button(frame, wx.ID_ANY, "Button 1", (50, 50))
button2 = wx.Button(frame, wx.ID_ANY, "Button 2", (160, 50))
frame.Show()
app.MainLoop()
I expect the one Button example to display in the specified place like the two button example does. Is this a bug with wxPython?

Think of the frame as the wrapper for your widgets. Normally a frame will contain one or more Panels.
From the documents:
if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area.
A panel is a window on which controls are placed.
It is usually placed within a frame. Its main feature over its parent class wx.Window is code for handling child windows and TAB traversal.
Incorporate a panel into your code:
import wx
app = wx.App()
frame = wx.Frame(None, wx.ID_ANY, "Window")
panel = wx.Panel(frame,wx.ID_ANY)
button1 = wx.Button(panel, wx.ID_ANY, "Button 1", pos=(50, 50))
frame.Show()
app.MainLoop()
and it will behave itself.

Related

Minimize a resizable JDialog

I'm working on an swing application with a main window (which extends JFrame) from which several child windows can be opened (more than 1 contemporarily).
These windows are all non-modal and resizable.
So far, I implemented these 'child' windows as a JFrame. However, I get a new icon on my Windows taskbar for each opened Window.
I therefore tried to implement these windows as a JDialog with type ModalityType.MODELESS.
Looks OK except that a JDialog has no minimize button.
Is there a way to resolve this?
I.e., I need to create non-modal and resizable child windows that can be minimized.
JInternalFrame is not an option since the main frame is not just a container with a JDesktopPane and child windows should be able to cross the borders of the main window.
For those interested:
Child windows register and unregister themselves on the main window when being opened/closed.
The main window has a menu with a 'Windows' item and child windows are added/removed from that menu upon registration/unregistration.
The user can switch between the various windows by selecting an item within this menu.
I am offering two suggestions.
A. Don't use the close button to get rid of the contents.
B. Set the type of child jframes to be utility.
I think having the JDialog close button destroy data is setting your users up for data loss. I would instead use the close to just hide the window, and then have controls inside of the dialog to cancel/finish/restart.
import java.awt.*;
public class DiFrame{
static JDialog log;
static JFrame ame;
public static void main(String[] args){
JFrame frame = new JFrame("Father of two");
JButton one = new JButton("dialog");
one.addActionListener( evt->{
if(log==null){
log = new JDialog(frame, "dialog child", false);
log.add(new JTextArea("fresh start"));
log.pack();
log.setVisible(true);
} else{
log.setVisible(true);
}
});
JButton two = new JButton("frame");
two.addActionListener( evt->{
if(ame==null){
ame = new JFrame("frame child");
ame.add( new JTextArea("fresh start") );
ame.setType(Window.Type.UTILITY);
ame.pack();
ame.setVisible(true);
} else{
ame.setVisible(true);
}
});
frame.add(one, BorderLayout.NORTH);
frame.add(two, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Click the dialog button and it shows the dialog. Then the text area can be modified. When the dialog is closed it can be re-opened.
Click the frame button and a jframe is shown. ( I actually cannot check if this shows up as a new application because it doesn't on my computer anyways. )

Codenameone WebBrowser component hiding tabs when tab titles are left aligned

When I have a Tabs component with the tabs on the left of the screen and one of the tabs contains a WebBrowser component (inside a container with BorderLayout) the tab tile (and icon) is hidden on the tab after the one that contains the WebBrowser component.
This is seen in the Simulator and Android builds - IOS builds are fine.
Basically the WebBrowser is too wide and covers the tabs.
Code sample:
Form f = new Form();
f.setLayout(new BorderLayout());
Tabs tabs = new Tabs();
tabs.setTabPlacement(Component.LEFT);
f.addComponent(BorderLayout.CENTER, tabs);
tabs.addTab("Tab 1", new Label("Label for tab 1"));
WebBrowser wb = new WebBrowser();
wb.setPage("<h1>Web Browse Tab 2", null);
tabs.addTab("Tab 2", wb);
tabs.addTab("Tab 3", new Label("Label for tab 2"));
f.show();
Tab 2
Tab 3
You can wrap your webbrowser in a BorderLayout container before placing it in a tab.
Also, tab expects containers to be added and not Labels or any other component.
You can simply do it this way:
Form f = new Form(new BorderLayout());
Tabs tab = new Tabs(Component.LEFT);
f.addComponent(BorderLayout.CENTER, tab);
tab.addTab("Tab 1", encloseIn(new BoxLayout(BoxLayout.Y_AXIS), new Label("Label for tab 1")));
WebBrowser wb = new WebBrowser();
wb.setPage("<h1>Web Browse Tab 2", null);
tab.addTab("Tab 2", BorderLayout.center(wb));
tab.addTab("Tab 3", encloseIn(new BoxLayout(BoxLayout.Y_AXIS), new Label("Label for tab 2")));
You should be able to place the BrowserComponent in a Tab directly and the Tabs themselves are in the CENTER of the form so this should "just work".
You seem to have done that fine so I'm not sure why this is happening. Make sure you didn't define a theme constant like tabsOnTopBool.
If this isn't there then it might be an issue with the internal TabsLayout class not leaving enough room for the side tabs.

Loading and unloading content from library in AS3

I'm doing a flash project but I'm new to actionscript.
I have a menu in the main page and I want to make other pages appear when I click on menu items.
I know how to load movieclips from library but I don't know how to specify their position on the screen and how to make them appear in a specific layer.
Is it necessary to unload previous content when I click on another item of menu?
will they stack on each other and make the program heavy if I don't unload them?!
I've added a layer for each menu item and added the following code in each of them (For every item there is a different class):
import flash.display.MovieClip;
btn_msg.addEventListener(MouseEvent.CLICK, ShowMessegePage);
function ShowMessegePage(event:MouseEvent):void
{
var msg_page:MessegePage = new MessegePage();
addChild(msg_page);
msg_page.x = 495;
msg_page.y = 323;
}
I have placed an image in the first frame as the main page. Then I placed some buttons each on separate layers. In order to load a page while clicking on each button, I have added the above code.
When I click on button A, Page A is loaded, when I click on button B, page B is loaded and so on. How can I see the main page again? I have added a button for the main page but for some reason I do not what to load it from library, it is in the first frame and under loaded contents. My question is how can I remove loaded contents so that I can see the main page again? Should I remove every previously loaded content when I click on every button and then load new contents?
Loading MovieClip from library:
var mc:MovieClip = new MyClipID();
parentLayer.addChild(mc); // parentLayer can be any DisplayObjectContainer, even the stage
Positioning a loaded clip:
mc.x = 15;
mc.y = 30;
Removing a loaded clip:
if(mc.parent)
{
mc.parent.removeChild(mc);
}
Placing a clip at a specific "layer" (index):
parentLayer.addChildAt(mc, 0); // this will place it behind everything on parentLayer
UPDATE: To answer your latest question, yes, you should remove the previously loaded clip before adding a new one. Then, when you want to display the main image again, you can just remove the currently loaded clip.
UPDATE2: An example of keeping track which page is currently loaded and removing it before displaying a new page:
import flash.display.MovieClip;
import flash.display.DisplayObject;
var currentPage:DisplayObject;
btn_msg.addEventListener(MouseEvent.CLICK, ShowMessagePage);
btn_msg2.addEventListener(MouseEvent.CLICK, ShowMesssagePage2);
function ShowMessagePage(event:MouseEvent):void
{
removeCurrentPage(); // remove the old page
var msg_page:MessagePage = new MessagePage();
addChild(msg_page);
msg_page.x = 495;
msg_page.y = 323;
currentPage = msg_page; // keep track of the current page
}
function ShowMessagePage2(event:MouseEvent):void
{
removeCurrentPage(); // remove the old page
var msg_page2:MessagePage2 = new MessagePage2();
addChild(msg_page2);
msg_page2.x = 495;
msg_page2.y = 323;
currentPage = msg_page2; // keep track of the current page
}
function removeCurrentPage():void
{
if(currentPage && currenPage.parent)
{
currentPage.parent.removeChild(currentPage);
}
}

Specifying a frame icon on Scala’s Swing Frame

How can you set a new frame icon on Scala’s scala.swing.Frame class? What are the intentions behind Frame.iconImage: Image and Frame.iconify()? I can’t figure out, what they’re doing.
Here’s my last attempt
import scala.swing.Frame
class MyFrame extends Frame {
iconImage = toolkit.getImage("src/main/resources/icon.png")
visible = true
}
I also tried several other methods, but nothing worked.
What you have there should work, but if the getImage can't find the file, it fails silently.
Since you have a relative path, it's probably because you code isn't executing in the directory you intended.
On Ubuntu this should highlight the difference. I get one window with a smiley icon and one with the standard java icon.
new Frame() {
iconImage = toolkit.getImage("/usr/share/icons/gnome/16x16/emotes/stock_smiley-10.png")
size = new Dimension(200, 200)
visible = true
}
new Frame() {
iconImage = toolkit.getImage("xxx")
size = new Dimension(200, 200)
visible = true
}
I'm guessing you are on OS X. Sadly, the icon decoration does not work for the OS X look and feel, neither does it work for Nimbus look and feel which seems to not come with a specific window decoration (uses title bar from OS X).
So you will need a look and feel that does paint its own window title bar:
import scala.swing._
import javax.swing._
UIManager.setLookAndFeel(new plaf.metal.MetalLookAndFeel)
JFrame.setDefaultLookAndFeelDecorated(true)
val f = new Frame {
iconImage = toolkit.getImage(new java.net.URL(
"http://www.scala-lang.org/sites/default/files/favicon.gif"))
size = new Dimension(200, 200)
visible = true
}
The only chance with OS X window title bars is if you want to decorate with the default icon used for a particular file.
Look for Window.documentFile here: http://developer.apple.com/library/mac/#technotes/tn2007/tn2196.html#//apple_ref/doc/uid/DTS10004439

Changing levels of nested movieclips in Flash AS 3.0

I am working on a zooming square navigation project (visually like Windows Metro, but squares take over the screen when clicked on). I'm having trouble getting the squares to the top layer (setChildIndex). Here is the code for when a square is clicked on:
function zoom(event:MouseEvent):void {
// saves location and size of icons
returnto = event.currentTarget;
returntoX = event.currentTarget.x;
returntoY = event.currentTarget.y;
returntoWidth = event.currentTarget.width;
returntoHeight = event.currentTarget.height;
// turn off button behaviours
event.currentTarget.buttonMode = false;
event.currentTarget.alpha = 1;
event.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, rollover);
event.currentTarget.removeEventListener(MouseEvent.CLICK, zoom);
event.currentTarget.removeEventListener(MouseEvent.ROLL_OUT, rollout);
// put clicked box on top
setChildIndex(returnto, numChildren-1);
// fade out icon decor
TweenLite.to(returnto.decor, .25, {alpha:0});
// make square take over screen, call finished when done
TweenLite.to(returnto, 1, {x:stage.x,y:stage.y, width:stage.stageWidth, height:stage.stageHeight, ease:Expo.easeInOut, onComplete:finished});
}
the squares are all inside a main movieclip (named navigation) on the stage (so the first square is navigation.sq_1)
I am not a Flash developer, so I'm really struggling with AS3. Hopefully this is enough info for someone to help. Thanks!
S
Just try to add your returnto clip with addChild() method:
// put clicked box on top
addChild(returnto);
Display List's default behavior when addChild() is to put the child on top of all childs. Other children's indexes are recounted.