How do I display Button in Nifty GUI without XML? - java

I'm trying to make a GUI for my game. I've tried various libraries and I've ended up with Nifty. I haven't found any useful tutorial and therefore I'm learning from code examples.
I want to display a simple Button, but it seems that my code is not working. I've tried setting background color of Panel which has been working. I have no idea why the Button is not displaying.
Here's what I have:
protected void prepareNifty(Nifty nifty) {
ScreenBuilder sb = new ScreenBuilder("start");
LayerBuilder lb = new LayerBuilder();
PanelBuilder pb = new PanelBuilder();
pb.control(new ButtonBuilder("btn1", "First Button!"){{
alignCenter();
valignCenter();
height("5%");
width("15%");
backgroundColor(Color.WHITE);
}});
pb.childLayoutCenter();
lb.childLayoutVertical();
lb.panel(pb);
sb.layer(lb);
nifty.addScreen("start", sb.build(nifty));
}
I should add I'm using Slick2D and my class extends NiftyBasicGame.
How can I display the Button and set it an absolute position?

You'll need to load the controls and the styles too when you want to use the default controls, like the Button control.
Try something like:
nifty.loadStyleFile("nifty-default-styles.xml");
nifty.loadControlFile("nifty-default-controls.xml");
new ScreenBuilder("start") {{
layer(new LayerBuilder("background") {{
backgroundColor("#f008");
childLayoutAbsolute();
control(new ButtonBuilder("showPopupButton", "SHOW") {{
x(SizeValue.px(100));
y(SizeValue.px(100));
interactOnClick("showPopup()");
}});
}});
}}.build(nifty);
nifty.gotoScreen("start");
Besides that, it might help to read the nifty-gui-the-manual-1.3.2.pdf

Related

jMenuItem doesn't appear

I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:
in MainPanel (the autogenerated code from swing is excluded):
public void init() {
initComponents();
initActions();
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private void initActions() {
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());
}
In PanelControl:
public class PanelControl {
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
public class ActionExit extends AbstractAction{
public ActionExit(){
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
In Application:
private void init() {
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();
}
i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
Your actionExit variable is null.
Nowhere in your code do you create an instance of your ActionExit class.
Somewhere you need:
actionExit = new ActionExit();
Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.
I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.
Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.
A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.
Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.
In your JFrame object you need to do this:
_menuBar = new JMenuBar();
// add controls to the frame
setJMenuBar(_menuBar);
Then you need to add your 'exitMenuItem' to your _MenuBar control.
Cheers

Javafx: Using Threads to load a GIF as background

In school I am working on a project using JavaFx. I would like to make it special and use a GIF as background.
It works and displays my GIF, but it makes my program and GIF run realy slow.
Is there a way to fix this using Threads?
(I am new to using threads and don't realy know how to use them here).
this is my code for the background:
public class WelkomScherm extends StackPane
{
private final screenControl sCtrl;
public WelkomScherm(screenControl sCtrl)
{
this.sCtrl = sCtrl;
//Image backgroundImage = new Image("/img/startBG.gif");
Image backgroundImage = new Image("/img/Welkom.png");
ImageView background = new ImageView(backgroundImage);
background.setFitHeight(height);
background.setFitWidth(width);
...some buttons and labels...
this.getChildren().addAll(background,...,...);
}
I am also new to working on bigger projects, all tips are welcome!

Creating a GWT iFrame like screen

I have been developing an app with Java + GAE (1.8.6) + GWT (2.5.1) and now, as per a requirement of my client, i need to create a screen layout divided in two parts (25%,75%), being in the left side the options, and in the other 75% of the screen, the result of the option clicked (like a screen set as an iFrame).
I have tried to use a DockLayoutPanel with west and center panel added to them, but i cant manage to load the contents of each link placed in the west panel on the center panel.
I have been Google'ing for a solution or some code to adapt it to my needs, but so far I havent had much success.
¿Could you please help me out?. I bought also GWT in Action by Manning ed. but havent found much about this matter there.
Meanwhile i will keep looking around / trying to find a solution on my own.
Thank you in advance,
Kind regards,
The solution i found was:
//define framedPanel for later use
private FramedPanel fupanel;
//Instantiate the widget i want to add when the option is clicked
final FileUp fileUp = new FileUp();
//Create the HtmlLayoutContainer in which add the widget later on
HtmlLayoutContainer conEnlaces = new HtmlLayoutContainer(getTablaEnlaces());
public Widget asWidget() {
if (fupanel == null) {
fupanel = new FramedPanel();
fupanel.setHeadingText("Administración");
(some other code here)
VerticalLayoutContainer p = new VerticalLayoutContainer();
conEnlaces.setWidth(Window.getClientWidth());
fupanel.add(p);
p.add(conEnlaces);
Hyperlink fileUph = new Hyperlink();
fileUph.setText("- Importación de CSV");
conEnlaces.add(fileUph, new HtmlData(".fileUph"));
filexh.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
conEnlaces.add(filex, new HtmlData(".resultado"));
}
});
fupanel.add(conEnlaces);
}
return fupanel;
}
private native String getTablaEnlaces() /*-{
return [ '<table border="0" cellspacing="0" cellpadding="0">',
'<tr><td>',
'<table border="0" width="75%">',
'<tr><td class=fileUph></td></tr>',
'</table></td>',
'<td><table border="0" style="margin-left: 25px;">',
'<tr><td class=resultado></td></tr>',
'</table>',
'</td></tr></table>'
].join("");
}-*/;
public void onModuleLoad() {
RootPanel.get().add(asWidget());
}
And like this my problem has been solved. I have shown you my code because i get angry at times when using stackoverflow forums, and people say to have reached a solution, but dont explain how.
Thank you for your response and time,

GWT widget not showing when I use a SplitLayoutPanel

I'm new to GWT, and am struggling through my first Web page with it.
I've created 2 Composite Widgets - ListWidget and MaintenanceWidget. When I add them both to a FlowPanel, they both show up as they should. However, when I try to use a SplitLayoutPanel, depending on how I do it, either none of them show or only one of them shows.
Below is my code:
public MainPanel(){
list = new ListWidget();
maintenance = new MaintenanceWidget();
panel = new SplitLayoutPanel();
panel.addWest(list, 200);
panel.addNorth(maintenance, 250);
initWidget(panel);
}
In my entry point onModuleLoad() method, I create an instance of MainPanel and add it to the root pane.
With this code, I get a blank space in the west where the list should be, and the maintenance widget on the top with a horizontal splitter beneath it.
I've tried different configurations of the panel.add****() method, but nothing has gotten me the results I'm looking for.
Any ideas? Thanks!
Make sure you have a doctype declaration in your HTML template (for example, <!doctype html>), since SplitLayoutPanel requires browser to work in standards mode.
I found some sample code here that used a method that I hadn't seen before.
My code now reads as follows:
public MainPanel(){
list = new ListWidget();
maintenance = new MaintenanceWidget();
panel = new SplitLayoutPanel();
panel.setPixelSize(500, 400);
panel.addWest(list, 200);
panel.add(maintenance);
initWidget(panel);
}
And now it works. Thanks for your help!
If not mistaken, SpliLayoutPanel must be attached to the body of the document.
Try something like:
public void onModuleLoad() {
SplitLayoutPanel panel = new SplitLayoutPanel();
panel.addWest(new Label("WEST"), 50);
panel.addNorth(new Label("NORTH"), 50);
panel.addEast(new Label("EAST"), 50);
panel.addSouth(new Label("SOUTH"), 50);
panel.add(new Label("CONTENT HERECONTENT HERECONTENT HERECONTENT HERECONTENT HERECONTENT HERE"));
RootLayoutPanel.get().add(panel); //This gets the body element and attaches itself to it, then adds panel.
}
Shouldn't be too hard to apply it to your code.

scrollPane remains gray randomly when it should display editorPane (html)

For some reason my HTML page is not appearing 100% on screen when it should, it looks like a timing issue to me. If I remove scrollpane and use just EditorPane it works ok.
What kind of code should I add below to force java applet screen to redraw/refresh and can I somehow wait until all images were really loaded ok? Currently images are drawn a bit after text is visible on GUI.
(the gray goes away and missing text appears when I minimize+maximize window.)
I use SynchronousHTMLEditorKit as m_editorPane.setEditorKitForContentType
private JEditorPane m_editorPane = new JTextPane();
private JScrollPane m_scrollPane = new JScrollPane();
....
JEditorPane.registerEditorKitForContentType( "text/html", "SynchronousHTMLEditorKit" );
m_editorPane.setEditorKitForContentType( "text/html", new SynchronousHTMLEditorKit() );
m_editorPane.setPage(ResourceLoader.getURLforDataFile(file));
m_scrollPane.getViewport().add(m_editorPane);
m_scrollPane.validate();
m_scrollPane.repaint(); <-- does not seem to solve this
add(m_scrollPane);
/// add( m_editorPane) <-- this WORKS !!
SynchronousHTMLEditorKit is defined as:
public class SynchronousHTMLEditorKit extends HTMLEditorKit {
public Document createDefaultDocument(){
HTMLDocument doc = (HTMLDocument)(super.createDefaultDocument());
doc.setAsynchronousLoadPriority(-1); //do synchronous load
return doc;
}
Try moving the validate and repaint calls to the bottom, after the add, and call them on the container, not the scrollpane
add(m_scrollPane);
validate();
repaint();
What happens if you don't use a SynchronousHTMLEditorKit? Your code works perfectly for me without it.

Categories