I'm creating an speed reader so I'm using a swingx.Timer object to display the words at a given time.I set the time to 1sec for testing. I had the code working before but then after closing eclipse and coming back to it the code doesn't work anymore. The timer does not call the actionlistener thus not performing the action it is supposed to perform. Here is my code. I read other questions talking about timer being a daemon thread and ending with main. However I don't think this is the case in my code but I could be wrong. Can someone help me point out my error?
package reader;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JMenu;
import java.awt.event.*;
import java.io.IOException;
import java.sql.Time;
import javax.swing.Timer;
public class ReaderPad extends JPanel{
public JButton startBtn, stopBtn, resetBtn;
public JList speedMenu;
public JMenuBar topMenu;
public JTextPane textPad;
public JScrollPane scroll;
public Timer timer;//to control the speed of the reader
public int speed; //the speed set for the reader, i.e. 100 words per minute
public JLabel speedLabel;
public JTextField speedText;
int wordLength; //size of text
String[] lines; //a line of text composed of 10 words from the text entered
int wpm;
int i;//index of word set being used
public ReaderPad(){
this.setLayout(new BorderLayout());
i = 0;
speedLabel = new JLabel("words per minute");
speedText = new JTextField();
speedText.setColumns(3);
startBtn = new JButton("Start");
stopBtn = new JButton("Stop");
resetBtn = new JButton("Reset");
textPad = new JTextPane();
topMenu = new JMenuBar();
textPad.setEditable(true);
textPad.setFont(new Font("Serif", Font.PLAIN,25));
textPad.setText("welcome to speed reader. Use CTRL + c to paste your text\n"
+ "Press start to begin");
scroll = new JScrollPane(textPad);
//create top menu bar
topMenu.add(startBtn);
topMenu.add(stopBtn);
topMenu.add(resetBtn);
topMenu.add(speedLabel);
topMenu.add(speedText);
//topMenu.add(speedMenu);
this.add(topMenu, BorderLayout.NORTH);
this.add(scroll, BorderLayout.CENTER);
//set listeners for buttons
Actions listener = new Actions();
startBtn.addActionListener(listener);
stopBtn.addActionListener(listener);
resetBtn.addActionListener(listener);
}//end of constructor
public void setFontSize(String size){
Font font = textPad.getFont();
String s = font.getName();
textPad.setFont(new Font(s, Font.PLAIN, Integer.valueOf(size)));
}
public void setFontColor(String color){
}
//sets background color
public void setBackgroud(String color){
}
public void setSpeed(String speed){
//speed = speed.replace("X", "");//remove the times symbol
if(speed.isEmpty())//set default
this.speed = 100;
else
this.speed = Integer.valueOf(speed);
}
public int getSpeed(){
return this.speed;
}
public void startReader(){//words per minute
int speed = getSpeed();//wpm selected by the user
System.out.println("speed: " + speed);
String text = textPad.getText();
// System.out.println(text);
lines = text.split(" ");
wordLength = lines.length;//get the number of words in the text
System.out.println("length: "+ wordLength);
//System.out.println(wordLength.length);
if(text.isEmpty()){
textPad.setText("You didn't enter any text in the text area.\n"
+"Please enter some text to begin.");
}
else{//set timer
//calculate speed first: time = (speed chosen * number of workds in text) = (sec/words)*wordsize
//wpm = (1/speed)*wordLength*60*1000; //multiply by 60 secons
wpm = 1000;
System.out.println("wpm: "+ wpm);
TimerReader counter = new TimerReader();
timer = new Timer(1000, counter);
timer.start();
System.out.println(timer.isRunning());
}
}
public void stopReader(){
}
//listener class
public class Actions implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
String source = e.getActionCommand();//read the button asking for the request
if(source.equals("Reset")){
textPad.setText("");
if(timer.isRunning())
timer.stop();
}
else if(source.equals("Start")){
//first read the speed selected and set the speed
setSpeed(speedText.getText());
startReader();
}
else if (source.equals("Stop"))
i = 0;
timer.stop();
}
}
public class TimerReader implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){
//once the timer is called the textpad starts displaying the words
String s = lines[i];
i++;
textPad.setText(s);
System.out.println(i);
System.out.println(e.getActionCommand());
System.out.print(textPad.getText());
}
}//end of inner class
}//end of ReaderPad class
Remove timer.stop() from the end of Actions actionPerformed method...
public class Actions implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String source = e.getActionCommand();//read the button asking for the request
if (source.equals("Reset")) {
textPad.setText("");
if (timer.isRunning()) {
timer.stop();
}
} else if (source.equals("Start")) {
//first read the speed selected and set the speed
setSpeed(speedText.getText());
startReader();
} else if (source.equals("Stop")) {
i = 0;
}
// This is stopping the timer AFTER it was already started
//timer.stop();
}
}
Related
I am making a vocabulary quiz program and when they click an option, a new window is supposed to pop up saying if they got it right or wrong. That works, but what doesn't show is the button in the window when it gives you the option to go back to the quiz, ruining the chance to continue onto the next question, or even worse, answering the current question.
Here is the main quiz code:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
public class SAT extends JFrame implements ActionListener{
private JButton buttonA;
private JButton buttonB;
private JButton buttonC;
private JButton buttonD;
private JPanel panel1;
private JFrame frame1;
private JTextField Text1;
private JLabel label1;
public static final String POOP = "Words3.xls"; //everywhere that there is POOP is the excel sheet
public static String read1(int rowIndex, int colIndex, int a)
{
String value = new String();
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(new FileInputStream(POOP));
} catch (Exception e) {
e.printStackTrace();
}
String sheet2=null;//placeholder
if (a==1){sheet2="SAT";}//one of these for each sheet
if (a==2){sheet2="Test";}
if (a==3){sheet2="vwlvlHU1";}
HSSFSheet sheet = wb.getSheet(sheet2);
HSSFRow row=sheet.getRow(rowIndex-1);
HSSFCell cell=row.getCell(colIndex-1);
DataFormatter formatter = new DataFormatter();
value = formatter.formatCellValue(cell);
return value;
}
private static void saveWorkbook(HSSFWorkbook wb) throws IOException
{
FileOutputStream out = new FileOutputStream(POOP);
wb.write(out);
out.close();
}
public static int RNG1(int a) { //this RNG is used to find the word and random definitions
//RNG code:
int min=1;//1
int max=100;//number of flash cards, highest number, this is a placeholder
if (a==1){max=253;}//we will have one of these statements for each set
if (a==2){max=20;}
if (a==3){max=40;}
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min + 1;//the +1 is so it doesn't choose header, maybe remove
return randomNum;
}
public static int RNG2(int b) {//this RNG will be used to determine which button gets true definition
//RNG code:
int min=1;//1
int max=4;//number of options
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
//FIND THE WORD AND POS
int a=1;//tells methods to look in right set
int rowIndex=RNG1(a);//here is where a random word, pos, and def will be called for
int colIndex=1;//use colIndex=1 to show word
String WORD=read1(rowIndex,colIndex,a);//to pull word
//System.out.println("Word: "+WORD);
int colIndexPOS=2;//POS=part of speech/colIndexPOS=2 for part of speech
String POS=read1(rowIndex,colIndexPOS,a);
//System.out.println("Part of speech: "+POS);
int b=1;//placeholder
int location=RNG2(b);
//if location=1, put it in button 1 and have random definitions for other 3
//if location=2... same for 3 and 4
private String def(){
//TRUE DEFINITION:
int colIndexDEF=3;//DEF= definition/colIndexPOS=3 for definitions
String DEF=read1(rowIndex,colIndexDEF,a);
return DEF;
}
//FAKE DEFINITIONS:
//fake def 1:
private String rdef1(){//THIS BLOCK HAS AN ISSUE. SHOULD NOT BE DIVIDED BY CURLY BRACES BUT IT IS AN ERROR IF THEY ARE NOT THERE. MAYBE MAKE IT A METHOD?
int rowIndex1=0;//null value
int colIndexDEF=3;
String weirdo="WHY";
do{
rowIndex1=RNG1(a);
weirdo=read1(rowIndex1, colIndexPOS, a);
}while(rowIndex1==rowIndex || !weirdo.equals(POS));
String RANDDEF1=read1(rowIndex1, colIndexDEF, a);
return RANDDEF1;
}
//fake def 2:
private String rdef2(){//FIX DOWHILE
int rowIndex2=1;//null value
int colIndexDEF=3;
String POSsave1=null;
//do{
rowIndex2=RNG1(a);
POSsave1=read1(rowIndex2, colIndexPOS, a);
//}while(rowIndex2==rowIndex1 || rowIndex2==rowIndex || !POSsave1.equals(POS));
String RANDDEF2=read1(rowIndex2, colIndexDEF, a);
return RANDDEF2;
}
//fake def 3:
private String rdef3(){//FIX DOWHILE
int rowIndex3=1;//null value
int colIndexDEF=3;
String POSsave2=null;
//do{
rowIndex3=RNG1(a);
POSsave2=read1(rowIndex3, colIndexPOS, a);
//}while(rowIndex3==rowIndex || rowIndex3==rowIndex2 || rowIndex3==rowIndex1 || !POSsave2.equals(POS));
String RANDDEF3=read1(rowIndex3, colIndexDEF, a);
return RANDDEF3;
}
private void createButtonA() {
String definition=null;
if (location==1){
definition=def();
}
if (location==2){
definition=rdef1();
}
if (location==3){
definition=rdef2();
}
if (location==4){
definition=rdef3();
}
buttonA = new JButton(definition);
add(buttonA);
buttonA.setContentAreaFilled(false);
buttonA.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
buttonA.setForeground(Color.blue);
buttonA.setBounds(45,130,295,25);
}
private void createButtonB() {
String definitionB=null;
if (location==1){
definitionB=rdef1();
}
if (location==2){
definitionB=def();
}
if (location==3){
definitionB=rdef3();
}
if (location==4){
definitionB=rdef2();
}
buttonB = new JButton(definitionB);
add(buttonB);
buttonB.setContentAreaFilled(false);
buttonB.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
buttonB.setForeground(Color.blue);
buttonB.setBounds(45,95,295,25);
}
private void createButtonC() {
String definitionC=null;
if (location==1){
definitionC=rdef2();
}
if (location==2){
definitionC=rdef3();
}
if (location==3){
definitionC=def();
}
if (location==4){
definitionC=rdef1();
}
buttonC = new JButton(definitionC);
add(buttonC);
buttonC.setContentAreaFilled(false);
buttonC.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
buttonC.setForeground(Color.blue);
buttonC.setBounds(45,60,295,25);
}
private void createButtonD() {
String definitionD=null;
if (location==1){
definitionD=rdef3();
}
if (location==2){
definitionD=rdef2();
}
if (location==3){
definitionD=rdef1();
}
if (location==4){
definitionD=def();
}
buttonD = new JButton(definitionD);
add(buttonD);
buttonD.setContentAreaFilled(false);
buttonD.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
buttonD.setForeground(Color.blue);
buttonD.setBounds(45,25,295,25);
}
public static void main(String[] args) {
SAT frameTable = new SAT();
//frameTable.SAT(); //Creating Frame
frameTable.createButtonA(); //Creating Button 1
frameTable.createButtonB(); //Creating Button 2
frameTable.createButtonC();//Creating button 3
frameTable.createButtonD();//Creating button 3
}
public SAT() {//problem
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //using the FlowLayout manager
setSize(400,250); //setting the size of the initial box
setVisible(true); //allows for manual size changing of the box
setTitle("FlaQuiZ"); //Title for the GUI
window.setBackground(Color.white);
setLocation(500,280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Allows us to actually exit when we click the "X"
panel1 = new JPanel(new GridLayout());
add(new JLabel("Choose the option that best fits this word: "+WORD));
//add(new JLabel(""));
}
public void actionPerformed(ActionEvent i) {
if ( location==1 && i.getSource() == buttonA || location==2 && i.getSource()==buttonB || location==3 && i.getSource()==buttonC || location==4 && i.getSource()==buttonD) {
GRATS congrats =new GRATS();
congrats.setVisible(true);
dispose();
}
else{
NO bad =new NO();
bad.setVisible(true);
dispose();
}
}
}
And then here would be the code if they got it wrong, where the button should appear to give the option to go back:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class NO extends JFrame implements ActionListener{
private JButton NOB;
private JPanel panel3;
public NO() {
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //using the FlowLayout manager
setSize(275,160); //setting the size of the initial box
setVisible(true); //allows for manual size changing of the box
setTitle("Flaquiz"); //Title for the GUI
window.setBackground(Color.white);
setLocation(500,280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Allows us to actually exit when we click the "X"
JPanel panel3 = new JPanel(new GridLayout());
add(new JLabel("Uh oh, you got it wrong!"));
//add(new JLabel(""));
}
public static void main(String[] args) {
NO bad = new NO();
//frameTable.SAT(); //Creating Frame
bad.createbuttonNOB(); //Creating Button 1
}
private void createbuttonNOB() {
NOB = new JButton("Go Back");
add(NOB);
NOB.setContentAreaFilled(false);
NOB.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
NOB.setForeground(Color.blue);
NOB.setBounds(45,45,45,45);
}
public void actionPerformed(ActionEvent i) {
if (i.getSource() == NOB ) {
SAT goback =new SAT();
goback.setVisible(true);
dispose();
}
}
}
I may be missing something, but it seems you add the button directly to the JFrame even though you have a JPanel occupying the whole frame. Why not add the button to the JPanel?
Side Note: In the NO constructor, SetVisible doesn't determine whether or not you can manually change size. That's controlled by things like SetMaximumSize, SetMinimumSize, and SetResizable.
Currently i am working on DEVSJAVA models and trying to make a GUI for those models.
In the below code i have written a displayphase class.In this class whenever a new input arrives it goes into the delext function and then reads the value of the varaible "result" and call the showstate function and then it should paint the result on the JFrame and again when another input arrives it should repaint on the JFrame.
But in the code what i have written makes all the panels print on the JFrame rather than repainting it. I know the error is in adding new panel everytime it goes into the showstate function. But i am unable to make it work.Could please help me out from this error.
package assignment2;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import simView.*;
import genDevs.modeling.*;
import genDevs.simulation.*;
import GenCol.*;
import simView.*;
public class displayphase extends ViewableAtomic{//ViewableAtomic is used instead
//of atomic due to its
//graphics capability
protected entity job;
protected double cul,cll,hul,hll,temp,varout,output,a,b,c,d,g;
protected double processing_time,temperature,temp1,temp2;
protected boolean acStatus,heaterStatus;
protected String result;
**JFrame f=new JFrame();**
public displayphase(){
this("displayphase"
);
}
public displayphase(String name){
super(name);
addInport("in");
addOutport("out");
addInport("in1");
addOutport("out1");
addOutport("out2");
addOutport("out3");
addRealTestInput("in1",71);
addRealTestInput("in",75);
addTestInput("in",new job("job","coolair",72.5),1);
addTestInput("in",new job("job",true,false,60),0);
addRealTestInput("in",3,1);
// processing_time = Processing_time;
}
public void initialize(){
phase = "passive";
sigma = INFINITY;
a=0;b=0;c=0;d=0;g=0;
job = new entity("job");
super.initialize();
}
public void deltext(double e,message x)
{
Continue(e);
if (phaseIs("passive"))
for (int i=0; i< x.getLength();i++)
if (somethingOnPort(x,"in"))
{ job dummy;
entity ent = x.getValOnPort("in",i);
// doubleEnt tem = (doubleEnt) job;
dummy = (job) ent;
temp= dummy.getthetemperature();
result = dummy.getthestate();
heaterStatus = dummy.isHeaterStatus();
System.out.println("The phase in ac"+result);
temperature = temp;
holdIn("busy",processing_time);
}
if (phaseIs("passive"))
for (int i=0; i< x.getLength();i++)
if (somethingOnPort(x,"in1"))
{ job dummy;
entity ent = x.getValOnPort("in1",i);
// doubleEnt tem = (doubleEnt) job;
dummy = (job) ent;
temp= dummy.getthetemperature();
result = dummy.getthestate();
System.out.println("The phase in heater"+result);
heaterStatus = dummy.isHeaterStatus();
temperature=temp;
holdIn("busy",processing_time);
}
**showstate()**;
}
public void deltint( )
{
passivate();
}
public void deltcon(double e,message x)
{
/*deltint();
deltext(20,x);
*/}
public message out( )
{
return outputNameOnPort(result,"out");
}
**public void showstate(){
f.add(new MyPanel());
f.repaint();
f.pack();
f.setVisible(true);
}
class MyPanel extends JPanel {
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
String ac,ab;
if(result == "aboveHT" || result=="coolAir")
ac = "cooler on";
else
ac = "cooler off";
/* else if(result == "belowHT" || result == "passive" || result=="belowH")
ac = "cooler off";*/
if(result == "belowHt" )
ab = "Heater on";
else
ab="Heater off";
//String ac=String.valueOf(timesacon());
// JFrame f=new JFrame();
JLabel l=new JLabel("THE STATUS OF AC IS ",JLabel.CENTER);
JLabel p=new JLabel("THE STATUS OF HEATER IS",JLabel.CENTER);
/* p.setVerticalTextPosition(JLabel.BOTTOM);
p.setHorizontalTextPosition(JLabel.CENTER);*/
JTextField t=new JTextField("");
t.setSize(80, 40);
t.setText(ac);
Point p1=new Point(100, 100);
t.setLocation(100,100);
l.setLocation(80, 80);
JTextField v=new JTextField("");
v.setSize(80,40);
v.setText(ab);
Point p2=new Point(100,200);
v.setLocation(p2);
p.setLocation(80, 180);
this.add(l);
this.add(p);
this.add(t);
this.add(v);
this.setSize(500, 300);
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.pack();
this.setVisible(true);
}
}
}**
Do not modify that state of any component from within any paintXxx method. This will trigger a cascade of repaint events, which will continue to be called until you CPU is running hot and you program is unresponsive...
The paintXxx methods are used to provide the ability to perform custom painting of a component, not modify its state.
Instead, construct the basic UI in the constructor and provide some means by which the state of the fields can be changed, for example, via a updateState method...
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class MyPanel extends JPanel {
private JTextField t;
private JTextField v;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
JLabel l = new JLabel("THE STATUS OF AC IS ", JLabel.CENTER);
JLabel p = new JLabel("THE STATUS OF HEATER IS", JLabel.CENTER);
t = new JTextField("");
v = new JTextField("");
this.add(l);
this.add(p);
this.add(t);
this.add(v);
}
public void updateState(String result) {
String ac, ab;
if ("aboveHT".equals(result) || "coolAir".equals(result)) {
ac = "cooler on";
} else {
ac = "cooler off";
}
if ("belowHt".equals(result)) {
ab = "Heater on";
} else {
ab = "Heater off";
}
t.setText(ac);
v.setText(ab);
}
}
String comparison in Java is done via the String#equals method. Using == is simply comparing the object memory reference, which has a very low likelihood of ever being true
Without knowing more, I would create an instance of a JFrame, add MyPanel to it and simply call MyPanel#updateState when you need to change it's state.
Modern UI's are expected to be capable of running on a variety of platforms, even when running the same OS, there are differences in the way that fonts may be rendered, making any statically placed and sized components unusable. Instead you should make use of the layout manager API which has being designed to solve this problem with minimal work on your part.
See Laying out components within a container
I have 3 clasess : Loader, MyDialog and TEST(with main method). (for code see below)
Everything I want to achieve is create simple dialog with JLabel and JProgressBar, which will notify user about how much time remains to show MyDialog. MyDialog is Jdialog with time consuming operation in constructor (loading data from database etc.).
In code below is model situation. When "MyDialog" is created by main (constant BY_USER is false), everything working exactly i want to. But when i make dialog with button , and instance of MyDialog is created after button press (constant BY_USER is true), Loader is blank white form. It looks like is not completed.
Loader is extending Thread, so i suppose that problem will be in threading (event dispatch thread)? I dont know, what is wrong and how fix it. Please help.
Thanks and sorry for my English.
CLASS TEST :
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class TEST {
public static final boolean BY_USER = false;
public static void main(String[] args) {
if (BY_USER) {
JFrame mainDialog = new JFrame("Main");
JButton show = new JButton("Show MyDialog");
show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
MyDialog dialog = new MyDialog();
}
});
mainDialog.add(show);
mainDialog.setLocationRelativeTo(null);
mainDialog.setMinimumSize(new Dimension(160, 80));
mainDialog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainDialog.setVisible(true);
} else {
MyDialog dialog = new MyDialog();
}
}
}
CLASS MyDialog :
package test;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MyDialog extends JFrame{
public MyDialog() {
super();
// making loader with title, first message and count of steps of operation
Loader loader = new Loader("Loader", "First showed message", 100);
loader.ShowLoader();
// time-consuming operation (loading data from database etc.).
// for clarity replaced with for statement
int j=0;
for(int i=0; i<Integer.MAX_VALUE; i++)
{
j++;
if(j==Integer.MAX_VALUE/100){
// updating loader message and progress bar value
loader.NewAction(Integer.MAX_VALUE - i+"");
j=0;
}
}
// closing loader
loader.DestroyLoader();
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
CLASS Loader:
package test;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
public class Loader extends Thread{
private JDialog dialog;
private JLabel message = new JLabel("", SwingConstants.CENTER);
private JProgressBar progressBar = new JProgressBar(0, 100);
private String newMessage;
private double percentForStep;
private int remainingSteps;
public Loader(String taskName, String firstMessage, int steps) {
this.remainingSteps = steps-1;
dialog = new JDialog((Dialog) null, taskName);
dialog.setLayout(new BorderLayout(15, 15));
dialog.add(message, BorderLayout.CENTER);
dialog.add(progressBar, BorderLayout.SOUTH);
message.setText(firstMessage);
percentForStep = 100 / steps;
}
public void ShowLoader()
{
dialog.setMinimumSize(new Dimension(400,120));
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
this.start();
}
public void DestroyLoader(){
dialog.dispose();
this.interrupt();
}
public void NewAction(String newMessage){
this.newMessage = newMessage;
this.remainingSteps--;
Lock.changed = true;
}
public int RemainingStepsCount()
{
return remainingSteps;
}
#Override
#SuppressWarnings({"CallToThreadYield", "SleepWhileInLoop"})
public void run() {
do{
synchronized (Lock.class) {
if (Lock.changed) {
Lock.changed = false;
this.message.setText(newMessage);
this.progressBar.setValue((int)(100-(remainingSteps*percentForStep)));
dialog.repaint();
}
dialog.repaint();
}
}while(true);
}
}
class Lock{
static boolean changed = false;
}
Look to SwingWorker and his use; I think it can help you to solve the problem.
I'm trying to got a tooltip which displays the current progress of a task. So I want that the tooltip text change while the tooltip is displayed. But, when I call setToolTipText() the displayed text remains the same until I exit the mouse from the tooltip component and enter again. And call setToolTipText(null) before doesn't change anything.
Indeed it does not update itself, even when resetting the tooltip to null between calls.
So far, the only trick I found was to simulate a mouse-move event and forward it on the TooltipManager. It makes him think that the mouse has moved and that the tooltip must be relocated. Not pretty, but quite efficient.
Have a look at this demo code which displays a progress in % from 0 to 100:
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.ToolTipManager;
public class TestTooltips {
protected static void initUI() {
JFrame frame = new JFrame("test");
final JLabel label = new JLabel("Label text");
frame.add(label);
frame.pack();
frame.setVisible(true);
Timer t = new Timer(1000, new ActionListener() {
int progress = 0;
#Override
public void actionPerformed(ActionEvent e) {
if (progress > 100) {
progress = 0;
}
label.setToolTipText("Progress: " + progress + " %");
Point locationOnScreen = MouseInfo.getPointerInfo().getLocation();
Point locationOnComponent = new Point(locationOnScreen);
SwingUtilities.convertPointFromScreen(locationOnComponent, label);
if (label.contains(locationOnComponent)) {
ToolTipManager.sharedInstance().mouseMoved(
new MouseEvent(label, -1, System.currentTimeMillis(), 0, locationOnComponent.x, locationOnComponent.y,
locationOnScreen.x, locationOnScreen.y, 0, false, 0));
}
progress++;
}
});
t.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initUI();
}
});
}
}
Here's a simplified version of Guillaume Polet's answer which is self-contained in a single method. This code assumes one has called component.setToolTip("..."); previously. This code does not show how to periodically update the tooltip to show the progress.
public static void showToolTip(JComponent component)
{
ToolTipManager manager;
MouseEvent event;
Point point;
String message;
JComponent component;
long time;
manager = ToolTipManager.sharedInstance();
time = System.currentTimeMillis() - manager.getInitialDelay() + 1; // So that the tooltip will trigger immediately
point = component.getLocationOnScreen();
event = new MouseEvent(component, -1, time, 0, 0, 0, point.x, point.y, 1, false, 0);
ToolTipManager.
sharedInstance().
mouseMoved(event);
}
Is it possible to change the color of a text in a text field?I am trying to build an interpreter, so I was wondering on how would you change the color of the text in real time.
For example the word I enter in the text field is:
printf("hi");
The word printf becomes green after a few seconds.
Is it possible?
package test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class BlinkColorTextField {
BlinkColorTextField() {
final JTextField blinkingText = new JTextField("Red & Blue");
ActionListener blinker = new ActionListener() {
boolean isRed = true;
public void actionPerformed(ActionEvent ae) {
if (isRed) {
blinkingText.setForeground(Color.BLUE);
} else {
blinkingText.setForeground(Color.RED);
}
isRed = !isRed;
}
};
Timer timer = new Timer(1000, blinker);
timer.start();
JOptionPane.showMessageDialog(null, blinkingText);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new BlinkColorTextField();
}
});
}
}
Try this:
HighlightPainter greenPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
//in a thread...
Highlighter h = tf.getHighlighter();
h.addHighlight(offset, offset+length, greenPainter);
You have to use JEditorPane / JTextPane instead of JTextField and also you can draw the text/string by overriding the paintComponent method.