In order to develop a text editor in Java, the user is able to open several files with a object JTabbedPane. Then, I stores such files on HashMap<String,TabManager>. (The key of the HashMap), should to be the name of the file. After, I have on memory the files opened within of HashMap. Now, I need manage my tabs. For example, if the user is on tab selected by it, is evident that the user would like to change the font of the text, save file selected, copy it, and so on. For manage the tab selected by the user, I need of a class to get just the objects from tab selected, just. Such as, JTextPane and File. Basically, I should do:
for(Map.Entry<String, TabManager> entry: HashMap.entry)
{
String key = entry.getKey();
tabManager = entry.getValue();
if(tab.getTabSelected().equals(key))
{
// resquest objects from tab selected by the user
this.container = tabManager.getJTextPane();
this.file = tabManager.getFile();
}
}
I have on my hands the objects from tab selected by the user. Now, I going to handle it. The issue is:
How I handle that big amount of datas ?
All time, I have that do the loop to know which tab the user is selected ?
How I handle this data ?
I can't see what is the name of your map... But assuming you have something like:
Map<String, TabManager> map = new HashMap<>();
where the string is the name of your tab, is unique per tab, and that you can get that name with tab.getTabSelected(), then you can do:
TabManager selectedTab = map.get(tab.getTabSelected());
to get the selected tab.
About your second question: how to handle that big data... Since you don't want to read and copy in memory the entire file every time, you want:
start and end file pointers to know which chunk of the file you are currently displaying
cache for all tabs the page each of them displays
open the file when the tab is selected and the cache is out-of-date or not covering the info you need, close it when the tab is not selected anymore (and/or after some time).
Other optimizations are possible...
Related
I am having trouble overcoming this and need your assistance
picture
Every time i click a stop button there is a random number generated in the field above. What I want to do is save each number in the field on the left. Names of the textfields are textField_0, textField_1, textField_2 and so on.
izvucen = rnd.nextInt(31) + 1;
randomField.setText(Integer.toString(izvucen));
textField_i.setText(Integer.toString(izvucen)); // critical line, need to replace "i" with something
i++;
You could put all text fields in a list when they are created and access them via indexing
txtFields.get(i).setText(...)
Or put all fields in a map (Map<Integer, TextField>) and access specific ones via get()
mapOfTextFields.get(i).setText()
Heyy. I am writing a tool in Java which deletes non-whitelisted files. Don't ask. It lists a directory that the user selects with the JFileChooser. This works well, all filenames are listet in a JList (in my case inside a JScrollPane) and displayed inside of the frame.
Now i want to read filenames from a .txt file and check if they match the names in the list. The reading and comparing is no problem. But i would like to set a green background if the file matches one of the files inside the .txt file. If not, then the background of the displayed item should be red. But how can i change these backgrounds for one line if its even possible?
Simple, set a custom ListCellRenderer to your JList using:
list.setCellRenderer(myListCellrenderer);
Now inside the overridden method getListCellRendererComponent() do something like this:
public Component getListCellRendererComponent(.....) {
Component c = super.getListCellRendererComponent();
c.setBackGround(Color.blue)
return c;
}
When the logic determines you should show the row as green (when the file names are equal), you also have the option of setting the state on the backing object of the row and test it for that state within getListCellRendererComponent(), setting the background green if the state is correct. Again, you have the option of setting a Swing Timer to revert the state on the backing object.
I have a properties page that looks like this
property1=value1
property2=value2
property3=value3
property4=value4
And my plan is to be able to create a page in my app that will display these values and allow the user to change and save them.
The way that I thought I would do this is to create a method that will iterate through the file and populate a text control with the data.
I am using the 'propertyNames' function explained here https://www.codenameone.com/javadoc/com/codename1/io/Properties.html
Unfortunately when I use this, all that is listed are the properties and not the values
Is there a way to show the entire contents of the properties file?
The other way I was thinking about doing it is to create a list of buttons that correspond to the keys - so one button for each key [with the key name as the button label] - and then when the user clicks on the button it displays the key value in an editable text box, and a save button that writes it back to the file.
Is this possible?
Thanks
propertyNames gives you all the keys, then you can retrieve the value of each get key using getProperty:
for(String key:props.propertyNames()){
String value = props.getProperty(key);
output += key+"="+value+"<br/>"; //however you output your stuff
}
After letting the user set new values, you can set them using setProperty(key, newValue).
I'm new to GWT. I have a simple SuggestBox which is populated using a MultiWordSuggestOracle. Users input their data to this SuggestBox, and if they find any match with the existing Suggestions its well and good. I'm able to retrieve this value in the SelectionHandler code as below.
display.getSuggestBox().addSelectionHandler(new SelectionHandler<Suggestion>() {
public void onSelection(SelectionEvent<Suggestion> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
// do something with the property value
}
});
But users are allowed to enter values which are not already in the Suggestion oracle, in which case I should read this value and do something with this,may be saving to db as a new data.(The thing which I'm looking for is something like a browsers navigation widget where we show suggestions, users can pick up any suggestion or he can type in his new entry and carry on.) What I needed is a way to retrieve this new text user has entered? Data will be read on a button click. What I tried out is this.
display.getSaveBtn().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String selectedProperty = display.getSuggestBox().getValue();
//String selectedProperty2 = display.getSuggestBox().getText();
// Blank in both cases :(
// tried display.getSuggestBox().getTextBox().getValue(),but blank again
}
});
I tried to employ onChange() event handlers (as shown below)
display.getSuggestBox().addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(ValueChangeEvent<String> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
Window.alert("on change -- "+selectedProperty);
}
});
This is working fine except one scenario. Suppose there are two suggestions in the oracle,say 'createTicketWsdl' and 'createTicketTimeout'. When the user types in 'cr', he is opted with these two options, and if he selects 'createTicketWsdl' by pressing keyboard ENTER, then my alert is printing 'createTicketWsdl' which is correct. But if he selects 'createTicketWsdl' using mouse, then my alert is printing 'cr' (I tried to post the screenshot to give a better understanding, but being a new user I'm not allowed).(which I wanted to get as 'createTicketWsdl'since thats what he has selected). Soon after printing my alert, the value in the SuggestBox changes to 'createTicketWsdl'.
Is there a way to retrieve the value of the suggest box? I saw a similiar thread GWT SuggestBox + ListBox Widget, where some source code for a custom widget is available. But I didn't take the pain of trying out that, since what I want is simply get the current value from the SuggestBox and I hope there should be some easy way.
Thanks for all your help!
Your question is not very clear. You need to clarify your language a lil' bit. For example - is the following a question or an assertion? I mean, it sounds like an assertion but it has a question mark.
What I needed is a way to retrieve this new text user has entered?
Also, I do not understand what you mean by "he is opted by". Did you mean to say, "he is presented with the options ..." ?
Therefore, I am guessing your situation.
You have a listbox of existing items.
You have a textbox which allows freeform text entry
Any items whose prefix values matches the current textbox entry, the listbox items would be filtered to be limited to the matching items.
Even if the current textbox entry presents matching prefixes to filtering the listbox, the user can still perform freeform text entry. So, there are two possible cases here
4.1 the user clicks on the list box to select one of the filtered items
4.2 the user press enter key, which triggers selection of the current value of the textbox.
However, you find your widget participating in a race condition, so that when you click on the widget, the ValueChangeHandler gets triggered rather than the SelectionHandler. I do not know the structure of your widget so that is my best guess.
The problem is that you are allowing two separate modes of obtaining an outcome and you probably did not have well-defined state machine to handle choosing the appropriate mode. One mode is by the textbox and the other is by selection on the listbox - and you do not have a well-defined way of which would mode would be effective at any moment.
If my guess is accurate, this is what you need to do:
You must restrict your outcome to coming from only the textbox.
Your listbox selection must not trigger any outcome. Any change in listbox selection must propagate back to the textbox - to allow the user the chance of making further freeform entry based on that value.
only the keyboard enter on the textbox will trigger the final outcome.
It may seem strange, but I need to get values of non-selected radio buttons for each radio button's group. I have used the code below to get all the selected buttons values, but I need to get the unselected ones.
ArrayList <String> userSelection = new ArrayList <String>();
Enumeration names = request.getParameterNames();
String selection = "";
while ( names.hasMoreElements() )
{
name = (String) names.nextElement();
userSelection.add(request.getParameter( selection ));
}
The browser will not send you the non-selected buttons. What you'll need to do is either:
Have your code know what all the buttons will be
Create a hidden field with a list of all the possible values.
If you go with #2, take heed of Mr HEBERT'S suggestion to never trust user input.
You simply can't get those values form your request.
It's the browser which will create the request and it doesn't send informations that appears useless for the navigation (such as unused values).
The only way to do that would be to guess the values.
Remember this request could be forged by hand so, never trust user input.