Unable to add ImageIcon to JLabel - jlabel

I am trying to add an image to a jLabel by using the following code:
ImageIcon logo = new ImageIcon("E://Logo.png");
JLabel imagelabel = new JLabel(logo);
When tried to execute, I am getting the following error:
Cannot cast javax.swing.JLabel to javax.swing.Icon
Can someone please let me know how to resolve this?

It is best not to have direct filepaths and instead put the images in your package folder. You must first create a JFrame and JPanel and add the JPanel to the JFrame, then create and add a label and then add the ImageIcon to the JLabel
with the .setIcon method
JFrame myWindow = new JFrame("Company logo");
myWindow.setVisible(true);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setSize(500, 500);
myWindow.setResizable(true);
JPanel myPanel = new JPanel();
//add panel to JFrame
myWindow.add(myPanel);
//create image icon constructor
ImageIcon logo = new ImageIcon("logo.png");
//create label constructor
JLabel logoLabel = new JLabel("logo");
//add label to panel
myPanel.add(imageLabel)
//add icon to label
logoLabel.setIcon(logo);

Related

Grouping textbox and label together

I have a lookup view on my xamarin.iOS app where the user puts their subdomain in and I want the textbox to be to the left of the label that has the parent domain, the image below shows what I basically want to achieve:
I was easily able to achieve this on android using drawables but my iOS skills are not as sharp so I'm lost on how I do this on iOS.
in iOS ,you can implment it in code.
in xxxViewController
public override void ViewDidLoad()
{
base.ViewDidLoad();
UIView backgroundView = new UIView(new CGRect(10,50,View.Bounds.Width-20,30));
UILabel domainLab = new UILabel()
{
Text = " https://www.example.com ",
Font = UIFont.SystemFontOfSize(12),
TextColor = UIColor.Gray ,
BackgroundColor=UIColor.LightGray,
Frame=new CGRect(0,0,150,30)
};
UITextField textField = new UITextField()
{
Frame=new CGRect(150,0,View.Bounds.Width-170,30),
};
textField.Layer.MasksToBounds = true;
textField.Layer.BorderColor = UIColor.LightGray.CGColor;
textField.Layer.BorderWidth = (System.nfloat)0.5;
backgroundView.AddSubview(domainLab);
backgroundView.AddSubview(textField);
View.AddSubview(backgroundView);
// Perform any additional setup after loading the view, typically from a nib.
}

How to create popover in Xamarin iOS (iPhone)?

I am creating a page where on click of a button a popover should appear as that in screenshot attached.
I have almost tried many methods to do so.but all ended up showing full screen.Is there really not any way to create a popover in Xamarin iOS?
Can any one help me with this?enter image description here
You can construct the custom pop view controller, then present it with the UIModalPresentationStyle Popover:
//The pop view controller you want to show, here I use Storyboard to initialize it
PopoverViewController popViewController = Storyboard.InstantiateViewController("PopoverViewController") as PopoverViewController;
//Set the presentation style to popover
popViewController.ModalPresentationStyle = UIModalPresentationStyle.Popover;
//The popover view's size
popViewController.PreferredContentSize = new CGSize(150, 150);
//The pop view's position
popViewController.PopoverPresentationController.SourceView = MyBtn;
popViewController.PopoverPresentationController.SourceRect = MyBtn.Bounds;
popViewController.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Up;
//We can also customize the background color
//popViewController.PopoverPresentationController.BackgroundColor = UIColor.White;
popViewController.PopoverPresentationController.Delegate = new PopOverViewDelegate();
PresentModalViewController(popViewController, true);
Do not forget to set the delegate below to achieve the same effect on iPhone:
public class PopOverViewDelegate : UIPopoverPresentationControllerDelegate
{
public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
{
return UIModalPresentationStyle.None;
}
}

How to make an OS X menubar in JavaFX

I'm unable to make a JavaFX MenuBar show as a standard OS X menu bar, at the top of the screen.
Here's what I've tried in my subclass of Application:
public void start(Stage primaryStage) throws Exception {
final Menu menu1 = new Menu("File");
final Menu menu2 = new Menu("Options");
final Menu menu3 = new Menu("Help");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(menu1, menu2, menu3);
menuBar.setUseSystemMenuBar(true);
primaryStage.setTitle("Creating Menus with JavaFX 2.0");
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT);
rootGroup.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
I assumed that the use of
menuBar.setUseSystemMenuBar(true);
would do the trick, but actually it makes the menuBar disappear altogether.
I'm using Java 1.8.0-b132 on OS X 10.9
I've had success with this code:
MenuBar menuBar = new MenuBar();
final String os = System.getProperty("os.name");
if (os != null && os.startsWith("Mac"))
menuBar.useSystemMenuBarProperty().set(true);
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
primaryStage.setScene(new Scene(borderPane));
It looks like OS X only displays the Menus if they have MenuItems inside them (which is a bit weird, as you can attach functionality to empty Menus).
I created a little project that gives you access to the auto-generated menu bar on OS X: NSMenuFX
Update: With the new pure JavaFX version, the API has slightly changed
It allows you to replace the default Mac OS menu bar items, so you can to something like this:
// Get the toolkit
MenuToolkit tk = MenuToolkit.toolkit();
// Create default application menu with app name "test"
Menu defaultApplicationMenu = tk.createDefaultApplicationMenu("test");
// Replace the autogenerated application menu
tk.setApplicationMenu(defaultApplicationMenu);
// Since we now have a reference to the menu, we can rename items
defaultApplicationMenu.getItems().get(1).setText("Hide all the otters");
You can of course also add new menu items as you do in your example above.
I just ran into this issue myself - I noticed that the system menubar wouldn't initially appear in OSX until I switched to another application and back.
Wrapping the setUseSystemMenuBar call in a runLater did the trick, so I unscientifically concluded there's more window setup required before OSX can successfully register an application menu.
Platform.runLater(() -> menuBar.setUseSystemMenuBar(true));
Credits to this tutorial that I have followed with success:
https://blog.codecentric.de/en/2015/04/tweaking-the-menu-bar-of-javafx-8-applications-on-os-x/
Below I paste the most important part to get an OS X menu bar compatible with Win classic menu bar:
#Override
public void start(Stage primaryStage) throws Exception {
MenuBar menuBar = new MenuBar();
menuBar.useSystemMenuBarProperty().set(true);
Menu menu = new Menu("java");
MenuItem item = new MenuItem("Test");
menu.getItems().add(item);
menuBar.getMenus().add(menu);
primaryStage.setScene(new Scene(new Pane(menuBar)));
primaryStage.show();
}
Building on dmolony with some corrections:
MenuBar menuBar = new MenuBar ();
if( System.getProperty("os.name","UNKNOWN").equals("Mac OS X")) {
menuBar.setUseSystemMenuBar(true);
}
BorderPane borderPane = new BorderPane ();
borderPane.setTop (menuBar);
primaryStage.setScene (new Scene (borderPane));

Gridpane positioning in Java Fx

I am new to Java Fx using Netbeans 7.3.1.I am experimenting new things with Gridpane and I just want to place a Gridpane in a another position other than the left topmost position of the window..The root.getChildren().add(gridpane); adds the gridpane to the topmost left corner of the window.. How can i place the Gridpane on another place of the window without adding any new child or root!!My root is just a group and the current code just overlaps the gridpane with the Menubar.. My full code is this!!
public class Menu extends Application {
#Override
public void start(Stage primaryStage){
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
MenuBar menuBar = new MenuBar();
javafx.scene.control.Menu m = new javafx.scene.control.Menu("File");
m.getItems().add(new MenuItem("New"));
m.getItems().add(new SeparatorMenuItem());
m.getItems().add(new MenuItem("Exit"));
menuBar.getMenus().add(m);
javafx.scene.control.Menu tools = new javafx.scene.control.Menu("Cameras");
tools.getItems().add(CheckMenuItemBuilder.create()
.text("Show Camera 1")
.selected(false)
.build());
menuBar.getMenus().add(tools);
root.getChildren().add(menuBar);
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(5);
gridpane.setVgap(5);
Label fNameLbl = new Label("First Name");
TextField fNameFld = new TextField();
Label lNameLbl = new Label("First Name");
TextField lNameFld = new TextField();
Button saveButt = new Button("Save");
// First name label
GridPane.setHalignment(fNameLbl, HPos.RIGHT);
gridpane.add(fNameLbl, 0, 0);
// Last name label
GridPane.setHalignment(lNameLbl, HPos.RIGHT);
gridpane.add(lNameLbl, 0, 1);
// First name field
GridPane.setHalignment(fNameFld, HPos.LEFT);
gridpane.add(fNameFld, 1, 0);
// Last name field
GridPane.setHalignment(lNameFld, HPos.LEFT);
gridpane.add(lNameFld, 1, 1);
// Save button
GridPane.setHalignment(saveButt, HPos.RIGHT);
gridpane.add(saveButt, 1, 2);
root.getChildren().add(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
try Scene Builder...it is used for designing javafx pages...
you can design all your pages according you need.
I'm not sure if it works but try using the following:
grid.setAlignment(Pos.CENTER);
Centre can be replaced with any other position as far as I am aware. I hope this helps somewhat.

Unable to update the text of a RootElement when used with a modal RadioGroup Section with MonoTouch Dialog

I am creating an iPad app using MonoTouch 2.10.11 and I want to MonoTouch.Dialog to create some of the editable fields on a form. One of the fields will use a RadioGroup to allow the user to select from a list of options. The default behavior of M.T.D is display the selection list table over the existing table. This works great for the iPhone layout, but on this iPad form, the table is only on a small area of the form and the navigation bar looks odd on the middle of the form. I want to display the selection as a full screen modal and the user will hit a "back" button to go back the previous form with the selected item.
I created a new RootElement descendant class like this:
public class ModalRootElement : RootElement
{
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
tableView.DeselectRow (path, false);
UIViewController uIViewController = this.MakeViewController ();
this.PrepareDialogViewController (uIViewController);
dvc.PresentViewController (uIViewController, true, null);
}
protected override void PrepareDialogViewController(UIViewController dvc)
{
base.PrepareDialogViewController(dvc);
UIButton button = UIButton.FromType (UIButtonType.RoundedRect);
button.Frame = new RectangleF (5, 5, 80, 20);
button.SetTitle ("back", UIControlState.Normal);
button.TouchUpInside += delegate {
DialogViewController d = dvc as DialogViewController;
(d.Root as ModalRootElement).TableView.ReloadData ();
d.DeactivateController(true);
};
dvc.View.AddSubview (button);
}
}
The table is implemented with the following code:
var _status = new ModalRootElement("Status", new RadioGroup("status", -1)) {
(new Section() {
new RadioElement("New", "status"),
new RadioElement("In process", "status"),
new RadioElement("Rejected", "status"),
new RadioElement("Deferred", "status"),
new RadioElement("Transferred", "status"),
new RadioElement("Unknown", "status"),
new RadioElement("Complete", "status")
})
};
var _odom = new EntryElement ("Odometer", "current odom", "");
_odom.KeyboardType = UIKeyboardType.DecimalPad;
_odom.TextAlignment = UITextAlignment.Right;
var root = new RootElement ("back") {
new Section("") {
_status,
_odom
}
};
_dvc = new DialogViewController(root);
_nav = new UINavigationController (_dvc);
_nav.SetNavigationBarHidden (true, false);
When I run the app, I can drill into the RadioGroup and make a selection. When I click the back button that I added to the view, the modal view closes and the RadioSelected properted of the ModalRootElement object is set correctly, but the text is not displayed.
If I change Selected() method to call dvc.ActivateController instead of PresentViewController, the ModalRootElement displays the correct text, but the RadioGroup table has the wrong size. Is there a way to get the RootElement to display the correct text when you use PresentViewController instead of ActivateController?
I think you need a Root.Reload() call.

Resources