How can I set background Color of JTextField in Java?
Page 1 of 1 • Share •
How can I set background Color of JTextField in Java?
I've got some problems while setting the background color of a JTextField . I think the following code should work for setting the background color of the JTextField .But It doesn't work . I also search for this in the Internet but I found the same thing which doesn't work .
Please anyone help me with the solution of this problem.
- Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Timer extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
Container container;
Thread t;
JTextField txtfield;
JButton startButton, stopButton;
public Timer() {
setSize(300, 300);
setLayout(null);
setLocationRelativeTo(null);
startButton = new JButton("Start");
startButton.setBounds(80, 200, 80, 30);
add(startButton);
stopButton = new JButton("Stop");
stopButton.setBounds(165, 200, 80, 30);
add(stopButton);
t = new Thread(this);
txtfield = new JTextField(16);
txtfield.setBounds(80, 100, 140, 50);
txtfield.setBackground(Color.RED);
add(txtfield);
txtfield.setEditable(false);
txtfield.setText("00 : 00 : 00 : 00");
txtfield.setFont(new Font("Time New Romans", Font.BOLD, 15));
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
t.start();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
t.stop();
}
});
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Timer();
}
int i = 0;
String s;
@Override
public void run() {
for (int i = 0;; i++) {
for (int v = 0; v < 10; v++) {
for (int j = 0; j < 6; j++) {
for (int b = 0; b < 10; b++)
for (int k = 0; k < 6; k++) {
for (int n = 0; n < 10; n++) {
for (int l = 0; l < 10; l++) {
for (int m = 0; m < 10; m++) {
String s1, s2, s3, s4, s5, s6, s7, s8;
s1 = Integer.toString(i);
s2 = Integer.toString(j);
s3 = Integer.toString(k);
s4 = Integer.toString(l);
s5 = Integer.toString(m);
s6 = Integer.toString(n);
s7 = Integer.toString(b);
s8 = Integer.toString(v);
txtfield.setText(s1+s8 + " : " + s2+s7
+ " : " + s3+s6 + " : " + s4+s5);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
}
}
}
Please anyone help me with the solution of this problem.
Last edited by BIT0208-Shuvo on Mon Apr 04, 2011 8:13 pm; edited 2 times in total

BIT0208-Shuvo- Global Moderator-RC
- Course(s):
- BIT
Blood Group: B+
Posts: 87
Points: 124
Re: How can I set background Color of JTextField in Java?
Can you please provide the full code? Just this segment may not give details about the problem.
Also, the title is a bit confusing. I didn't realize whether you are discussing about how to set background color in JTextField or you are discussing why you can not set background color in JTextField.
Also, the title is a bit confusing. I didn't realize whether you are discussing about how to set background color in JTextField or you are discussing why you can not set background color in JTextField.
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: How can I set background Color of JTextField in Java?
I think now both the tittle and the code is is self explanatory , isn't it ??
Last edited by BIT0208-Shuvo on Mon Apr 04, 2011 7:43 pm; edited 1 time in total

BIT0208-Shuvo- Global Moderator-RC
- Course(s):
- BIT
Blood Group: B+
Posts: 87
Points: 124
Re: How can I set background Color of JTextField in Java?
This is the output I am seeing, which shows the color?


_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: How can I set background Color of JTextField in Java?
But It's not working with my IDE . Now I'm not understanding the problem

BIT0208-Shuvo- Global Moderator-RC
- Course(s):
- BIT
Blood Group: B+
Posts: 87
Points: 124
Re: How can I set background Color of JTextField in Java?
Just now I've got the point , the problem was in the look and fill which is the system look and fill. I think , the system look and fill in linux doesn't support the background color of the JTextField .
Now the problem is solved.
Thank you Amit vai for helping me and
Now the problem is solved.
Thank you Amit vai for helping me and

BIT0208-Shuvo- Global Moderator-RC
- Course(s):
- BIT
Blood Group: B+
Posts: 87
Points: 124
Re: How can I set background Color of JTextField in Java?
Most welcome
You can add the word solved in your top post in a bold letter so that it may not create confusion. No need to edit the title though :p
BTW, IDE is not exactly an issue unless you change the run configurations manually. IDE just helps you write the code, and things like that. Running.. is based on your compiler and the code.
BTW, IDE is not exactly an issue unless you change the run configurations manually. IDE just helps you write the code, and things like that. Running.. is based on your compiler and the code.
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: How can I set background Color of JTextField in Java?
Good to see that you have figured that out yourself. 
I have an observation that is not related to your problem.
You have declared some objects like:
My problem is with the variable names. They does not tell us what they do.
This should be like you have named the following two:
For sure you start and stop the timer with these two.
Another thing is, swing already defines a Timer class that works with ActionListener. You can use that one to create your timer. Be careful, Java defines more than one Timer classes. Choose the one that is in the swing package.
I have an observation that is not related to your problem.
You have declared some objects like:
- Code:
Container container;
Thread t;
JTextField txtfield;
My problem is with the variable names. They does not tell us what they do.
This should be like you have named the following two:
- Code:
JButton startButton, stopButton;
For sure you start and stop the timer with these two.
Another thing is, swing already defines a Timer class that works with ActionListener. You can use that one to create your timer. Be careful, Java defines more than one Timer classes. Choose the one that is in the swing package.

BIT0102-Mohaimin- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 415
Points: 713
Re: How can I set background Color of JTextField in Java?
Again, Thread.stop() is deprecated. It may do devastating things in your program. There is the deprecation statement of the methods Javadoc:
Dont ask me what all these mean. I dont know really.
You can do these:
Create a boolean flag named threadRunning which should be true when the thread is running, false otherwise.
In the innermost for loop in the run() add this:
And in action listener of the stopButton, make the threadRunning false.
/ *This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
*ThreadDeathexception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
* other threads, potentially resulting in arbitrary behavior. Many
* uses ofstopshould be replaced by code that simply
* modifies some variable to indicate that the target thread should
* stop running. The target thread should check this variable
* regularly, and return from its run method in an orderly fashion
* if the variable indicates that it is to stop running. If the
* target thread waits for long periods (on a condition variable,
* for example), theinterruptmethod should be used to
* interrupt the wait.
* For more information, see
* Why are thread.sTop, Thread.suspend and Thread.resume Deprecated?.
*/
Dont ask me what all these mean. I dont know really.
You can do these:
- Code:
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
t = new Thread(Timer.this);
t.start();
}
});
Create a boolean flag named threadRunning which should be true when the thread is running, false otherwise.
In the innermost for loop in the run() add this:
- Code:
if(!threadRunning){
return;
}
And in action listener of the stopButton, make the threadRunning false.

BIT0102-Mohaimin- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 415
Points: 713
Re: How can I set background Color of JTextField in Java?
BIT0102-Mohaimin wrote:
You have declared some objects like:
- Code:
Container container;
Thread t;
JTextField txtfield;
My problem is with the variable names. They does not tell us what they do.
This should be like you have named the following two:
- Code:
JButton startButton, stopButton;
For sure you start and stop the timer with these two.
You see, there is only one object for each of the two defined types . That's why I've not used the explanatory name of the objects . We use the explanatory name when there is a number of different objects of similar types for making the work more easier while doing some other things with the objects which I've used in the buttons .But thank you to inform me that , I will try to do these everywhere
Though you have tried to give an explanation of deprecation , I've not understand it well . But again thank you for your effort and


BIT0208-Shuvo- Global Moderator-RC
- Course(s):
- BIT
Blood Group: B+
Posts: 87
Points: 124
Re: How can I set background Color of JTextField in Java?
Erm, by "we", I will assume you are talking about your classmates perhaps.
But good coding involves writing understandable variable names even if you use just one variable.
For example,
Does NOT explain why you inserted a text Field, unless we run the code. or go through the whole code line by line and figure it out our self.
But,
At least gives us an idea that it displays time.
remember, we don't write codes for compilers, we code codes for Programmers
The more understandable it is, the more clean code it will be 
But good coding involves writing understandable variable names even if you use just one variable.
For example,
- Code:
JTextField txtfield;
Does NOT explain why you inserted a text Field, unless we run the code. or go through the whole code line by line and figure it out our self.
But,
- Code:
JTextField displayTimeTextField;
At least gives us an idea that it displays time.
remember, we don't write codes for compilers, we code codes for Programmers
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: How can I set background Color of JTextField in Java?
Must it be JavaScript?
A stylesheet works fine, if not.
And then:
input type="text" id="input_box"
A stylesheet works fine, if not.
And then:
input type="text" id="input_box"
adamricky- Pre-Alpha Release

- Course(s):
- N/A
Blood Group: NULL
Posts: 5
Points: 5
Re: How can I set background Color of JTextField in Java?
adamricky wrote:Must it be JavaScript?
A stylesheet works fine, if not.
And then:
input type="text" id="input_box"
No man, its a Java desktop application. Stylesheet has nothing to do here.

BIT0102-Mohaimin- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 415
Points: 713
Re: How can I set background Color of JTextField in Java?
You can get good examples from sites like
roseindia.net
java2s.com
jtextfield you can call the static import of color class to give it.
roseindia.net
java2s.com
jtextfield you can call the static import of color class to give it.
phoniex05- Pre-Alpha Release

- Course(s):
- N/A
Blood Group: NULL
Posts: 3
Points: 3

Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Active Topics!


» Understanding Timestamp Based Protocol
» Hi from Newbie
» Compare an image from a list of images in the sql database....tell which image is matching
» Sending email without email ID
» teach yourself c by herbert schildt pdf
» .NET MVC3: Unable to find the requested .Net Framework Data Provider. It may not be installed.
» SAD Slides: 4.1, 4.2
» QA topic Presentation - Urgent Notice