Adding element to jList in Netbeans 8.2 - java

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

Related

Autocomplete does not load data correctly

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.

How to correct a display problem when inserting a row in a JavaFX grid?

I want to insert a new JavaFX bean in a grid using an "insert" button. Everything is fine, except for a display problem. After insertion, a "ghost selection" is displayed lower in the grid, as shown in this screenshot. In this example, a fourth section bean was added and selected as requested. But a fake selection appears 10 lines under the last real bean, where no bean is set for this row.
Has anyone experienced this kind of behavior? Any clue how get rid of this ghost selection? Here is what the code for the insert button looks like:
#FXML
private Button insert;
...
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
tableView.getSelectionModel().focus(index);
tableView.getSelectionModel().select(index);
};
According to javaFX-8 documentation, SelectionModel.java does not expose any focus() method. FocusModel.java does instead. Therefore JVM will fail to compile your presented code.
Below is a possible solution:
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
// below line is the amendment
tableView.getFocusModel().focus(index);
tableView.getSelectionModel().select(index);
};
Finally, adding tableView.refresh() corrected this weird behavior. No more ghost selection.

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).

How to show names in Jtextfield click on JButton using Netbeans

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.

Permanently add item to combobox

I have a comboBox which has a few items in it already.
At Run time, I add an item to it, which gets added successfully.
But when, at the end of the execution, the JFrame is loaded again, the recently added item isn't present.
I used both the methods, using model.addElement() and using additem() and both have the same problem.
Here's the code:
int i = JOptionPane.INFORMATION_MESSAGE;
String a = JOptionPane.showInputDialog(null, "Enter new item", "Add", i);
DefaultComboBoxModel model = (DefaultComboBoxModel)
cmbo0.getModel();
model.addElement(a);
cmbo0.setSelectedItem(a);
At the moment, your combobox only lives as long as your application.
You'll need a permanent datasource that you can write to, i.e. file, database, etc.
Take a look at these links:
Connecting to a database
Reading and Writing from a File

Categories