JColorchooser
Create a Swing application to demonstrate use of scrollpane to change its color selected using colour chooser.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollPaneColorChooserDemo extends JFrame {
private JTextArea textArea;
private JScrollPane scrollPane;
private JButton colorButton;
public ScrollPaneColorChooserDemo() {
setTitle("ScrollPane Color Chooser Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JTextArea
textArea = new JTextArea(10, 30);
// Create a JScrollPane and add the JTextArea to it
scrollPane = new JScrollPane(textArea);
// Create a JButton for choosing color
colorButton = new JButton("Choose Color");
colorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color chosenColor = JColorChooser.showDialog(ScrollPaneColorChooserDemo.this, "Choose Background Color", scrollPane.getBackground());
if (chosenColor != null) {
scrollPane.setBackground(chosenColor);
}
}
});
// Add components to the content pane
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(colorButton, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ScrollPaneColorChooserDemo().setVisible(true);
}
});
}
}
Comments
Post a Comment