How can you use file choosers and color choosers in Swing? Explain with suitable example,

Using/ Creating Color Choosers

We can use JColorChooser class to enable users to choose from a palette of colors. A color chooser is a component that we can place anywhere within our program GUI. The JColorChooser API also makes it easy to bring up a model or non-modal dialog that contains a color-chooser. We can create color-chooser using any one of the following constructors:

JColor Chooser();

JColor Chooser(intial_color);

Once an object of JColorChooser class is created, we can display it in a dialog box by calling its show Dialog() method. Signature of this method is as below:

 Color show Dialog(parent, title, intial_color)

Example

import javax.swing.*;

import java.awt.*;

import java.awt.event. *;

class DialogDemo extends JFrame

{

JColorChooser cc;

JPanel p;

JDialog d;

DialogDemo()

{

setSize(700, 500);

setDefault LokAnd FeelDecorated (true);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

setTitle("Paraent Frame");

p-new JPanel();

add (p);

setVisible(true);

cc = new JColorChooser();

Color c=cc.showDialog (this, "Select new color....", Color.white);

p.setBackground (c);

}

public static void main(String args[])

{

DialogDemo frame=new DialogDemo();

}

}


Using/Creating File Choosers
File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. We can add it at any location of our GUL To display a file chooser in modal dialog box we usually use the JFileChooser API. We can create file choosers by using any one of the following constructors:
JFileChooser()
JFile Chooser(file_object)
FileChooser(initial_path)
Once an object of JColorChooser class is created, we can display it in a dialog box by calling its show Dialog() or showOpenDialog() or showSave Dialog() methods. Signature of these methods is as below:
int show Open Dialog(parent)
int showSaveDialog(parent)
int show Dialog(parent,title)

Example
import javax.swing.*;
import java.awt.";
import java.awt.event.*;
class DialogDemo extends JFrame
{
fileDialog;
FileChooser
JPanel p;
DialogDemo ()
{
setSize(700, 500);
setDefaultLookAndFeelDecorated (true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Paraent Frame");
setVisible (true);
fileDialog= new JFileChooser();
fileDialog.showOpenDialog(this);
}
public static void main(String args[])
{
DialogDemo frame=new DialogDemo();
}
}





Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?