I encounter a strange issue.
The scenario is that I need to replace a keyword in JTextArea with another word.
I have two buttons, one is to find the keyword, and the other is to replace the keyword.
For both buttons, I add the mouse listener and implement function mouseClicked. At current stage I can highlight the found keyword and scroll to the keyword position. But when dealing with replace button, after re-setting text, the JTextArea always scrolls down to the bottom, but I want to keep the position where the replacement happens, what should I do? Below is my code snippet, but it doesn't work.
replaceBtn.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
String keyword = jKeyword.getText();
if (keyword.length() == 0)
return;
String text = jTextArea.getText();
pos = text.indexOf(keyword, 0);
if (pos == -1) {
pos = 0;
JOptionPane.showMessageDialog(null, "can not find " + keyword);
return;
}
jTextArea.setText(text.replaceFirst(keyword, jReplaceKW.getText()));
//jTextArea.revalidate();
//scroll to first keyword occurrence
try {
Rectangle rectangle = jTextArea.modelToView(pos);
jTextArea.scrollRectToVisible(rectangle);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
I found a workaround: set text in function mousePressed then scroll in function mouseReleased.
I suspect the text has to be represented in the GUI before the component can figure out the scroll dimension? Not sure.
replaceBtn.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
String keyword = jKeyword.getText();
if (keyword.length() == 0)
return;
String text = jTextArea.getText();
pos = text.indexOf(keyword, 0);
if (pos == -1) {
pos = 0;
JOptionPane.showMessageDialog(null, "can not find " + keyword);
return;
}
jTextArea.setText(text.replaceFirst(keyword, jReplaceKW.getText()));
}
#Override
public void mouseReleased(MouseEvent e) {
//scroll to first keyword occurrence
try {
Rectangle rectangle = jTextArea.modelToView(pos);
jTextArea.scrollRectToVisible(rectangle);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
Related
So, i was able to make my code run, however i am having trouble with the highscore code. I am unable to use the bufferedreader and printwriter functions because for some reason that i am not understanding, they are not running. I want the program to compare the score to the highscore, and if the score is larger than the highscore, the highscore will be updated on a txt file. the reason the txt file is necessary is due to the fact that once the program closes, i need a method as to check for the previous highscore. I am really new to using processing and writing and reading txt files using programs, and none of the other sites and forums ive looked at have helped because they do not write the highscore variable onto a txt file. Please help, im ready to break my computer.
EM1.score = the score accumulated over the course of the program.
class High_Score {
int highscore = 0;
PrintWriter output; //imports the writer
BufferedReader reader; //imports the reader
int state = 0; //sets the varoable for the keyboard
void scoring() {
int score = EM1.score;
if (score > highscore) {
highscore = score;
}
textSize(30);
text("Your score is "+ score, 150, height/4);
text("The current highscore is "+highscore+".", 75, height/2);
text("Try to beat it.", 200, 450);
textSize(12);
text("Press esc to exit this page.", 225, 550);
}
void reader() {
importHS();
updateHS();
}
void updateHS() {
int score = EM1.score;
output = createWriter("highscore.txt"); //creates the file that will be
if (highscore < score) {
highscore = score;
output.print(highscore);
output.close();
}
}
void importHS() {
reader = createReader("highscore.txt"); //reads the current highscore
if (reader == null) {
highscore = 0;
return;
}
String line;
try {
line = reader.readLine();
}
catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line != null) {
highscore = int(line);
println(highscore);
}
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
void type() { //allows the screen to close is esc is pressed on the keyboard
state = key;
if (key == ESC) {
exit();
}
}
}
You're almost there. There are only a couple of things I see slightly off:
you're not calling flush() on the writer to writes the remaining data to the file
checking if (highscore < score) in updateHS() is preventing the write: you don't really need this anyway since scoring() already checks if the current score is a highscore and updates accordingly.
Here's most of your code tweaked to write/read from disk:
EM1Class EM1 = new EM1Class();
High_Score hs = new High_Score();
void setup(){
size(800,600);
EM1.score = 500;
hs.reader();
}
void draw(){
background(0);
hs.scoring();
hs.updateHS();
}
void keyPressed(){
if(keyCode == UP) EM1.score += 10;
if(keyCode == DOWN) EM1.score -= 10;
}
class EM1Class{
int score;
}
class High_Score {
int highscore = 0;
PrintWriter output; //imports the writer
BufferedReader reader; //imports the reader
int state = 0; //sets the varoable for the keyboard
void scoring() {
int score = EM1.score;
if (score > highscore) {
highscore = score;
}
textSize(30);
text("Your score is "+ score, 150, height/4);
text("The current highscore is "+highscore+".", 75, height/2);
text("Try to beat it.", 200, 450);
textSize(12);
text("Press esc to exit this page.", 225, 550);
}
void reader() {
importHS();
updateHS();
}
void updateHS() {
int score = EM1.score;
output = createWriter("highscore.txt"); //creates the file that will be
output.print(highscore);
output.flush();
output.close();
}
void importHS() {
reader = createReader("highscore.txt"); //reads the current highscore
if (reader == null) {
highscore = 0;
return;
}
String line;
try {
line = reader.readLine();
}
catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line != null) {
highscore = int(line);
println(highscore);
}
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
void type() { //allows the screen to close is esc is pressed on the keyboard
state = key;
if (key == ESC) {
exit();
}
}
}
There'a placeholder EM1Class there just so a sketch could compile.
I'm not sure the High_Score class should know about EM1 directly.
You might want to refactor the code a bit and make sure it's not as tightly coupled as it currently is since you're using classes anyway.
Also check out a Java style guide: it will help to write code that is consistent and neatly organised. It will surely pay off building the discipline to do so on the long run, but also immediately as you're code will be easier to scan/quickly read and deduce it's flow.
Another option is to use the Processing built-in XML and JSON functions which will make be easier to parse (and also have handy load/save functions).
int score = 0;
int highScore;
void setup(){
loadHighScore();
}
void draw(){
background(0);
text("score:"+score+"\nhighScore:"+highScore+"\n\nuse UP/DOWN\narrows",10,15);
}
void keyPressed(){
if(keyCode == UP) score += 10;
if(keyCode == DOWN) score -= 10;
if(score > highScore) highScore = score;
}
void loadHighScore(){
try{
JSONObject jsonScore = loadJSONObject("highScore.json");
highScore = jsonScore.getInt("highScore");
}catch(Exception e){
println("error loading/parsing highScore.json");
e.printStackTrace();
}
}
void saveHighScore(){
JSONObject jsonScore = new JSONObject();
jsonScore.setInt("highScore",highScore);
saveJSONObject(jsonScore,"highScore.json");
}
//save on close
void exit(){
saveHighScore();
super.exit();
}
This is a part of my onActivity Result code. What i want to do is make a bitmap variable that i can modify everytime i press a button instead of modifying the ImageView(imagen) and not saving the changes that happen to the image.
When i try to change the btp_tmp variable inside the onClick code of the button it throws an error about btp_tmp being in an inner class and must be made final.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap btp_img = null;
InputStream in_stream;
Bitmap btp_tmp = null;
if (resultCode == Activity.RESULT_OK && requestCode == RCode)
{
//ARXIKO IMAGE
try {
if (btp_img != null) {
btp_img.recycle();
}
in_stream = getContentResolver().openInputStream(
data.getData());
btp_img = BitmapFactory.decodeStream(in_stream);
in_stream.close();
btp_tmp = btp_img;
imagen.setImageBitmap(btp_img);
//btn_seleccion.setText(getResources().getString(R.string.modifa));
} catch (IOException e) {
e.printStackTrace();
}
//NEGATIVE
//final boolean test = false;
final Bitmap finalBtp_img1 = btp_tmp;
//if (test == false){}
btp_tmp = btp_img;
Neg_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//imagen.setImageBitmap(invert(finalBtp_img));
Negative neg = new Negative();
imagen.setImageBitmap(neg.invert(finalBtp_img1));
btp_tmp = neg.invert(finalBtp_img1);
}
});
//UNDO
final Bitmap finalBtp_imgUndo = btp_img;
eraser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imagen.setImageBitmap(finalBtp_imgUndo);
}
});
I found the solution. The mistake i was doing is that i didn't declare the temporary variable in the Main Activity Class, so i got an error everytime i used the variable in the OnActivity part of the code.
Rookie mistake for sure.
I have a script that currently displays dialogue text letter by letter. Currently it rolls through each string of text in my _stanzas array without pausing. I would like to change this script so that it requires the user to press "Z" on their keyboard before the script begins to print the next block of text from _stanzas. I cant seem to figure it out...here is my code.
using UnityEngine;
using System.Collections;
public class Text : MonoBehaviour {
public float LetterPause = 0.1f;
public GameObject text;
public GameObject box;
public GameObject name;
private bool _textFinished;
private int _textIndex;
private string[] _stanzas;
int tracker;
public void Start()
{
if (Application.loadedLevel == 1) {
_stanzas = new [] {
" It’s Terrence’s hat... ",
"He always was one to lose track of the" + "\n" + "material objects in life," + "\n" + "but to hold onto the immaterial forever.",
"I’ll carry it for him" + "\n" + "until I find him. "
};
}
_textFinished = true;
_textIndex = -1;
tracker = _textIndex;
}
public void Update()
{
if (_textFinished)
{
if (_textIndex++ >= (_stanzas.Length - 1))
{
_textFinished = false;
text.SetActive(false);
box.SetActive(false);
name.SetActive(false);
}
if (_textFinished){
SetText(_stanzas[_textIndex]);}
}
}
private IEnumerator TypeText(float waitTime, string text)
{
_textFinished = false;
guiText.text = "";
foreach (var letter in text)
{
guiText.text += letter;
yield return new WaitForSeconds (waitTime);
}
_textFinished = true;
}
private void SetText(string text)
{
StartCoroutine(TypeText(LetterPause, text));
}
}
change
if (_textFinished)
to
if (_textFinished && (Input.GetKeyDown(KeyCode.Z) || _textIndex < 0))
I have a search text-box in my app. In my database there are two column named English and Bangla. I can search either Bangla or English. there is a button beside search text-box.By default English search is activated. I can change the search option by clicking the button. It works correctly but problem is that the search is very slow.
search option selection code by clicking button is:
private void button5_Click(object sender, RoutedEventArgs e)
{
if (SearchMood % 2 != 0)
{
//search bangla column from the database
button5.Content = "Eng";
}
else {
//search english column from the database
button5.Content = "Bng";
}
SearchMood++;
}
Code for searching is:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
List<dataLists> mylist = new List<dataLists>();
string word = textBox1.Text;
try
{
if (SearchMood % 2 == 0)// for english search
{
// show 5 words in listbox matched with entered text
var contacts = (from m in db.Dics where m.English.StartsWith(word) select new { m.English, m.Bangla }).Take(5);
string s1, s2;
try
{
foreach (var a in contacts)
{
s1 = a.English;
s2 = a.Bangla;
mylist.Add(new dataLists() { Eng = s1, Bng = s2 });
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
listBox1.ItemsSource = mylist;
}
else // for bangla search
{
// show 5 words in listbox matched with entered text
var contacts = (from m in db.Dics where m.Bangla.StartsWith(word) select new { m.English, m.Bangla }).Take(5);
string s1, s2;
try
{
foreach (var a in contacts)
{
s1 = a.English;
s2 = a.Bangla;
mylist.Add(new dataLists() { Eng = s1, Bng = s2 });
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
listBox1.ItemsSource = mylist;
}
}
catch { }
}
How can I increase the performance of searching??? Can anyone give any solution|???
N:B: My table creation script looks like
public System.Data.Linq.Table<Dic> Dics
{
get
{
return this.GetTable<Dic>();
}
}
public System.Data.Linq.Table<Learn_table> Learn_tables
{
get
{
return this.GetTable<Learn_table>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dic")]
public partial class Dic : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Serial;
private string _English;
private string _Bangla;
private System.Nullable<int> _Fav;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnSerialChanging(int value);
partial void OnSerialChanged();
partial void OnEnglishChanging(string value);
partial void OnEnglishChanged();
partial void OnBanglaChanging(string value);
partial void OnBanglaChanged();
partial void OnFavChanging(System.Nullable<int> value);
partial void OnFavChanged();
#endregion
public Dic()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="serial", Storage="_Serial", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Serial
{
get
{
return this._Serial;
}
set
{
if ((this._Serial != value))
{
this.OnSerialChanging(value);
this.SendPropertyChanging();
this._Serial = value;
this.SendPropertyChanged("Serial");
this.OnSerialChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="english", Storage="_English", DbType="NVarChar(2000)")]
public string English
{
get
{
return this._English;
}
set
{
if ((this._English != value))
{
this.OnEnglishChanging(value);
this.SendPropertyChanging();
this._English = value;
this.SendPropertyChanged("English");
this.OnEnglishChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="bangla", Storage="_Bangla", DbType="NVarChar(2000)")]
public string Bangla
{
get
{
return this._Bangla;
}
set
{
if ((this._Bangla != value))
{
this.OnBanglaChanging(value);
this.SendPropertyChanging();
this._Bangla = value;
this.SendPropertyChanged("Bangla");
this.OnBanglaChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="fav", Storage="_Fav", DbType="Int")]
public System.Nullable<int> Fav
{
get
{
return this._Fav;
}
set
{
if ((this._Fav != value))
{
this.OnFavChanging(value);
this.SendPropertyChanging();
this._Fav = value;
this.SendPropertyChanged("Fav");
this.OnFavChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Your problem appears in using TextChanged event Handler. Place a breakpoint there and you will see it firing twice and hence causing the slow performance for you. It seems a bug in WP7 TextBox control.
Use KeyUp event handler, instead of textBox1_TextChanged
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//your code
}
Hope this solves your problem. !!
You can use of AutoCompleteBox rather than use of TextBox. AutoCompleteBox available in Microsoft.Phone.Control.Toolkit.
Execute you select query at once when you select language on buttonClick and assign result of your query to AutoCompleteBox.Itemsource. It should really increase search performance.
<toolkit:AutoCompleteBox x:Name="AutoBoxFood" Width="440" SelectionChanged="txtFodd_SelectionChanged" FilterMode="StartsWith" HorizontalAlignment="Left" Height="70"/>
Add indexes to the columns in the database file.
I am unable to figure out how could I add a double click event to the cell of the CellTable.
Is it possible with GWT CellTable or not?
Is there any workaround
thank you..
al
BTW, i saw this post but there is no reply...
http://www.devcomments.com/Adding-DoubleClicks-and-OnContextMenu-to-CellTable-at1066168.htm
I crafted something different that just fit my needs:
cellTable.addCellPreviewHandler(new Handler<TitoloProxy>() {
long lastClick=-1000;
#Override
public void onCellPreview(CellPreviewEvent<TitoloProxy> event) {
long clictAt = System.currentTimeMillis();
GWT.log("clickAt: "+(clictAt));
GWT.log("lastClick: "+(lastClick));
if(event.getNativeEvent().getType().contains("click")){
GWT.log(""+(clictAt-lastClick));
if(clictAt-lastClick < 300) { // dblclick on 2 clicks detected within 300 ms
Window.alert("I am a double click crafted event!");
}
lastClick = System.currentTimeMillis();
}
}
});
cellTable.addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
Window.alert("That's it!");
}
}, DoubleClickEvent.getType());
Integer row=0;// to hold row index
Integer column=0;// to hold column index
_Grid.addCellPreviewHandler(new CellPreviewEvent.Handler<Model>() {
// this is to handle row id
#Override
public void onCellPreview(final CellPreviewEvent<Model> event) {
if (BrowserEvents.CLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
row = event.getIndex();
column=event.getColumn();
}
}
});
// because Doubleclick handler doesn't give row index or column index we will use addCellPreviewHandler to return row index or column index.
_Grid.addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(final DoubleClickEvent event) {
System.out.println(" You clicked row = " + row);
System.out.println(" You clicked column = " + column);
}
}, DoubleClickEvent.getType());
For cell lists, this code works ok:
cellList.addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
// do the stuff
}
}, DoubleClickEvent.getType());
I'm not sure about table cells
Because the CellPreview interface does not natively capture double click events you will need add event logic into the Overriden onCellPreview method. First you would think the best way would be to check the click time differences. However it is much more efficient and elegant to use a state machine and count clicks. This is more robust and allows you to deal with multiple event cases - Such as mouse hover, single, and double clicks. The code is pretty straightforward. So enjoy!
public class CellHoverHandler implements Handler<T> {
Timer singleClickTimer;
int clickCount = 0;
int clickDelay = 300;
#Override
public void onCellPreview(final CellPreviewEvent<T> event) {
if (Event.getTypeInt(event.getNativeEvent().getType()) == Event.ONMOUSEOVER) {
handleOnMouseOver(event);
} else if (Event.getTypeInt(event.getNativeEvent().getType()) == Event.ONCLICK) {
clickCount++;
if (clickCount == 1) {
singleClickTimer = new Timer() {
#Override
public void run() {
clickCount = 0;
handleOnClick(event);
}
};
singleClickTimer.schedule(clickDelay);
} else if (clickCount == 2) {
singleClickTimer.cancel();
clickCount = 0;
handleOnDblClick(event);
}
}
}
private void handleOnMouseOver(CellPreviewEvent<T> event) {
Element cell = event.getNativeEvent().getEventTarget().cast();
GWT.log("mouse over event");
}
private void handleOnClick(CellPreviewEvent<T> event) {
Element cell = event.getNativeEvent().getEventTarget().cast();
GWT.log("click event");
}
private void handleOnDblClick(CellPreviewEvent<T> event) {
Element cell = event.getNativeEvent().getEventTarget().cast();
GWT.log("double click event");
}
OPTIMIZATION: feel free to stick the count, timer, and delay as static class members or global members to reuse. Also check to see if the timer is null before making a new instance. I had omitted this for simplicity. Unlike a lot of other techniques this way still provides you with easy and direct access to the cell event. The technique with overloading the AbstractCell works well too, however sometimes you really don't have custom cells or want to make a custom cell to just handle events on the cell.
Leaving this here for future reference
private Set<GenericEventHandler<T>> dblClickHandlers = new HashSet<>(4);
dblClickHandlers simply maps interface implementations of my choice
table.addCellPreviewHandler(event -> {
if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
LOGGER.info("dblclick (native) " + event.getIndex() + " " + event.getColumn() + "; " + event.getValue());
dblClickHandlers.forEach(handler -> {
handler.onEvent(event.getValue());
});
}
});
table.sinkBitlessEvent(BrowserEvents.DBLCLICK);
The trick is to sink the 'dblclick' event.
If you wanted a text cell that allows you to support your own chosen list of events, you can use this:
public class EventfulTextCell extends AbstractSafeHtmlCell`<String`> {
private static final String[] NO_CONSUMED_EVENTS = null;
public EventfulTextCell() {
this(NO_CONSUMED_EVENTS);
}
public EventfulTextCell(String... consumedEvents) {
super(SimpleSafeHtmlRenderer.getInstance(), consumedEvents);
}
#Override
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
if (value != null) {
sb.append(value);
}
}
}
Then you instantiate it:
new EventfulTextCell("click", "dblclick")
Then override the onBrowserEvent() method to process your events.