Autocomplete does not load data correctly - java

I have a problem with the following code, if filtered data appears to me, but it only filters it when the jframe starts, but not when I add a new one, I have to close the jframe and reopen it so that it just recognizes that data. I put the data in .txt
String barrapro = File.separator;
String ubicacionpro = System.getProperty("user.dir")+barra+"Procesador"+barra;
File contenedorpro = new File(ubicacionpro);
File [] procesadorlistado = contenedorpro.listFiles();
public TextAutoCompleter AutocompletarProcesador;
public Registrar() {
initComponents();
setLocationRelativeTo(this);
AutocompleterReg();
public void AutocompleterReg() {
AutocompletarProcesador = new TextAutoCompleter(TProcesador1);
for (int i = 0; i < procesadorlistado.length; i++) {
AutocompletarProcesador.addItem(procesadorlistado[i].getName().replace(".procesador", ""));
}
}
I saw in some forums that use repaint and but I only want that when one is modified in real time the filtering is updated, it does but it continues showing the data that was already deleted until I close the jframe and reopen it, I also tried to do it with timer but if I do that, it won't let me select as if it were google search, pressing the down arrow key to select the result I want.
video_recording.mp4

The problem you're experiencing is likely due to the fact that the list of files in the "Procesador" directory is only being read once, when the Registrar JFrame is first created. When a new file is added to the directory, the program doesn't know to refresh the list of files.
One way to fix this would be to update the list of files in the directory and repopulate the TextAutoCompleter every time the JFrame is made visible. You can do this by overriding the setVisible() method of the JFrame and updating the list of files and the TextAutoCompleter inside of it.

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.

Buttons Generating other Buttons

So, i'm asking cause i searched and didn't found nothing about this, i don't know if i'm only searching it wrong.
I'm building a POS (Point of Sale) for my final School work but instead off adding the buttons manually i wanted to make an interface for the admn where he could add the buttons to the main project (ex. I want to add the button for Meat, Fish, etc.)
It's much likely to be easy to do it, my other doubt becomes with, if the button is generated how it will be called so i can use it later on?
With the NetBeans form designer you can see what code must be created.
Then instead of jButton1, jButton2 use List<JButton> buttons = new ArrayList<>();
In the initComponents (or after its call) create the buttons dynamically, using some list with button data: caption Meat / Fish / ... and so on. These data could come from a file you generated, so they are persist even if quitting the application.
A file can be read as:
Path path = Paths.get("buttons.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (String line : lines) {
String[] words = line.split(";\\s*");
if (words.length > 2 && words[0].equals("button")) {
JButton button = new JButton(word[1]);
button.addActionListener(this); ...
... add(button);
buttons.add(button);
}
}
I think you shouldn't generate new buttons. The best way is to hide the buttons you've created by calling button.setVisibility(View.Gone). So just create buttons and call setVisibility(View.Gone) in onCreate. And where needed make them visible by calling button.setVisibility(View.visible).

Adding element to jList in Netbeans 8.2

Using "test" rather than my string results to make less specific. Have checked other questions with this problem and have attempted but cannot find answer that works.
Trying to firstly print just "test" into my jList FriendsList which has been sent up through Netbeans. The model is default and I can't see anything wrong with the way I've set it up. The file is reading correctly as the System.out.
Java:
DefaultListModel listOfFriends;
listOfFriends = new DefaultListModel();
friendsList = new JList(listOfFriends);
String[] result = line.split(","); // line is previously init.
for (int i = 0; i < result.length; i++)
listOfFriends.addElement("test"); // THIS DOES NOT WORK
System.out.println(result); // THIS WORKS
}
The JList is added to the frame at the end of the file as Netbeans automatically does and then you cannot change it.
Java end of file (variables declaration which cannot be edited):
public javax.swing.JList<String> friendsList;
You have two ways of doing this:
1) In your code manually call list.setModel() anywhere after initComponents() is called. 2) Do it through NetBeans - Right click the list, go to "Customize Code". The first code section is the list's constructor call. Change the dropdown from "Default Code" to "Custom Creation" and simply insert your ListModel in the constructor call. You can do this by setting it tonew javax.swing.JList(new DefaultListModel())
or by instantiating your listmodel before the call to initComponents() in the code and then doing
javax.swing.JList(defaultModel);
I just copied and pasted the code from a similar question answered by someone

Moxieapps file uploader for GWT adding multiple upload buttons

The GWT web app I'm building has a page where users can upload CSV files. The upload code uses the Moxieapps GWT Uploader, which mostly works great.
However, I've discovered a strange scenario, where navigating away from the page and back to it adds the upload button again. So the third time I visit the page, the upload section will look like this:
And the relevant part of the generated HTML viewed in an inspector shows that both the input and the div containing the "button" get added over and over (though there is only ever one dropzone):
I've gone over my code many times to see whether I was doing something that could be causing this, but haven't found anything. You don't actually manually add the button or the input; this is done automatically by the framework. The fileUploader gets initialised only once (this being GWT client code, I've debugged using the inspector as well as logging statements to the console to confirm this):
fileUploader.setButtonDisabled(true).setFileTypes("*.csv")
.setUploadURL(getBaseUrl() + "/fileUpload.upload")
.setButtonText("<span class=\"buttonText\">Select CSV file to upload</span>")
.setFileSizeLimit(FILE_SIZE_LIMIT)
.setButtonCursor(CustomUploader.Cursor.HAND)
.setButtonAction(CustomUploader.ButtonAction.SELECT_FILE)
.setUploadProgressHandler(new UploadProgressHandler() {...})
.setUploadSuccessHandler(...)
// etc. with other handlers
The method setButtonText() is called from a couple of other places, and the text changes as it should, but only on the last button (if there are several). Otherwise, there's nothing in my code that could possibly be adding the button as far as I can tell.
Has anyone else encountered this issue? Is there some property I need to set to prevent this? Could it be a bug in the moxieapps code?
After writing out my question, and adding "Could it be a bug in the moxieapps code?" at the end, I followed up on that suspicion, and it turns out that it is indeed a bug in the org.moxieapps.gwt.uploader.client.Uploader class.
The input and the "select file" button are added in the onLoad() method of that class without a check whether they may have been added already.
It looks like there hasn't been any active development on this framework for some time, so I thought it was time for a custom override version. I've tested this and it works:
package yourpackagename.client.override;
import java.util.Iterator;
import org.moxieapps.gwt.uploader.client.Uploader;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetCollection;
/**
* The sole reason this class exists is to fix a bug in the moxieapps uploader
* (org.moxieapps.gwt.uploader-1.1.0.jar) where it adds a new upload input and
* button each time its <code>onLoad()</code> method is called, i.e. every time
* you navigate away from the page and then back to it.
*/
public class CustomUploader extends Uploader {
#Override
protected void onLoad() {
boolean hasFileUploadAlready = false;
WidgetCollection children = getChildren();
for (Iterator<Widget> iterator = children.iterator(); iterator.hasNext();) {
Widget eachWidget = iterator.next();
if (eachWidget instanceof FileUpload) {
hasFileUploadAlready = true;
}
}
// Only call the super method if there isn't already a file upload input and button
if (!hasFileUploadAlready) {
super.onLoad();
}
}
}
Instead of referencing the org.moxieapps.gwt.uploader.client.Uploader, I've changed the references to point to my custom uploader class, which will now check for an existing FileUpload child widget, and simply skip the original onLoad() code if it finds such a widget.
Might be a bit of a crowbar approach, but it works (and in my case, changing the maven-managed JAR file is not very practical). Hopefully, this will be useful to anyone else coming across this problem.

How to check if the tab selected is changed in java

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();

Categories