File selector and Color picker

Source: Internet
Author: User

File Selector

The file selector JFileChooser is a common class for opening and saving files, and is commonly used to construct the following methods:

Construction Method Name Method meaning
JFileChooser () Create a file selector for the user's default directory
The JFileChooser (file currentdirectory) uses the specified file as the path to construct a filename selector
JFileChooser (String currentdirectorypath) creates a file selector using the specified path
Common methods of the file selector JFileChooser class

public void Showsavedialog (Component parent): Pop-up Save File Selector dialog box

public void Showopendialog (Component parent): Popup Open File Selector dialog box

Public file Getselecedfile (): Gets the selected file

Public file[] Getselectedfiles (): Get a list of multiple selected files, you need to set the file selector to multiple selection

Public File getcurrentdirectory (): Gets the current directory

public void SetCurrentDirectory (): Sets the current directory

public void Setdialogtitle (string dialogtitle): Sets the string displayed in the title bar of the file selector window

public void setmultiselectionenabled (Boolean B): Set to select multiple files

Attention:

Filechooser.addchoosablefilefilter (New Filenameextensionfilter ("JPEG picture file", "JPG", "jpeg"));

Filechooser.addchoosablefilefilter (New Filenameextensionfilter ("gif picture file", "GIF", "GIF"));

Filenameextensionfilter is a file filter, only jpeg,gif styles are displayed in this sample


Color Picker

The color picker, which is the Javax.swing.JcolorChooser class, can be used as a separate dialog box or as a component in any container, making it easy to develop an interface that contains a color selector

The JColorChooser is constructed as follows:

Public JColorChooser (): Create a color selector with an initial color of white

Public JColorChooser (Color Initialcolor): Creates a color selector with the specified initial color

Common methods of JColorChooser class

Public Color GetColor (): Gets the current color value of the color picker

public void SetColor (int c): Specifies the color Picker's current color

public void SetColor (int r,nt g,int b): Specifies the color picker's current color (RGB)


code example:

Package Ch10;import Java.awt.borderlayout;import Java.awt.color;import java.awt.event.*;import javax.swing.*;import Javax.swing.filechooser.filenameextensionfilter;public class Colorchoosertest extends JFrame implements     actionlistener{JPanel JP = new JPanel ();     JButton color = new JButton ("Click the color of My Settings label");     JButton open = new JButton ("Click I can open File");     Private jbutton[] Jbuttonarray = new Jbutton[]{color,open};     JLabel JL = new JLabel ("Click on the two buttons above, set the text color here or open the file");     JFileChooser filechooser = new JFileChooser ("g:\\");     Public Colorchoosertest () {for (int i=0;i<jbuttonarray.length;i++) {jp.add (jbuttonarray[i]);     Jbuttonarray[i].addactionlistener (this);     } this.add (Jl,borderlayout.south);     This.add (JP);     Filechooser.removechoosablefilefilter (Filechooser.getchoosablefilefilters () [0]);//Initialize the file selector and delete the original file selector     Filechooser.addchoosablefilefilter (New Filenameextensionfilter ("JPEG picture file", "JPG", "jpeg")); Filechooser.addchoosablefilefilter (New Filenameextensionfilter ("gif picture file", "GIF", "GIF"));     Filenameextensionfilter file filter, this sample can only display jpg,gif style this.settitle ("File and color selection");     This.setbounds (200,200,400,300);     This.setvisible (TRUE);     This.setdefaultcloseoperation (Jframe.exit_on_close); } public void actionperformed (ActionEvent a) {if (A.getsource () ==jbuttonarray[0]) {Color r = Jcolorch     Ooser.showdialog (This, "Choose desired Color", Color.cyan),//Popup Color selection Dialog jl.settext ("The color of the text becomes the color of your choice");     Jl.setforeground (R); } else if (A.getsource () ==jbuttonarray[1]) {filechooser.showopendialog (this);//Popup Open File Dialog Jl.settext ("You choose to play     Open a file selector ");     }} public static void Main (String args[]) {new colorchoosertest (); }}



File selector and Color picker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.