How to dynamically show/hide GXT panels? - java

I'm creating ContentPanel and hide it:
final ContentPanel infoPanel = new ContentPanel();
final TabPanel infoTabPanel = new TabPanel();
...
//Adding TabItems with some forms
infoPanel.add(infoTabPanel);
infoPanel.hide();
add(infoPanel);
Then in some Event Listener I try to show this hidden panel:
infoPanel.show();
infoPanel.layout();
But this panel is shown without any data. Only if I click on tabs data appears.
So how to hide/show this panel correctly?
EDITED:
I'm using GXT 2.2.4.
I'm creating ContentPanel with TabPanel which contains FormPanel and hide ContentPanel.
Then in Event Listener I try to show this hidden panel, but it is shown without form. Only if I click on tabs form appears.
Here is code:
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
final ContentPanel infoPanel = new ContentPanel();
infoPanel.setAutoHeight(true);
final TabPanel infoTabPanel = new TabPanel();
infoTabPanel.setAutoHeight(true);
final FormPanel testForm = new FormPanel();
FieldSet fieldSet = new FieldSet();
fieldSet.setHeading("Information");
FormLayout fLayout = new FormLayout();
fieldSet.setLayout(fLayout);
LabelField field1 = new LabelField();
LabelField field2 = new LabelField();
field1.setFieldLabel("Field1:");
field1.setName("field1");
fieldSet.add(field1);
field2.setFieldLabel("Field2:");
field2.setName("field2");
fieldSet.add(field2);
testForm.add(fieldSet);
TabItem formTab = new TabItem("Form Tab");
formTab.add(testForm);
infoTabPanel.add(formTab);
TabItem longText = new TabItem("Long Text");
longText.addStyleName("pad-text");
longText.addText("Long Text" + "<br>" + "Long TextLong Text");
infoTabPanel.add(longText);
infoPanel.add(infoTabPanel);
infoPanel.hide();
Button buttonShow = new Button("show");
buttonShow.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
infoPanel.show();
}
});
Button buttonHide = new Button("hide");
buttonHide.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
infoPanel.hide();
}
});
add(infoPanel);
add(buttonShow);
add(buttonHide);
}

Which GXT version do you have? I can't reproduce your problem on GXT 3.x. I wrote a little example code which does exactly what you're asking for.
public void onModuleLoad() {
final ContentPanel infoPanel = new ContentPanel();
final TabPanel infoTabPanel = new TabPanel();
infoTabPanel.add(new TextButton("moo"), "moo");
infoTabPanel.add(new TextButton("boo"), "boo");
infoPanel.add(infoTabPanel);
infoPanel.hide();
VerticalLayoutContainer vlc = new VerticalLayoutContainer();
vlc.setPixelSize(300, 250);
TextButton buttonShow = new TextButton("show");
buttonShow.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
infoPanel.show();
}
});
TextButton buttonHide = new TextButton("hide");
buttonHide.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
infoPanel.hide();
}
});
vlc.add(infoPanel, new VerticalLayoutData(1, 1));
vlc.add(buttonShow);
vlc.add(buttonHide);
RootPanel.get().add(vlc);
}

OK I see.
OK I see, since clicking on the tab makes it appear, why don't you programmatically select the tab in your listener?
infoTabPanel.setSelection(formTab);
http://dev.sencha.com/deploy/gxt-2.2.5/docs/api/com/extjs/gxt/ui/client/widget/TabPanel.html#setSelection(com.extjs.gxt.ui.client.widget.TabItem)
---------below didn't work------------
so try infoPanel.repaint();

Related

How I set a OnClick event to <body> element in vaadin 7?

I need to "emulate" this:
document.getElementsByTagName("body")[0].onclick = function gameOver() { ...}
in vaadin 7, to display a dialog box when the user clicks anywhere on the web page
My code:
//...
#Override
protected void init(VaadinRequest request) {
Label labelH1 = new Label("<span style=\"color:SteelBlue;\">M</span>atching "
+ "<span style=\"color:Purple;\">G</span>ame!", ContentMode.HTML);
labelH1.setStyleName("h2");
Label labelH4 = new Label("Click on the extra smiling face on the <span>left</span>.",
ContentMode.HTML);
labelH4.setStyleName("h4");
CssLayout layout = new CssLayout();
AbsoluteLayout leftLayout = new AbsoluteLayout();
leftLayout.setId("leftSide");
AbsoluteLayout rightLayout = new AbsoluteLayout();
rightLayout.setId("rightSide");
layout.addComponent(labelH1);
layout.addComponent(labelH4);
layout.addComponent(leftLayout);
layout.addComponent(rightLayout);
setContent(layout);
}
You can use the click listener on the current UI. As following:
UI.getCurrent().addClickListener(new ClickListener()
{
#Override
public void click(com.vaadin.event.MouseEvents.ClickEvent event)
{
// You can show the dialouge box or any other of your desired task here ...!!!
System.out.println("UI is clicked");
}
});

When adding class in DefaultListModel, did it save the value of toString or the entire class?

Newbie here.
When I added an element in the DefaultListModel, I used a class with an overriden toString.
Based on the sample code below, I want to display the selected item's ID when I click the button btnid.
The commands under displayID doesn't seem to work. Help please. Thanks!
class SomeClass {
JFrame f = new JFrame("Sample");
JScrollPane sp = new JScrollPane();
DefaultListModel dlm = new DefaultListModel();
JList lst = new JList(dlm);
public SomeClass() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnadd = new JButton("Add");
JButton btnid = new JButton("View ID");
Container p = f.getContentPane();
sp.getViewport().add(lst,null);
p.add(sp, BorderLayout.WEST);
p.add(btnadd, BorderLayout.EAST);
p.add(btnid, BorderLayout.SOUTH);
btnadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dlm.addElement(new ElementDisplay(dlm.getSize(),"Element " + dlm.getSize()));
}
});
btnid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayID();
}
});
f.pack();
f.setVisible(true);
}
private void displayID() {
ElementDisplay ed;
ed = dlm.getElementAt(lst.getSelectedIndex());
System.out.println(ed.elementID);
}
public static void main(String args[]) {
SomeClass sc = new SomeClass();
}
class ElementDisplay {
public int elementID;
private String elementDescription;
public ElementDisplay(int pid, String pdesc) {
elementID=pid;
elementDescription=pdesc;
}
#Override
public String toString() {
return elementDescription;
}
}
}
Works fine for me. What makes you think it doesn't work? You need to actually have an item selected in the list for the button press to work, you will get ArrayIndexOutOfBoundException
Instead of depending on a button press, just add a listener to the JList. That way only when the item in the JList is selected, does it print. No need for the button and trying to avoid the ArrayIndexOutOfBoundException
lst.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
JList list = (JList)e.getSource();
DefaultListModel model = (DefaultListModel)list.getModel();
ElementDisplay ed = (ElementDisplay) model.getElementAt(lst.getSelectedIndex());
System.out.println(ed.elementID);
}
}
});
See How to Write Event Listeners where you will run into possible listeners you can use for different components. As GUIs are event driven, you should take time to learn most of them.

LibGDX and ScrollPane with multiple widgets

Trying to add multiple items to a scrollpane I quickly found that all "addActor" functions are Unsupported. So, I went with adding a table with all the items I wanted (this code misses a image that I still want to add) to make a scrollable credits screen... but this approach (currently) doesn't allow for overflow, rendering the ScrollPane useless. (My text is only shown up to what the screen's height allows, and isn't scrollable).
What's the way to make a scrollable pane with multiple widgets in LibGDX? (I only care about Android and Win/Lin/Mac platforms at the moment.
pane = new ScrollPane(null, skin);
pane.setFillParent(true);
paneContent = new Table(skin);
paneContent.setFillParent(true);
Label temp = new Label("", skin);
temp.setAlignment(Align.left, Align.center);
temp.setText( Gdx.files.internal("licenses/credits.txt").readString("UTF-8") );
paneContent.addActor(temp);
pane.setWidget(paneContent);
stage.addActor(pane);
If you want to put multiple items into the ScrollPane you simply need to put a table into it and call add() for each widget you want to put into the ScrollPane.
Below is an example of how to make your credits scrollable:
public class ScrollTest implements ApplicationListener {
private Stage stage;
private static final String reallyLongString = "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
+ "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
+ "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";
#Override public void create() {
this.stage = new Stage();
Gdx.input.setInputProcessor(this.stage);
final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
final Label text = new Label(reallyLongString, skin);
text.setAlignment(Align.center);
text.setWrap(true);
final Label text2 = new Label("This is a short string!", skin);
text2.setAlignment(Align.center);
text2.setWrap(true);
final Label text3 = new Label(reallyLongString, skin);
text3.setAlignment(Align.center);
text3.setWrap(true);
final Table scrollTable = new Table();
scrollTable.add(text);
scrollTable.row();
scrollTable.add(text2);
scrollTable.row();
scrollTable.add(text3);
final ScrollPane scroller = new ScrollPane(scrollTable);
final Table table = new Table();
table.setFillParent(true);
table.add(scroller).fill().expand();
this.stage.addActor(table);
}
#Override public void render() {
this.stage.act();
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
this.stage.draw();
}
#Override public void resize(final int width, final int height) {}
#Override public void pause() {}
#Override public void resume() {}
#Override public void dispose() {}
}
Edit: Added code on setting up a table inside of a ScrollPane.

MessageBox in GWT

How do I show a AJAX "Message Box" in GWT? I know I can use the Window.alert() function, but that's too ugly/annoying. Is there a built-in function for?
Thank you!
Yvan
Here is simple implementation of custom alert widget (modify it to what you want):
public static DialogBox alertWidget(final String header, final String content) {
final DialogBox box = new DialogBox();
final VerticalPanel panel = new VerticalPanel();
box.setText(header);
panel.add(new Label(content));
final Button buttonClose = new Button("Close",new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
box.hide();
}
});
// few empty labels to make widget larger
final Label emptyLabel = new Label("");
emptyLabel.setSize("auto","25px");
panel.add(emptyLabel);
panel.add(emptyLabel);
buttonClose.setWidth("90px");
panel.add(buttonClose);
panel.setCellHorizontalAlignment(buttonClose, HasAlignment.ALIGN_RIGHT);
box.add(panel);
return box;
}
And use it like (note center() method at the end, it actually shows the widget):
CustomWidgets.alertWidget("Adding account failed",
"System failed to add this account. Please chceck your settings properly.").center();
You can use DialogBox instead.

JFace ApplicationWindow: createContents isn't working

I'm attempting to create a window that is divided into three parts. A non-resizable header and footer and then a content area that expands to fill the remaining area in the window. To get started, I created the following class:
public class MyWindow extends ApplicationWindow {
Color white;
Font mainFont;
Font headerFont;
public MyWindow() {
super(null);
}
protected Control createContents(Composite parent) {
Display currentDisplay = Display.getCurrent();
white = new Color(currentDisplay, 255, 255, 255);
mainFont = new Font(currentDisplay, "Tahoma", 8, 0);
headerFont = new Font(currentDisplay, "Tahoma", 16, 0);
// Main layout Composites and overall FillLayout
Composite container = new Composite(parent, SWT.NO_RADIO_GROUP);
Composite header = new Composite(container, SWT.NO_RADIO_GROUP);
Composite mainContents = new Composite(container, SWT.NO_RADIO_GROUP);;
Composite footer = new Composite(container, SWT.NO_RADIO_GROUP);;
FillLayout containerLayout = new FillLayout(SWT.VERTICAL);
container.setLayout(containerLayout);
// Header
Label headerLabel = new Label(header, SWT.LEFT);
headerLabel.setText("Header");
headerLabel.setFont(headerFont);
// Main contents
Label contentsLabel = new Label(mainContents, SWT.CENTER);
contentsLabel.setText("Main Content Here");
contentsLabel.setFont(mainFont);
// Footer
Label footerLabel = new Label(footer, SWT.CENTER);
footerLabel.setText("Footer Here");
footerLabel.setFont(mainFont);
return container;
}
public void dispose() {
cleanUp();
}
#Override
protected void finalize() throws Throwable {
cleanUp();
super.finalize();
}
private void cleanUp() {
if (headerFont != null) {
headerFont.dispose();
}
if (mainFont != null) {
mainFont.dispose();
}
if (white != null) {
white.dispose();
}
}
}
And this results in an empty window when I run it like this:
public static void main(String[] args) {
MyWindow myWindow = new MyWindow();
myWindow.setBlockOnOpen(true);
myWindow.open();
Display.getCurrent().dispose();
}
What am I doing wrong that I don't see three labels the way I'm trying to display them? The createContents code is definitely being called, I can step through it in Eclipse in debug mode.
Apparently, I needed to set the size and location of the labels to get them to appear.

Categories

Resources