Results 1 to 20 of 22
Thread: Text Color
- 05-07-2012, 02:00 AM #1
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Text Color
First of all i wanted to say hello. For some background i am currently a student and taking Java. I am a Comp Sci major starting out. Semester is winding down and i am somewhat bummed for the Java class that i am currently in was only a 7 week course and you cant really pack a lot in there. I am trying to learn as much as i can as to be better prepared for the in depth study and programming in the language. I came across some issues with my assignments and i hope some Java Wizards here can point me in the right direction. I'll post my code on here but i have to warn ya it is pretty choppy ( im learning :-D)
So here is what i am trying to accomplish: Using Swing create 5 radio buttons that change the text color within a text box. I can't figure out how i can actually change the color. I have tried to do so as i create font objects but not working out to well. Can somebody point me out in the right direction?
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.*;
import javax.swing.*;
public class RadioButtonFrame extends JFrame
{
private JTextField textField; // used to display font changes
private Font plainFont; // font for plain text
private Font boldFont; // font for bold text
private Font italicFont; // font for italic text
private Font boldItalicFont; // font for bold and italic text
private JRadioButton blueJRadioButton, redJRadioButton, yellowJRadioButton, pinkJRadioButton, grayJRadioButton; // selects plain text
private JLabel colorBlue = new JLabel ("Your color");
private JLabel colorRed = new JLabel ("Your color");
private JLabel colorYellow = new JLabel ("Your color");
private JLabel colorPink = new JLabel ("Your color");
private JLabel colorGray = new JLabel ("Your color");
private ButtonGroup radioGroup; // buttongroup to hold radio buttons
// RadioButtonFrame constructor adds JRadioButtons to JFrame
public RadioButtonFrame()
{
super( "RadioButton Test" );
setLayout( new FlowLayout() ); // set frame layout
textField = new JTextField(45);
add( textField ); // add textField to JFrame
colorBlue.setForeground(Color.BLUE);
colorRed.setForeground(Color.RED);
colorYellow.setForeground(Color.YELLOW);
colorPink.setForeground(Color.PINK);
colorGray.setForeground(Color.GRAY);
// create radio buttons
blueJRadioButton = new JRadioButton( "Blue", true );
redJRadioButton = new JRadioButton( "Red", false );
yellowJRadioButton = new JRadioButton( "Yellow", false );
pinkJRadioButton = new JRadioButton( "Pink", false );
grayJRadioButton = new JRadioButton( "Gray", false );
add( blueJRadioButton ); // add plain button to JFrame
add( redJRadioButton ); // add bold button to JFrame
add( yellowJRadioButton ); // add italic button to JFrame
add( pinkJRadioButton ); // add bold and italic button
add( grayJRadioButton );
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup(); // create ButtonGroup
radioGroup.add( blueJRadioButton ); // add plain to group
radioGroup.add( redJRadioButton ); // add bold to group
radioGroup.add( yellowJRadioButton ); // add italic to group
radioGroup.add( pinkJRadioButton ); // add bold and italic
radioGroup.add( grayJRadioButton );
// create font objects
plainFont = new Font("Serif", Font.PLAIN, 14 );
boldFont = new Font( "Serif", Font.BOLD, 14 );
italicFont = new Font( "Serif", Font.ITALIC, 14 );
boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 );
textField.setFont( plainFont ); // set initial font to plain
// register events for JRadioButtons
blueJRadioButton.addItemListener( new RadioButtonHandler( plainFont ) );
redJRadioButton.addItemListener(new RadioButtonHandler( boldFont ) );
yellowJRadioButton.addItemListener( new RadioButtonHandler( italicFont ) );
pinkJRadioButton.addItemListener( new RadioButtonHandler( boldItalicFont ) );
grayJRadioButton.addItemListener( new RadioButtonHandler( boldItalicFont ) );
} // end RadioButtonFrame constructor
// private inner class to handle radio button events
private class RadioButtonHandler implements ItemListener
{
private Font font; // font associated with this listener
public RadioButtonHandler( Font f )
{
font = f; // set the font of this listener
} // end constructor RadioButtonHandler
// handle radio button events
public void itemStateChanged( ItemEvent event )
{
textField.setFont( font ); // set font of textField
} // end method itemStateChanged
} // end private inner class RadioButtonHandler
} // end class RadioButtonFrame
- 05-07-2012, 02:51 AM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: Text Color
Hi LostWind,
Try this to see if it is going to work.
Java Code:public void itemStateChanged( ItemEvent event ) { textField.setFont( font ); // set font of textField textField.validate(); }Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 05-07-2012, 04:54 AM #3
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Re: Text Color
I dont think that would change the color of the font displayed based on the selection of JRadioButtons.
in lame terms i was trying to
plainFont.setColor(Color.BLUE)
plainFont = new Font("Serif", Font.PLAIN, 14 );
or something along those lines and have an individual color be assigned to a different font object. Make sense? i dont know if i could do that though? i dont know if im even making sense at this point
- 05-07-2012, 07:56 AM #4
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Re: Text Color
i cleaned it up a bit but i really do feel kinda dumb spending so many hours reading and i still cant figure out how to change the color accordingly, here is the new code:
import java.awt.*;
import javax.swing.*;
public class RadioPlayNew extends JFrame
{
private JTextField textField; // used to display font changes
private Font blueFont, redFont, yellowFont, pinkFont, grayFont;
private JRadioButton blueJRadioButton, redJRadioButton, yellowJRadioButton, pinkJRadioButton, grayJRadioButton;
private ButtonGroup radioGroup; // buttongroup to hold radio buttons
// RadioButtonFrame constructor adds JRadioButtons to JFrame
public RadioPlayNew()
{
super( "RadioButton Test" );
setLayout( new FlowLayout() ); // set frame layout
textField = new JTextField(45);
add( textField ); // add textField to JFrame
// create radio buttons
blueJRadioButton = new JRadioButton( "Blue", true );
redJRadioButton = new JRadioButton( "Red", false );
yellowJRadioButton = new JRadioButton( "Yellow", false );
pinkJRadioButton = new JRadioButton( "Pink", false );
grayJRadioButton = new JRadioButton( "Gray", false );
add( blueJRadioButton );
add( redJRadioButton );
add( yellowJRadioButton );
add( pinkJRadioButton );
add( grayJRadioButton );
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add( blueJRadioButton );
radioGroup.add( redJRadioButton );
radioGroup.add( yellowJRadioButton );
radioGroup.add( pinkJRadioButton );
radioGroup.add( grayJRadioButton );
// create font objects
blueFont = new Font("Blue", Font.PLAIN, 14 );
redFont = new Font( "Red", Font.BOLD, 14 );
yellowFont = new Font( "Yellow", Font.ITALIC, 14 );
pinkFont = new Font( "Pink", Font.BOLD + Font.ITALIC, 14 );
grayFont = new Font ("Gray", Font.PLAIN, 14);
textField.setFont( blueFont );
// register events for JRadioButtons
blueJRadioButton.addItemListener( new RadioButtonHandler( blueFont ) );
redJRadioButton.addItemListener(new RadioButtonHandler( redFont ) );
yellowJRadioButton.addItemListener( new RadioButtonHandler( yellowFont ) );
pinkJRadioButton.addItemListener( new RadioButtonHandler( pinkFont ) );
grayJRadioButton.addItemListener( new RadioButtonHandler( grayFont ) );
} // end RadioButtonFrame constructor
// private inner class to handle radio button events
private class RadioButtonHandler implements ItemListener
{
private Font font; // font associated with this listener
public RadioButtonHandler( Font f )
{
font = f; // set the font of this listener
}
// handle radio button events
public void itemStateChanged( ItemEvent event )
{
textField.setFont( font ); // set font of textField
}
} // end private inner class RadioButtonHandler
} // end class RadioButtonFrame
main class***************************************
import javax.swing.JFrame;
public class RadioPlayNewTest
{
public static void main( String[] args )
{
RadioPlayNew radioButtonF = new RadioPlayNew();
radioButtonF.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
radioButtonF.setSize( 400, 150 ); // set frame size
radioButtonF.setVisible( true ); // display frame
} // end main
} // end class RadioButtonTest
- 05-07-2012, 08:13 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Text Color
Do Java Font instances have a colour?
Just asking because, if not, the buttons' event handler shouldn't be altering the font but something else.
- 05-07-2012, 08:35 AM #6
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
Hello, I am a java programmer of 1 semester. Wrote up a little solution for you...using ActionListeners.
The panel which holds everything:
PHP Code:import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.BoxLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class rbcChangerPanel extends JPanel { //attributes //textfield private JTextField textField = null; //radio buttons private JRadioButton radioButtons [ ] = null; private int numRadioButtons = 3; //radio button container and main container private JPanel rbContainer = new JPanel ( ), mainContainer = new JPanel ( ); //constructor public rbcChangerPanel ( ) { //initialise text field textField = new JTextField ( "LostWind" ); //initialise button array ( give the array a size ) radioButtons = new JRadioButton [ numRadioButtons ]; //define each button radioButtons [ 0 ] = new JRadioButton ( "red!", false ); radioButtons [ 1 ] = new JRadioButton ( "blue!", false ); radioButtons [ 2 ] = new JRadioButton ( "green!", false ); //add the radio buttons to a container for ( int buttons = 0; buttons < numRadioButtons; buttons ++ ) rbContainer.add ( radioButtons [ buttons ] ); //layout the contents of rbContainer using box layout rbContainer.setLayout ( new BoxLayout ( rbContainer, BoxLayout.X_AXIS ) ); //add text field and radio buttons to mainContainer mainContainer.add ( textField ); mainContainer.add ( rbContainer ); //layout things on main Container mainContainer.setLayout ( new BoxLayout ( mainContainer, BoxLayout.Y_AXIS ) ); //add main container to this panel add ( mainContainer ); //add action listener to the radio button for ( int buttons = 0; buttons < numRadioButtons; buttons ++ ) radioButtons [ buttons ].addActionListener ( new radioButtonListener ( ) ); //enable focus on this panel setFocusable ( true ); } //action listener private class radioButtonListener implements ActionListener { public void actionPerformed ( ActionEvent aEvent ) { Object event = aEvent.getSource ( ); if ( event == radioButtons [ 0 ] ) { textField.setBackground ( Color.red ); setOtherSelectionsFalse ( 0 ); } if ( event == radioButtons [ 1 ] ) { textField.setBackground ( Color.green ); setOtherSelectionsFalse ( 1 ); } if ( event == radioButtons [ 2 ] ) { textField.setBackground ( Color.blue ); setOtherSelectionsFalse ( 2 ); } repaint ( ); //repaint this panel } } public void setOtherSelectionsFalse ( int currentRadioButtonIndex ) { for ( int buttons = 0; buttons < numRadioButtons; buttons ++ ) if ( buttons != currentRadioButtonIndex ) radioButtons [ buttons ].setSelected ( false ); } }
The frame on which the panel above is added:
PHP Code:import javax.swing.JFrame; import java.awt.Dimension; public class rbcChangerFrame { public static void main ( String [ ] args ) { //establish panel rbcChangerPanel panel = new rbcChangerPanel ( ); //establish frame JFrame frame = new JFrame ( ); frame.add ( panel ); frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); frame.setPreferredSize ( new Dimension ( 800, 600 ) ); frame.pack ( ); frame.setVisible ( true ); } }Last edited by Bushman; 05-07-2012 at 08:55 AM.
- 05-07-2012, 08:49 AM #7
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
I think what you are trying to accomplish is to change the colour of a text field as a radio button is selected.
My solution constitutes 3 simple aspects:
1.Establish one text field
2.Establish an array of radio buttons
3.Establish an action listener to trigger changes in the text field as each radio button is toggled.
I have condensed your problem into two classes, one which extends a panel and adds the necessary components,
and the next which adds that panel to a frame.
You don't have to declare separate radio buttons, but rather a single array, since they all have a single synonymous function, in this case you want to vary
a text field's appearance in real time.
screens:
default

By bushman21 at 2012-05-06
radio button 0

By bushman21 at 2012-05-06
radio button 1

By bushman21 at 2012-05-06
radio button 2

By bushman21 at 2012-05-06
here is a link to the code should you want to try it:
rbcChanger.rar
- 05-07-2012, 10:02 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: Text Color
Instead of write the entire code for you.... I will instead direct you to the javadocs.
Since JTextField inherits from the Component class it has a setForeground() method.
setForeground(java.awt.Color)
Also has useful methods like setBackground() and such.Last edited by brynpttrsn; 05-07-2012 at 10:05 AM.
- 05-07-2012, 10:27 AM #9
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
Lol if you want to change the text colour instead of the background colour as the code does, just chaneg setBackground tp setForeground in code as suggested by brunpttsn above.
I'm sorry if typing out the entire code is problematic to you..I am fast typer.
- 05-07-2012, 11:04 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: Text Color
No its not problematic in that sense.... The usual procedure is to guide the poster through the problem.
Even if you can write up a program in 5 minutes that does whatever, it's just not protocol to produce an end product for someone. Tends to ruin the learning experience.
I believe the saying is "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime."
- 05-07-2012, 04:11 PM #11
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Re: Text Color
its actually pretty cool to see a different way of accomplishing the task and dissecting how its done. as far as setBackground and setForeground, i could not figure out if i could hardcode it to each radio button and i looked through Javadocs.
Bushman, just have a couple of question, what is the purpose of
repaint ( ); //repaint this panel
and BoxLayouts, what instances are they better used in. We have pretty much just done FlowLayouts, but once again it kinda blows having the class for 7 weeks instead of a full blown semester of Java. come fall :-) in no time i will be helping and teaching people like you guys do
- 05-07-2012, 11:25 PM #12
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
Thanks for understanding LostWind, I was fully ware that you already had an adequate solution, and just wanted to present a new way of accomplishing the end result, comprised of new techniques.
The above triggers the panel to refresh, that is actually repaint itself so that new changes in components may be seen. If we don't repaint, then we wouldn't see the textfield changePHP Code:repaint ( );
colour. IT IS VERY ESSENTIAL THAT YOU REPAINT WHEN USING ACTION LISTENERS.
You are going to see a lot of action listener usage in the future.
The box layout is a simple layout mechanism that allows the programmer to layout a collection of items either horizontally or vertically.
As you can see this is the format:
<your panel>.setLayout ( new BoxLayout ( <your panel>, BoxLayout axis ) );
Suppose I want some buttons to be vertically aligned, all I would have to do is create a JPanel, and add those buttons to that JPanel, then set that panels layout using Box Layout.
What Idid in the example above used two panels; the first to hold the radio buttons (where i applied box layout to that), the second to hold the text field, and the panel that holds the radio buttons (also applyng box layout to that)
The final step was to add that panel to "THIS" panel, which would be he one that is an extension of JPanel component.Last edited by Bushman; 05-08-2012 at 04:00 AM.
- 05-08-2012, 01:54 AM #13
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Re: Text Color
I see. Now is it possible to do something "Inception" style and bury a panel inside a panel. for example creating a panel with box layout and putting in it other panels with different layouts...does that question make sense?
- 05-08-2012, 02:49 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Text Color
I'm not sure about this. What happens when you remove the repaint() call from the radio buttons' handler?IT IS VERY ESSENTIAL THAT YOU REPAINT WHEN USING ACTION LISTENERS.
-----
The slings and arrows that posted code solutions are heir to: begin classes with uppercase letters (RbcChangerPanel, RcChangerFrame) and code that creates or interacts with Swing components should run in the Event Dispatch Thread not the initial thread (main()). The latter point is subtle, but well enough explained on the Initial Threads page of Oracle's Tutorial and illustrated throughout that tutorial's treatment of Swing.
- 05-08-2012, 03:44 AM #15
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
LostWind, its not really about inception. It is rather a way via which you can nicely arrange the elements so they appear organized on the screen buffer.
Lets look at this little image i just put together:

By bushman21 at 2012-05-07
To make things appear neat and sensible, we
A. first create a panel to group and organize our radio buttons(this is the black panel). We do this by:
*adding the radio buttons to the black panel*
*setting the buttons horizontally aligned side by side each otherPHP Code:for ( int buttons = 0; buttons < 3; buttons ++ +) blackPanel.add ( radioButtons [ buttons ] )
The "BoxLayout.X_AXIS" tells java to organize the radio buttons horizontally, side by side in a line. So we finished organizing our radio buttons.PHP Code:blackPanel.setLayout ( new BoxLayout ( ) ( blackPanel, BoxLayout.X_AXIS );
B. Second we create another panel to hold our text field PLUS the black panel of radio buttons. We don't need a panel for the text field, since
it is by itself. We just need to organize the text field and panel of radio buttons together in a uniform way. A nice way is to make the text field occur first,
then the panel of radio buttons directly under that. So here we create a vertical arrangement of text field, and panel of radio buttons.
(This new arrangement takes place on the blue panel)
We do this by:
*adding the text field to the blue panel*
*adding the panel of radio buttons to the blue panel*PHP Code:bluePanel.add (textfield);
*organize the text field and black panel vertically*PHP Code:bluePanel.add(blackPanel);
the "BoxLayout.Y_AXIS" tells java to origanize the text field plus black panel of radio buttons verticallyPHP Code:bluePanel.setLayout ( new BoxLayout ( bluePanel, BoxLayout.Y_AXIS );
C.finnally we add this blue panel to our main panel(the red panel in image)*
PHP Code:redPanel.add ( bluePanel );
now we are done.Last edited by Bushman; 05-08-2012 at 03:59 AM.
- 05-08-2012, 04:38 AM #16
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Re: Text Color
i think that is the most sensible explanation i have really ever gotten with Java. Made sense, clear, and i actually have a pretty clear understanding of what is happening. Will be pretty beneficial for my assignments left in this week of class.
So organization wise. I have to create kind of like a movie calculating program. The layout is like this:
|TextField| |JComboBox| (TextField asks user to make a String selection from ComboBox (list of movies))
|TextFiel| |JComboBox| |CheckBox| (Text field asks user to make an Int selection from the ComboBox (number of tickets) and then check the CheckBox for whether or not its a matinee ticket type of thing which will have to do with calculation of the total price.
|JButton| (click it to calculate cost)
|TextField| (editableTextField(false) will display the total price
so i can pretty much take each row of item and throw it in a panel and have 4 panels, then take those and throw them into a main panel... sweet
- 05-08-2012, 05:56 AM #17
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
Perfection.
You have grasped the box layout/panel system gracefully and quickly. Your welcome
Just a small note.
So now you know your going to have to apply horizontal box layout to the panels of items per row, then apply vertical alignment to the panel which you plaster those 4 panels to, and finally add that panel to the main panel which you extend.Last edited by Bushman; 05-08-2012 at 05:59 AM.
- 05-08-2012, 06:09 AM #18
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
- 05-08-2012, 06:19 AM #19
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
Re: Text Color
good luck..
- 05-08-2012, 04:30 PM #20
Member
- Join Date
- May 2012
- Location
- Maryland
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Different text color in JTextArea
By bigvanilla in forum New To JavaReplies: 2Last Post: 01-29-2011, 02:21 PM -
Changing text color in SWT
By ourimaler in forum SWT / JFaceReplies: 1Last Post: 06-02-2010, 01:08 PM -
text color
By jpito in forum Advanced JavaReplies: 1Last Post: 11-18-2009, 12:33 AM -
color text in standalone???
By hung_tyh in forum New To JavaReplies: 6Last Post: 12-24-2008, 04:29 PM -
Changing the color of text
By Lang in forum New To JavaReplies: 1Last Post: 11-04-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks