adding text box on top of gwt java - java

I am currently working on a school project where we are creating a GWT web application which uses a GeoChart widget to display information about the servers we have crawled. Simply put, I would wish to create a text box on top of our GeoChart widget which shows an interactive world map that takes up the whole screen right now to input information. I have searched quite extensively but I have been unable to come up with an answer.
Here is the code as follows:
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
}
As GeoChart is a widget, it is wrapped under(i am not sure if this is the right word) a SimpleLayoutPanel right now which will display it into a full screen. As stated above, I would wish to include text above the geoChart. From my understanding, I would need to create another widget containing my text and add both the GeoChart widget and the text box widget into it. What would be the best way to go about doing this?

I believe DialogBox could solve your problem. People usually program the DialogBox in a way that it only pops up into display when certain event is triggered and disappears after user finishes some operation. In your particular case, you can simply make the DialogBox shows up from the beginning and never disappears. And the best part of it: you don't need to add the DialogBox widget to the geoChart widget. Calling dialogBox.center() or dialogBox.show() will do the magic for you.
Here is the sample code.
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
// NOTE: the first argument 'false' makes sure that this dialog box
// will not disappear when user clicks outside of it
// NOTE: the second argument 'false' makes sure that mouse and keyboard
// events outside of the dialog box will NOT be ignored
DialogBox dialogBox = new DialogBox(false, false);
DialogBox.setText("Demo");
HorizontalPanel panel = new HorizontalPanel();
panel.setSpacing(5);
InlineLabel labelOfTextBox = new InlineLabel("Label");
TextBox textBox = new TextBox();
panel.add(labelOfTextBox);
panel.add(textBox);
dialogBox.setWidget(panel);
// show up in the center
dialogBox.center();
}

Dear all thanks for answering my question. To rectify this problem, I have made use of the custom widget API within GWT(known as Composite). Here's the code as below:
private static class CombinedWidget extends Composite {
public CombinedWidget() {
// place the check above the text box using a vertical panel.
VerticalPanel panel = new VerticalPanel();
DockLayoutPanel dPanel = new DockLayoutPanel(Unit.EM);
panel.setSpacing(13);
panel.add(nameProject);
nameProject.setStyleName("gwt-Group-Label");
panel.add(className);
panel.add(nameKs);
panel.add(nameEsmond);
panel.add(nameBowen);
panel.add(nameAaron);
dPanel.addWest(panel, 13);
dPanel.add(getGeoChart());
// all composites must call initWidget() in their constructors.
initWidget(dPanel);
setWidth("100%");
}
Actually I sort of changed from the original idea. Instead of putting it on the very top, I attached the labels into a VerticalPanel and then created a CombinedWidget(custom widget) which adds both a VerticalPanel and DockLayoutPanel together. I then added the VerticalPanel(containing all the labels) and the GeoChart into the DockLayoutPanel.
This solved my problem of displaying both the labels and the GeoChart on the same page(as originally i added it into a VerticalPanel but it would not work as the app would not read the GeoChart due to the VerticalPanel being overlayed on top of the GeoChart).
If you guys want a picture of my app to visualise, please say so!

Related

turn off the old dialog before open the new one java swing

anyone help me please , i create a notepad by java-swing . But i have a problem . Anytime i using a action-event (replace , find ) and the program show it on the screen , and i using action-event again it show another on the screen but doesn't turn off the old one .
private void replaceController(MainForm mainForm) {
mainForm.getReplace().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ReplaceForm replaceForm = new ReplaceForm(mainForm, false);
replaceForm.setVisible(true);
replaceForm.getBtnReplace().setEnabled(false);
replaceForm.getBtnReplaceAll().setEnabled(false);
ReplaceController replaceController = new ReplaceController();
replaceController.checkEmptyReplace(replaceForm);
replaceController.replace(mainForm, replaceForm);
replaceController.f(mainForm, replaceForm);
replaceController.replaceAll(mainForm, replaceForm);
replaceController.cancelReplace(replaceForm);
}
});
}
Pay attention to default close operation you apply to your top-level JFrame or JDialog.
If you want the frame just to hide and not to be disposed use HIDE_ON_CLOSE. And set it to visible when you want it to shown on the screen again. This means you need the only instance of the frame.
If you want it to be disposed (looks like that, as here you construct new one each time the action called), use DISPOSE_ON_CLOSE. Then each time user closes the frame it will be destroyed.

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

UnfoldingMaps doesn't refresh in JPanel

For a application I'm developing, I make use of the UnfoldingMaps library for displaying a map. The map is placed inside a JPanel. In the same JPanel other Jpanels are showed.
So I change between the map and other panels with the help of buttons.
The problem is that the map doesn't refresh. I can display it once and when I've displayed another Panel, and go back to the map, the map won't display. The only option I got then is to restart the application.
private void showVis(String chart) {
//visual is the JPanel where elements are viewed in
visual.removeAll();
if (chart.equals("bar")) {
bar.setPart("", partial, location);
bar.createVisualisation(ai);
if (bar.checkGraph()) {
System.out.println("Really creating bar");
ChartPanel panB = bar.getPanel();
JPopupMenu popB = panB.getPopupMenu();
panB.setPopupMenu(popB);
visual.add(panB);
visual.validate();
}
} else if (chart.equals("map")) {
map = null;
map = new Maps();
map.setPart("", partial, location);
map.createVisualisation(ai);
if (map.checkGraph()) {
map.init();
visual.add(map);
visual.validate();
}
}
the string chart decides which panel will be showed.
In the same JPanel other Jpanels are showed. So I change between the
map and other panels with the help of buttons.
it should be parentPanel.revalidate(); and then parentPanel.repaint();
better is usage of CardLayout (So I change between the
map and other panels with the help of buttons.) is designated for this purpose, rather than to remove and then add a new JComponent(s) to already visible Swing GUI

Perform action when button clicked once and do a different action when clicked twice, three times etc. (Netbeans, Java)

I am fairly new to Java programming and want to make a basic game that shows an image when clicked once, a different image when clicked twice and etc.
I know how to do all this but I don't know how to keep track of how any clicks and then do an actions based on how many clicks have been done (Hard to explain, my apologies...)
I ... want to make a basic game that shows an image when clicked once, a different image when clicked twice and etc. I know how to do all this but I don't know how to keep track of how any clicks
As per my comment, give the class with the ActionListener an int field, say called buttonCount, and increment it each time the button is pressed -- inside of the button ActionListener's actionPerformed method: buttonCount++
and then do an actions based on how many clicks have been done (Hard to explain, my apologies...)
In the ActionListener's actionPerformed method change the image displayed. How you change it all depends on how you show it, something that you've yet to show us, and so I can't give you any code.
One way to make it easy is to create an ArrayList of ImageIcons to hold your images (as ImageIcons of course), and then call get(buttonCount) on the ArrayList to get the appropriate ImageIcon and display it in a JLabel via its setIcon(...) method. Make sure that the buttonCount is less than the size of the ArrayList so as not to get an ArrayIndexOutOfBoundsException. One way to do this is to mod your buttonCount by the size of the ArrayList. This will allow you to cycle through your collection of images.
Again, you will want to read the Swing tutorials on how to use JButtons and then break down your big problem into small steps, trying to solve each step one at a time.
Again if you need greater detail and more specific help, then you must show what you've tried and explain in detail what problems you may be having with it. It is my sincere believe and philosophy that you learn most by by forcing your brain to do new and unfamiliar things, by mental effort and sweat. So have at it, you've nothing to lose.
You can count the the mouse clicks in this way. By using an if-else or switch case you can display the images.
public class ButtonStart extends Frame {
private int mouseclicked = 0;
TextField objTextField;
public static void main(String args[]) {
ButtonStart BS = new ButtonStart();
}
public ButtonStart() {
Frame objFrame;
Button objButton;
TextField objTextField;
objFrame = new Frame("Clicking Buttons");
objButton = new Button("Click me!");
objTextField = new TextField("0");
objFrame.addMouseListener(new MyMouseListener());
objFrame.add(objButton);
objFrame.add(objTextField);
objFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
int mouseclicked = me.getClickCount();
objTextField.setText("Mouse clicked this many times:"
+ mouseclicked);
}
}
}

performing multiple actions on one button in java swing

I am trying to create a wizard in java swing programatically.
On the wizard pane I have a next button and it has to perform multiple actions according to which panel is displayed on the wizard.
Is it possible to use java command pattern? may I know how?
thanks in advance.
the code i used for the wizard is
this.mainPanel.add(fileSelectionPane,"SELECT FILE");
this.mainPanel.add(sqlConnectionPane,"SQL CONNECTION");
this.mainPanel.add(devicePane,"PARSER");
this.mainPanel.add(detailsPane,"DISPLAY");
thisLayout.show(this.mainPanel,"SELECT FILE");
this.finishButton.setEnabled(false);
this.backButton.setEnabled(false);
if(newValue==1) {
this.thisLayout.show(this.mainPanel, "SQL CONNECTION");
this.nextButton.setEnabled(true);
this.nextButton.setText("Connect..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
if(newValue==2) {
this.thisLayout.show(this.mainPanel, "PARSER");
this.nextButton.setEnabled(true);
this.nextButton.setText("Parse..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
i want the next button to perform specific actions on SELECT FILE and SQL CONNECTION.
is it possible to use command patterns?
Ok, so, You add action listeners to buttons. These action listeners do something when an event occurs.
You want to change the functionality of the button depending on which panel is being displayed? Why not set a instance variable which reflects the state of the Wizard?
For example (roughly),
int state = 0; // home panel
change panel to help page, event listener is fire, set 'state' to 1. You are now tracking which panel is being displayed.
Now, in your original problem, when the button (the one you want multiple functionality with) fires, you can choose the action it will take based on the 'state' var.
have look at CardLayout
those cards put to the JDialog (JDialog has preimplemented by default BorderLayout) to the CENTER area
create a new JPanel and place there JButtons
JPanel with JButtons put to the SOUTH area
search here, on this forum, there are a few excelent examples for wizard or image previue based on CardLayout
try the following code for button:
JButton btn1;
btn1= new javax.swing.JButton();
btn1.setToolTipText("Submit");
btn1.setContentAreaFilled(false);
btn1.setBorderPainted(false);
btn1.setMargin(new java.awt.Insets(2, 2, 2, 2));
btn1.addActionListener(this);
btn1.setIcon(this.getIcons()[21]);
add(btn1); // add to Jpanel
btn1.setBounds(250,10, 12, 12);
public void actionPerformed(java.awt.event.ActionEvent evt) {
Object obj = evt.getSource();
if (obj == btn1) {
// your function on on click of button
return;
}

Categories