Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to add Gradient colors to JComponents.
Udara Amarasinghe
Ranch Hand
Posts: 109
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi I want to add gradient color to JButton and JPanel. This is my code can you help me to solve this mater.
public class GetName extends JFrame{ JButton getButton=new JButton("Get Name");//I want add gradient colors to this JButton JTextField nameField=new JTextField(); JLayeredPane pane=new JLayeredPane();//I want add gradient colors to this JPanel public GetName(){ nameField.setBounds(10,10,200,20); getButton.setBounds(15,25,100,20); setContentPane(pane); getContentPane().add(nameField,0); getContentPane().add(getButton,0); getButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); } public static void main(String args[]){ GetName t=new GetName(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.show(); } }
pete stein
Bartender
Posts: 1561
posted 14 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
what specifically is not working for you? Tell us this and we'll be much better able to help you. My guess is to add a gradient color background, paint it in the JComponent's paintComponent method. For instance:
import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class GradColor { private static final Color ulColor = new Color(50, 50, 255); private static final Color lrColor = new Color(0, 0, 60); private JTextField nameField = new JTextField(); private JButton getButton = new JButton("Get Name"); JLayeredPane mainPanel = new JLayeredPane() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int x = getWidth(); int y = getHeight(); Graphics2D g2 = (Graphics2D) g; g2.setPaint(new GradientPaint(new Point(0, 0), ulColor, new Point(x, y), lrColor, false)); g2.fillRect(0, 0, x, y); } }; public GradColor() { mainPanel.setPreferredSize(new Dimension(400, 200)); nameField.setBounds(10, 10, 200, 20); getButton.setBounds(10, 40, 100, 20); mainPanel.add(nameField, 0); mainPanel.add(getButton, 0); } public JComponent getMainPanel() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("GradColor"); frame.getContentPane().add(new GradColor().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
button colors, etc.
Calculator GUI app
Why are my JFrames closing?
How is a Graphical object move on the Screen by using JButton.
calling another swing application to run when a button is click from my swing application
More...