Change text color

change text and background colors

Page 1 of 1

3 Replies - 30357 Views - Last Post: 10 March 2008 - 02:31 AM Rate Topic: -----

#1 trying  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-March 08

Change text color

Posted 09 March 2008 - 04:10 PM

Under set colors I can change the Next button color to black
how to change the stock name, etc to a color?
With one command rather than
stockLabel.setForeground(Color.red);
over and over again for the 4 words stock name, volume, closing price, and change
The UIManager.setLookAndFeel doesn't exactly work?

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

public class Stock extends Frame implements ActionListener
{
   //Declare an input variable
	DataInputStream input;

   //Construct components
JPanel fieldPanel = new JPanel();//8-16
JPanel buttonPanel = new JPanel();

JLabel stockLabel = new JLabel("Stock Name:");
JLabel stock = new JLabel("			   ");
JLabel volumeLabel = new JLabel("Volume:");
JLabel volume = new JLabel("			");
JLabel priceLabel = new JLabel("Closing Price");
JLabel price = new JLabel("				  ");
JLabel changeLabel = new JLabel("Change:");
JLabel change = new JLabel("			");

JButton next = new JButton("next  ->");


   public static void main(String[] args)
   {

//set the look and feel of the interface
				try
				{
	   				UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

	   		}
	   		 catch(Exception e)
	   		{
				JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel.","error",JOptionPane.INFORMATION_MESSAGE);
		 }
	  Stock window = new Stock();
	  window.setTitle("Yesterday's 10 Hottest Stocks");
	  window.setSize(300, 175);
	  window.setVisible(true);
   }

   public Stock()
   {
	 //Set colors
stockLabel.setForeground(Color.red);//CHANGES STOCK(HOW TO ALL WITH ONE EASY WAY)
next.setForeground(Color.black);//THIS CHANGES THE NEXT BUTON YEAH!
	 //Set layout managers
setLayout(new BorderLayout());//5
fieldPanel.setLayout(new GridLayout(4,2));//6
buttonPanel.setLayout(new FlowLayout());//7

	 //Add components and actionListener to interface
fieldPanel.add(stockLabel);//8-17
fieldPanel.add(stock);
fieldPanel.add(volumeLabel);
fieldPanel.add(volume);
fieldPanel.add(priceLabel);
fieldPanel.add(price);
fieldPanel.add(changeLabel);
fieldPanel.add(change);

buttonPanel.add(next);

add(fieldPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);

next.addActionListener(this);

	 try
	 {
		//Open the file
		input = new DataInputStream(new FileInputStream("hotStocks.dat"));
	 }
	 catch(IOException ex)
	 {
		closeFile();
	 }

	 //Construct window listener
	 addWindowListener(
		new WindowAdapter()
		   {
			   public void windowClosing(WindowEvent e)
			   {
				  closeFile();
			   }
		   }
	  );
   }

   public void actionPerformed(ActionEvent e)
   {
	  try
	  {
		 //Read the data into the text fields
		 stock.setText(input.readUTF());//8-18
		 volume.setText(input.readUTF());
		 price.setText(input.readUTF());
		 change.setText(input.readUTF());

	  }
	  catch(IOException e2)
	  {
		 //Print End of file message and clear fields
		 stock.setText("End of File");
		 volume.setText("");
		 price.setText("");
		 change.setText("");

	  }
   }

   public void closeFile()
   {
	  try
	  {
		 input.close();
	  }
	  catch(IOException c)
	  {
		 System.exit(1);
	  }
	  System.exit(0);
   }
}


This post has been edited by trying: 09 March 2008 - 05:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change text color

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Change text color

Posted 09 March 2008 - 06:29 PM

A method setForeground(Color color) will change the color of the text in a JTextField, or JLabel.
Was This Post Helpful? 3

#3 trying  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-March 08

Re: Change text color

Posted 09 March 2008 - 07:42 PM

View Postletthecolorsrumble, on 9 Mar, 2008 - 06:29 PM, said:

A method setForeground(Color color) will change the color of the text in a JTextField, or JLabel.


didn't I already do that with this line of code
stockLabel.setForeground(Color.red);//CHANGES STOCK
(HOW TO ALL WITH ONE EASY WAY)

looking to change all text without repeating this for every word.
trying(beginner)
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Change text color

Posted 10 March 2008 - 02:31 AM

You could do that using fieldPanel.setForeground(Color.red);.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1