NetBeans: Making a JTextField disapear - java

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");
}
}

Related

ActionListener Loops back on itself

Well, I actually feel stupid for even asking this question but: Is it possible for an ActionListener to loop back on it self? The situation is as follows (kind shorthandish because it is a lot of in itself sound code that does nothing to the problem)
JMenuItem Menu=new JMenuItem();
Menu.addActionListener(e -> {
JDialog Dialog=new JDialog();
JTextField Name= new JTextField("",30);
JFormattedTextField ID =new JFormattedTextField(NumberFormat.getIntegerInstance());
ID.setColumns(8);
JButton Enter=new JButton();
\\GUI building stuff
Enter.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
Integer ImportID=((Number) ID.getValue()).intValue();
String ImportName= Name.getText();
System.out.println("Eingaben\n"+ImportID);
System.out.println(ImportName);
//loads of different stuff
Dialog.dispose();
System.out.println(ImportID);
System.out.println(ImportName);
//ID.setValue(null);
//Name.setText(null);
//ImportID=null;
//ImportID=null;
System.out.println("Fertig!");
}
});
});
Now this menu leads to a dialog intended as an import dialog. What happens now is that the first time any user input data is used this data is propagated correctly. But if the user attempts make a input a second time the first and the second values are handed through - even with the disposing of the Dialog the data of the first go through shouldn't even be accessible, right?
I tried replacing the inner ActionListener with an e.getSource, which didn't work. Setting the variables or the Field to null doesn't help either.
I am aware that placing an ActionListener within an ActionListener is propably bad form.
Edit: I should explain how this works: The MenuItem (which sits inside the menubar of a proper GUI) is selected and calls forth a Dialog where the user enters two pieces of data and selects some files. Selected files are copied into a new directory and the entered data is processed into two text files. After that happened the Dialog is set to invisible and some different and to the data unrelated processing occurs. This processign being finished things are supposed to be wrapped up so that the dialog can be called upon again. If however I close the application this error does not occur.

Vaadin unbuffered grids won't close

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.

Copy what I write in TextArea when I press a button

I want to make it so people can type in a Text Area and when they're done typing they press a "Post!" Button which basically just adds their post as text like it works on facebook. You write something blablabla about your day, press Post!
How would I do this?
I'm doing this in NetBeans if that matters heres how my GUI looks if that helps. (I do code first then design) xd
https://imgur.com/a/xQ18P
Basically what I want is, for example I type "Hello my name is cow!" in the textarea and when I press the "POST" button I want the jLabel1 to turn into the text I wrote in the textarea.
I would suggest using JFrame!
First you need to extend you class to use it and import the API like so:
import javax.swing.JFrame;
public class Example extends JFrame{}
Once you have variables for the button and text field you need to attach listeners like so:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//LOGIC to grab data
}
Last to get the data from the text box you can create a simple method to be able to grab data from the textbox:
public static JTextField getData() {
return data;
}
Here is some documentation on the library here:
https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html

Passing variable value to another tab (JPanel)

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.

JLabel does not update when using setText method

In the project I am currently working on I have several pieces of information that I would like to make visible via Jlabel. There are a few buttons and textfields elsewhere in the GUI that allow for altering said information I would like to update the JLabel but the text never changes, or updates upon startup.
I have attempted to use concurrency to update the labels, as suggested in other questions on this site, but I have had no luck with the labels updating. The concurrency does work with updating textfields and comboBoxes as needed.
The current iteration of my code looks as follows,
The JFrame
//// This is a snippet from the JFrame
public void start()
{
this.setSize(900, 700);
this.setVisible(true);
devicePanel.populateDeviceDefinitions();
updateServiceInfo();
updateCommandInfo();
startUpdateTimer();
}
public void updateServiceInfo()
{
EventService service = JetstreamApp.getService();
generalPanel.updateServiceInfo(service.getBaseUri(),
service.getAccessKey(), String.valueOf(service.getWindowTime()));
}
public void updateCommandInfo()
{
JetstreamServiceClient client = JetstreamApp.getClient();
generalPanel.updateCommandInfo(client.getBaseUri(), client.getAccessKey());
}
The JPanel named generalPanel
//// This is a snippet from the generalPanel
//// All of the variables in the following code are JLabels
public void updateServiceInfo(String baseUrl, String accessKey,
String windowTime)
{
serviceUrl.setText(baseUrl);
serviceAccessKey.setText(accessKey);
serviceWindowTime.setText(windowTime);
}
public void updateCommandInfo(String baseUrl, String accessKey)
{
commandUrl.setText(baseUrl);
commandAccessKey.setText(accessKey);
}
The labels start with an Empty string for their text and upon window start it is intended that they be updated by grabbing the information from the relevant sources. Can I please have some insight as to why the JLabels never update and display their information?
How did you create the JLabel? If the text starts out as "", and you've created it with new JLabel(""), the width of the JLabel may be initialized to 0 and then none of your text would show up when you update it. I believe I've had that sort of problem in the past. As a test, try using new JLabel("aaaaaaaaaa") or some longer string to create the label, then setText(""); then later, when you setText(somethingElse), see if that causes text to show up. If it does, then the width is probably the problem and you can work on it from there. – ajb 19 mins ago
This comment is the actual answer, when creating a JLabel with an empty string as the text the label's dimensions do not get set properly when using WindowBuilderPro. My labels did exist, and were being updated with the code provided in my question but the labels were not visible.
Starting with a label that has text in it, then setting the text to an empty string works properly.
The method paintImmediately() can be used to cause a Swing component to get updated immediately. after setText(), you should call paintImmediately() like below.
jLabel.setText("new text")
jLabel.paintImmediately(jLabel.getVisibleRect());
You should try to call revalidate() or repaint() on the component that contains your JLabels.
Cheers

Categories