Adding Icon on a button on NetBeans - user-interface

I have a problem whereby I can seem to be unable to see my icon image on the button on NetBeans. It works very well on Eclipse by showing button text with the icon, but when I move the code to NB it does not display the icon just the text. Could someone help me with this problem.
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements ActionListener{ //implements allows the button to perform action
JButton button = new JButton(); //making a button global. Declare outside the contructor
MyFrame(){
ImageIcon icon = new ImageIcon("icon.png");//adding an Icon
button = new JButton();
button.setBounds(200, 100, 200, 100);//poistion and size
button.addActionListener(this); //To make the button work, so it display what you need to display
button.setText("CLICK ME"); //Set text to the button
button.setFocusable(false); //Removes the border aroound the button text
button.setIcon(icon); //Adding icon to a button
button.setHorizontalTextPosition(JButton.CENTER); // setting alignment for the button
button.setVerticalTextPosition(JButton.BOTTOM);
button.setFont(new Font("Comic Sans", Font.BOLD,25)); //setting font for the button
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(500,500);
this.setVisible(true);
this.add(button);
}
//METHOD FOR ACTION LISTENER
#Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){ //to check event occours with our button
System.out.println("YESSIR"); //displaying what I want when I click the button
}
}

The icon file cannot be found. Assuming your image is stored under src/main/resources/icons/icon.png you would load it with:
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/icon.png");

Related

MudBlazor: how to display notification in Bottom-Right of window?

I have created an app using MudBlazor template.
All is ok, I use MudBlazor Snackbar and in MainLayout I use
and button on it.
On click I call a function
ShowNotification("BottomRight")
private void ShowNotification(string position)
{
Snackbar.Clear();
Snackbar.Configuration.PositionClass = position;
Snackbar.Add(message, Severity.Normal);
}
I'd like to show notifications in the Bottom-Right of the Browser window but it displayed in left top corner because it is MudAppBar.
How to display notification in the Bottm-right of browser window?
You should not be using "BottomRight" but Defaults.Classes.Position.BottomRight instead:
#inject ISnackbar Snackbar
<MudButton
OnClick="#this.ShowMessage"
Color="Color.Success">
Show message
</MudButton>
#code {
void ShowMessage()
{
this.Snackbar.Clear();
this.Snackbar.Configuration.PositionClass = Defaults.Classes.Position.BottomRight;
this.Snackbar.Add("Wazzup?", Severity.Normal);
}
}
Demo: https://try.mudblazor.com/snippet/wumnYmbNHjwLplPt

Print button on webview is not working

I have a webpage that has a "Print" button and works normally when using Any Browser on mobile phone.
But when using the webview, is not working. Its like not firing.
For java
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
//perform click pdf creation operation on click of print button click
// Use this on main activity class tag
public void printPDF(View view) { createWebPrintJob(myWebView); }
For Xml Create Button
android:onClick="printPDF"

JavaFX Real Fullscreen on macOS

I want a javafx.stage.Stage to be in fullscreen mode on macOS with no (native) menu bar visible. Also I want that the dock is not available while the application is in fullscreen mode.
I have a Stage and I set stage.setFullScreen(true). If the style of my stage is set to stage.initStyle(StageStyle.UNDECORATED), the application displays the native menu bar and the dock. Therefore the whole screen isn't available for the application.
If I set the style to stage.initStyle(StageStyle.UTILITY) the menu bar is not visible, but instead there is a grey area.
And if I move the cursor where usually the menu bar is placed, the menu bar will show itself additionally with a small closing button and a small bar.
Also If I cursor the mouse down where the dock is placed, the dock will show up itself.
How can I achieve a real fullscreen application with no menubar and no dock displayed and no grey bar at the top so my application can use all of the screen size?
Code
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Fullscreen extends Application {
#Override public void start(Stage stage) throws Exception {
AnchorPane pane = new AnchorPane();
Scene scene = new Scene(pane);
pane.setStyle("-fx-background-color: blue");
stage.initStyle(StageStyle.UTILITY);
stage.setFullScreen(true);
stage.setScene(scene);
stage.show();
//stage.setHeight(stage.getHeight() + 16);
//stage.setY(-16);
}
}
Annotation
If I set stage.setHeight(stage.getHeight() + 16) and stage.setY(-16) after stage.show() and the style is set to stage.initStyle(StageStyle.UTILITY), the grey bar isn't visible anymore. But this seems not to be a good solution for me because it depends on the screen size (in my case 2560x1440) and I don't know how to exactly get the size of the grey bar. Also the menubar and the dock are available.
Using Stage#setFullScreen(boolean) is the correct way. When I run this MWE, I get the expected OS X full screen (no dock, automatically hidden menu bar):
public class FullScreenDemo extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) {
primaryStage.setFullScreen(true);
primaryStage.setScene(new Scene(new Label("Foo")));
primaryStage.show();
}
}
I don't think you can get rid of the native menu bar that pops up when you hover over its position—that's just how the OS behaves. The corresponding Javadoc also states:
The implementation of full-screen mode is platform and profile-dependent.

Add Context Menu to a dialogue in e4 rcp

I want to add a context menu to a dialogue. I want it in such a way that when clicked anywhere where it is empty a deafaul context menu appears. I have seen example of context menu added to table and tree but not a dialogue as a whole any snippets or examples will be greatly appreciated.
This is what I have tried.
import org.eclipse.jface.action.MenuManager;
#Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(4, false);
layout.marginRight = 5;
layout.marginLeft = 10;
container.setLayout(layout);
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.add(new Action("New Thing") {
/* (non-Javadoc)
* #see org.eclipse.jface.action.Action#run()
*/
#Override
public void run() {
System.out.println("came in options");
}
});
parent.setMenu(menuMgr.createContextMenu(parent));
productListTreeCheckBox(parent);
return super.createDialogArea(parent);
}
Try creating the menu in a similar way to the table/tree menu, but using to top level Composite for the dialog. Using a menu manager that might be something like:
Control topLevelComposite = ... get top level composite
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(... you menu listener....);
final Menu menu = menuMgr.createContextMenu(topLevelComposite);
topLevelComposite.setMenu(menu);
You will then have to call setMenu on every Composite and control in the dialog which you want to use this menu. You can just use:
control.setMenu(parent.getMenu());
for this (as long as you do it on everything starting from the children of topLevelComposite).

GWT widget being truncated on Panel

I have the following class which adds an image to a button and then adds the button to a panel
private PushButton button;
private AbsolutePanel panel = new AbsolutePanel();
private Image image = new Image("images/rectangle_blue.png");
public ExpandingRectangularButton(String text)
{
String width = "120px";
String height = "160px";
image.setWidth(width);
image.setHeight(height);
button = new PushButton(image);
panel.add(button, 0, 0);
panel.setWidth(width);
panel.setHeight(height);
initWidget( panel );
}
When I display an instance of ExpandingRectangularButton the button is truncated on the bottom. However if I take the panel out of the equation as follows
private PushButton button;
private Image image = new Image("images/rectangle_blue.png");
public ExpandingRectangularButton(String text)
{
String width = "120px";
String height = "160px";
image.setWidth(width);
image.setHeight(height);
button = new PushButton(image);
initWidget( button );
}
and just use the image and the button ( ie I call init() on the button ) then the image will display with no truncation. As I don't set the height of the button I assume it grows dynamically to accomodate it's child widget. Why does this not happen when I put the button onto the panel?
Quoting the Javadoc:
An absolute panel positions all of its children absolutely, allowing them to overlap.
Note that this panel will not automatically resize itself to allow enough room for its absolutely-positioned children. It must be explicitly sized in order to make room for them.
You can:
explicitly set the AbsolutePanel size;
remove the overflow: hidden property from it (workaround);
use panel.add(button, -1, -1) to position the button statically;
change the AbsolutePanel with one that naturally adapt its size (like almost any other Panel).
I'd prefer the last, but your use case is not clear.

Resources