I am fresher in Netbeans. I have a small assignment related Netbeans. I want to show some names in JTextfield(for example "abcd","ijkl","mnop" etc as next by next) and it must show one by one. I had create some code which not working properly. I am looking some helps related to it. Please mention as full code here that I can understand in future.I do not know how to create it. So i am done it some another ways. But showing error message. My code is as follows.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s[]={"abcd","ijkl","mnop" } ;
for(int d=0; d<s.length;d++)
jTextField1.setText(""+s);
}
Both these variables should be inside of your class but outside of any method.
String s[]={"abcd","ijkl","mnop" } ;
int i=0;
In action performed method
if(i>=s.length)
i=0;
jTextField1.setText(s[i]);
i++;
For the first click, display abcd.
Next click, display ijkl.
Next click, display mnop.
But I didn't check it. Just try it. It will works.
Related
I am having a weird problem on my Vaadin app. I have a screen with two separate unbuffered grids.
The user is able to edit the data in those two grids and then click a "Save" button to save the changes made.
My problem is that I want to close the editors when the user clicks on "Save".
I tried the following code:
private void closeEditors() {
if (tab1.getEditor().isOpen()) {
tab1.getEditor().closeEditor();
}
if (tab2.getEditor().isOpen()) {
tab2.getEditor().closeEditor();
}
}
I don't understand why this code doesn't work, editors stay opened. I also tried calling the cancel method but in vain.
I am using Vaadin 14.
I am posting this here with not much hope of finding an answer, this problem seems really precise.
But with any luck, maybe someone has experienced a similar issue ?
Maybe there is another glitchier way of forcing my editors to close ?
Any suggestion would be of great help, thanks in advance for anything you could think of !
EDIT: a little more code
This is the grids:
private Grid<Map<String, String>> tab1;
private Grid<Map<String, List<String>>> tab2;
This is the save function
public void saveData() {
saveDataFromTab1();
saveDataFromTab2();
try {
ServicesProxyImpl.getInstance().updateInBD(someObject);
saveButton.setEnabled(false);
cancelButton.setEnabled(false);
closeEditors();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_OK);
dialog.open();
} catch (JAXBException e) {
e.printStackTrace();
Dialog dialog = VaadinComponentUtils.generateDialog(Constantes.MSG_SAVE_KO);
dialog.open();
}
}
And this is the save button:
public Button getSaveButton() {
Button saveButton= VaadinComponentUtils.generateButton("Save",
VaadinIcon.CHECK_CIRCLE_O, null, true);
saveButton.setEnabled(false);
saveButton.addClickListener(event -> saveData());
return saveButton;
}
EDIT 2:
I have noticed something, when I click on an element of one of my two grids, I want the editor to open for that specific element and I want to close the editor on the other grid (the one not concerned by the modification). This works ! My grids behave like I want. It seems I am only losing control over my editors after I have actually modified one of the cells and clicked on my save button.
The isOpen function returns false on both grids after I call my closeEditors function, so it seems the grid thinks its editor is closed but it is still opened on my UI.
EDIT 3: I have found a workaround
Well, I have solved my problem by adding a close event listener on both my grids and calling resetGrids when the close event is fired. This function simply removes the grids from the UI, fetches the data to be displayed and then adds the grid one again, both editors being closed. I guess it solves my problem but I would have wanted to understand what was going on...
private void closeEditors() {
tableauHoraires.getEditor().addCloseListener(e -> resetGrids());
tableauRamassagePorteAPorte.getEditor().addCloseListener(e -> resetGrids());
if (tableauRamassagePorteAPorte.getEditor().isOpen()) {
tableauRamassagePorteAPorte.getEditor().closeEditor();
}
if (tableauHoraires.getEditor().isOpen()) {
tableauHoraires.getEditor().closeEditor();
tableauHoraires.getEditor().refresh();
}
}
Make sure that the objects in your grid have proper equals and hashcode methods and that the field(s) being edited do not influence them.
I use the PK from the database.
I'm trying to make an interface for a login/register app and I have, in the email box (jTextField) an example as text (example#gmail.com) but when I run my program when I click that box to write my email on it, I have to delete my set text to write what I want.
What I thought to do was to create 2 jTextFields, the one behind not editable and the one forward where I'd put my text. So there are two things I don't know how to do:
put the forward jTextField invisible so we can see the behind
one
make the text on the behind jTextField disappear when I click the front one
Thanks for trying the help.
Can easily done with FocusGained and focuseLost events
private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {
if (txtEmail.getText().equals("example#example.com")) {
txtEmail.setText(null);
}
}
private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {
if ( txtEmail.getText().equals("")) {
txtEmail.setText("example#example.com");
}
}
I was looking for the solution, but the problem still remains. I'm designing a GUI and trying to change the variable by clicking a button:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
var=(String)jComboBox2.getSelectedItem();
}
Then i want to use it to change label in another tab, which already exist, but it always says null (value of the variable is not updated even though it is defined in the most-upper class.
I was trying with:
jLabel4.revalidate();
jLabel4.repaint();
but it also fails.
Ok, I will explain it better :D I have a button defined as follows:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
var=(String)jComboBox2.getSelectedItem();
jLabel4.revalidate(); //actually makes no difference if i delete this 2 lines...
jLabel4.repaint(); }
And I want to use this variable in another tab (JPanel):
jLabel4.setText("Studenci z przedmiotu " + var);
but the variable var always remains null as defined in the mother class (this is quite logical, because all components are initiated at the start-up, before pressing the button...). But do you know why functions repaint and revalidate do not work? How can I substitute them? sry for not posting the whole code, but i think it will be too much spam ;d
if you want change a Text variable use setText() Method in gui
For Example:
jlable4.setText("new text");
and put it in your action like this:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jlable4.setText("new text");
}
if that was your question,Enjoy!if not give me a peace of code and tell me what is error message and i'be help you.
Before I start, Hi. This is is my first question here. I am not good with Java so have been trying and improve that and here it goes.
I am trying to create an email client and server application using sockets in Java. However I have been running into a problem. I have created a jFrame which is basically the Welcome window. The code is too huge to post so I'll post the relevant portions. There is a preferences jDialog. When the OK button on the dialog, an action handler comes in to play. The code:
private void okActionPerformed(java.awt.event.ActionEvent evt) {
Welcome wel = new Welcome();
wel.setStatusBar("Pressed OK");
dispose();
}
Obviously, the setStatusBar() sets the text of the statusLabel. The code for setStatusBar():
public void setStatusBar(String s)
{
statusLabel.setText(s);
}
Also, the preferences dialog is opened through menu item with this code:
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
settings pref=new settings(null,true);
pref.show();
}
The problem is if I set the status label from any other class, for instance settings class, it does not reflect but if I do so from the Welcome class ( the class where the statusLabel is present), it works fine. This problem is not only limited to this setStatus() but virtually pops up whenever I try to use a method of a different class.
If you guys need more of the code, I could post it. I would be grateful if could help a Java beginner out.
Thanks.
private void okActionPerformed(java.awt.event.ActionEvent evt) {
Welcome wel = new Welcome();
wel.setStatusBar("Pressed OK");
dispose();
}
You're creating a new (hence the keyword new) object of type Welcome. This new object is different from the already existing object of type Welcome, that you have created earlier. It thus has its own label, and you're setting the text of this different label, which is not displayed anywhere in the screen.
Java objects work like regular object. Let's say you would like a cool logo on one of your blue t-shirts. You go to a T-shirt vendor and ask him to print a cool logo. The vendor doesn't have your blue t-shirt. If the vendor gets another red t-shirt from his shop and prints the logo on this red t-shirt, your blue t-shirt will still have no logo at all.
For the vendor to be able to print a logo on your blue t-shirt, you need to give him this blue t-shirt. Same in Java: you need to pass the existing Welcome object to the preferences dialog, and the actionPerformed method must set the label on this Welcome object. Not on a new Welcome object.
As far as i have seen the event:
(1) private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {}
Checks whether a new tab is added or an exiting tab is deleted or not.
On googling , i found this code:
(2) ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
// my code
}
};
jTabbedPane1.addChangeListener(changeListener);
I guess since it uses stateChanged event , it should do what the same a my first code.
By t way even after using both the codes i could not get the required resuts(ie An event that could be invoked when user changes the tab).
Can anyone suggest me a good event [i am using netbeans GUI environment] for effective action. (I dont want any mouseEvents)
Edit:
I want the following code to be excecuted if the tab changes:
String send3=( jTabbedPane1.getSelectedComponent().getComponentAt(0,0)).getName();
The above code dynamically gets the name of jTextarea (in the current tab) which is created dynamically in the jTabbedPanel.
I just checked my own source code where addChangeListener() works fine. The event is fired whenever the tab is changed by the user or programatically. In stateChanged() itself, the now selected tab is determined by
JTabbedPane p = (JTabbedPane)e.getSource();
int idx = p.getSelectedIndex();