Permanently add item to combobox - java

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

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.

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

Add confirm (Yes /No) dialog box inside Model class in Adempiere

I want to create confirm dialog box inside Model class.
I created window which has several text boxes and after entering the values user can save data. At the point of saving data I want to add a confirm dialog box asking "Are you sure to save these data ?"
So inside Model class I tried to put
org.adempiere.webui.window.FDialog.ask(1,null,"Are you sure to save these data ?");
When I add this into my code, it will give errors and I can't build the project.
If anyone knows how to add confirm dialog box in model class? Please help me to do this...
In Adempiere
For Swing Class (i.e.) model class you can use by below
int response = JOptionPane.showConfirmDialog(null, Are you sure to save these data ?
"", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION)
;
else
;
or
Adialog in Client modules cann't used in Base modules
ADialog.ask(WindowNo, null,"Are you sure to save these data ?");
FDialog Should be used only in ZKWebui package, never use zk classes in base/client modules
org.adempiere.webui.window.FDialog.ask(1,null,"Are you sure to save these data ?")
In window/tab before save you can use "Commit Warning" column in Window,Tab & field (Application Dictionary)
Hope it will helps you.
You can use JOptionPane but not ADialog or FDialog.
Usage of ADialog throws build error. As its is defined in client folder, you cant use it in the upper hierarchy.
You can find the build order from here

Text duplication

I am working on simple text editor and on main panel, I have JList with currently opened files (CDocument class) and active document, which contents is shown (also CDocument class). I store opened files (CDocument objects) in vector and on the right side the active document is shown.
Now, at program start, there is no active document and opened document list is empty. After I click File->New, I create new, empty object from class CDocument. If I enter something into active document area (the red region on screenshot) and then I reclick File->New, I get new, empty (with no text - I've doublechecked with ) CDocument object. But, the text from previous active document still shows into newly created (red region - newly empty CDocument). I am busting my brain here because I do not know why?! Here is File->New code chunck:
`
if(e.getSource()==this.menuItemFileNew())
{
CDocument currentDocument=new CDocument();
if(this.panelMain().documentActive()!=null)
{
this.panelMain().remove(this.panelMain().documentActive());
}
this.panelMain().openedDocuments().add(currentDocument);
this.panelMain().setDocumentActive(currentDocument);
this.panelMain().add(panelMain().documentActive().pane(),
BorderLayout.CENTER);
this.panelMain().documentActive().addKeyListener(this);
this.panelMain().documentActive().requestFocus();
this.menuItemFileSave().setEnabled(true);
this.menuItemFileSaveAs().setEnabled(true);
this.menuItemFileClose().setEnabled(true);
this.menuItemFileCloseAll().setEnabled(true);
this.toolBarFileSwitcher().panelActiveDocumentInfo().
panelFileSizeInfo().updatePanel(this.panelMain().documentActive().getText().length(),
false);
this.toolBarFileSwitcher().listOpenedFiles().model().addElement(currentDocument.filename());
this.toolBarFileSwitcher().listOpenedFiles().setSelectedIndex(this.toolBarFileSwitcher().listOpenedFiles().model().size()-1);
this.toolBarFileSwitcher().setVisible(true);
}
`
Why is text shown, I've tried updateUI, repaint, nothing works!
Use Action to encapsulate functionality related to your CDocument data type. This will help ensure that all invocations are consistent. This example manages images, while this example illustrates a menu of files.

Java swing- Can I change the name on the tab when an event is fired?

First, the code:
tab_textArea_file.addTab(docLabel, null, scrollPane_textArea, null);
So the situation is that I have a list of files the user can select from. When a user clicks on a file, the contents of the file is read and loaded into a textArea. "docLabel" (which is in the code above) is the string that's suppose to change to the name of the file selected, but it doesn't. Is it possible for to change the name within docLabel from the code above? I've tested it with a JOptionPane (works), but it's not working within a tab.
Have you tried something like
int index = tab_textArea_file.getSelectedIndex();
tab_textArea_file.setTitleAt(index, "New Title");
from java doc i see
setTitleAt(int index, String title)
you could take index of tab clicked and change is name
You can know the selected tab index by calling
int selectedIndex = tabbedPane.getSelectedIndex();
and then after your file is selected call
tabbedPane.setTitleAt(selectedIndex, "New Name");
I've had issue with the setTitleAt(int index, String title) : If the title doesn't appears, try to replace:
tab_textArea_file.setTitleAt(index, docLabel);
with:
tab_textArea_file.setTitleAt(index, new String(docLabel));

Categories