Java-swing Getting Started FAQ I__java

Source: Internet
Author: User
Tags gety pack pear

1, frame how to achieve full screen
The first thing to know is the size of the screen window, and then adjust the position and size of the window to the same size as the screen window, OK. Two methods can be implemented.

Ⅰ:
The program code is as follows:
JFrame frame = new JFrame ("Test full Screen JFrame");
Frame. setundecorated (true);
Frame. Getgraphicsconfiguration (). GetDevice (). Setfullscreenwindow (frame);
Frame. setvisible (true);


Ⅱ:
JFrame frame = new JFrame ("Test full Screen JFrame");
Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();
Rectangle bounds = new Rectangle (screensize);
Frame. SetBounds (bounds);
Frame. setvisible (true);

2, swing frame in the Runtime automatic window maximization
Import javax.swing.*;

        Frame frame = new frame ();
        frame.pack ();
        frame.setdefaultcloseoperation (jframe.exit_on_close);
        frame.setextendedstate (Jframe.maximized_both);
        frame.setvisible (true);
3, initial screen centered
Astcenterbootstrap astcenter = new Astcenterbootstrap ("Astcenter system"); 
Astcenter.pack ();  
Dimension local_size = new Dimension; 
Astcenter.setsize (local_size); 
Astcenter.setlocationrelativeto (NULL); 
Astcenter.setvisible (true); 

4, Swing display modal window
public class Subdialog extends JDialog {
Public Subdialog (JFrame f)
{
Super (F,true);
This.setsize (200,300);
This.settitle ("dialog");
This.setlocationrelativeto (NULL);

}
Public Subdialog ()
{

}
}

5, child window to the parent window to pass the value, from the control value
Method One:
Parent-child windows are contacted by a data object that belongs to the parent window, which is saved when the child window initializes the object reference
In the function of the event-handling object of the child window, first the child window control is fetched, and then the parameter of the data object is given
JDialog.fatherData.lostRate = integer.valueof (JDialog.jTextField.getText ());

6, Window control arrangement

7, Pop-up prompts dialog box
Joptionpane

8, the conversion of string in the enumeration, enum. valueof
public enum Strlist {
APPLE,

PEAR,

ORANGE
}

Switch (strlist.valueof (cmd))
{
Case APPLE:
Break
Case PEAR:
Break
Default
Break
}
9, set the Closing window, the process is over
This.setdefaultcloseoperation (Jframe.exit_on_close);

10, menu shortcut key settings and event trigger
Newitem.setaccelerator (Keystroke.getkeystroke (' N ', ctrl_down_mask));
11, open the file and filter
Open File Class JFileChooser
Filters need to be written by themselves, inheriting FileFilter
Package Windowspak;
Import javax.swing.*;
Import Javax.swing.filechooser.FileFilter;
Import java.io.*;

Public final class Capfilefilter extends FileFilter {
Private String extention;
Private String desc;
Public Capfilefilter (String extention, string description)
{
Super ();
This.extention = extention;
This.desc = description;

}
Public Boolean accept (File f)
{
if (null!= f)
{
if (F.isdirectory ())
return true;
String ext = getextention (f);
if (null!= f && this.extention.equals (EXT))
{
return true;
}
}
return false;
}
Public String getdescription ()
{
return THIS.DESC;
}
Private String getextention (File f)
{
if (null!= f)
{
String file_name = F.getname ();
int i = File_name.lastindexof ('. ');
return file_name.substring (i + 1). toLowerCase ();
}
return null;
}
}
12, JTable Data refresh
Method 1:
Refresh display data mainly by setting model
Mode declaration
TableModel Datamodel = new Abstracttablemodel () {
These methods always need are implemented.
public int getColumnCount () {return column_names.length;}
public int GetRowCount () {return data.length;}
Public Object getvalueat (int row, int col) {return data[row][col];}

The default implementations of these methods in
Abstracttablemodel would work, but we can refine them.
Public String getcolumnname (int column) {return column_names[column];}
Public Class getcolumnclass (int c) {return getvalueat (0, C). GetClass ();}
public boolean iscelleditable (int row, int col) {return true;}
public void setValueAt (Object avalue, int row, int column) {
System.out.println ("Setting value to:" + avalue);
Data[row][column] = Avalue;
}
}; /*
Set mode for a table
Application Form
Table_view = new JTable (Datamodel);
Table_view.setautoresizemode (Jtable.auto_resize_off);
*/
Packet_table.setmodel (Datamodel);

Update JTable
Jtable.updateui ();


Method 2:
The table data is refreshed using the following:
Modol.getdatavector (). removeallelements ();
Modol.firetabledatachanged ();

14, GridBagLayout layout 3 lattice two columns
GridBagLayout gridbag=new gridbaglayout ();

Container.setlayout (Gridbag);
This.setlayout (g_layout);
A. Forms table Section
Tab_pane = AddPanel ();
Tab_pane.add ("Packet List", Addjscrollpane ());

Set Form table tab position
Gridbagconstraints gridbagconstraints_a = new java.awt.GridBagConstraints (0, 0, 3, 1, 1, 1, Gridbagconstraints.baseline, Gridbagconstraints.both, New Insets (0, 0, 0, 0), 0, 0);
Gridbagconstraints (Gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);

Gridbag.setconstraints (Tab_pane, gridbagconstraints_a);
Container.add (Tab_pane);
15, JTree node selected events
Add left mouse button select event
Rtp_tree.addmouselistener (New Mouseadapter () {
public void mousepressed (MouseEvent e) {
int selrow = Rtp_tree.getrowforlocation (E.getx (), e.gety ()); Returns the row where the node is located,-1 indicates that the mouse position is not within the displayed cell boundary
TreePath Selpath = rtp_tree.getpathforlocation (E.getx (), e.gety ()); Returns the tree path of the specified node

Boolean condition = true;
Condition = Condition && (selrow!=-1); If selected
Condition = Condition && (E.getbutton () = = 1); Left E.getbutton () = = 1 Right key E.getbutton () = 3
Condition = Condition && (e.getclickcount () = = 1); Click


If you click on the left button
if (condition = = True && (E.getbutton () = 1)) {
object[] nodes = Selpath.getpath ();

Defaultmutabletreenode node = (defaultmutabletreenode) nodes[nodes.length-1];
Joptionpane.showmessagedialog (NULL, node.tostring ());

}
}
});
16, the prompt dialog box uses the Joptionpane class
17, Edit tree JTree, use Defaultcelleditor
public class Captreecelleditorlistener extends Defaultcelleditor {
public JTree edit_tree = null;
Public Captreecelleditorlistener (JTextField TextField)
{
Super (TextField);
This.setclickcounttostart (2);
}
public boolean stopcellediting ()
{
TreePath Tree_path = Edit_tree.getselectionpath ();

String New_value = Getcelleditorvalue (). toString ();
Edit_tree.getmodel (). valueforpathchanged (Tree_path, New_value);
System.out.println ("INFO:" + new_value);
Joptionpane.showmessagedialog (null, new_value);
Fireeditingcanceled ();
return true;
}
public void cancelcellediting () {

TreePath Tree_path = Edit_tree.getselectionpath ();

String New_value = Getcelleditorvalue (). toString ();
Edit_tree.getmodel (). valueforpathchanged (Tree_path, New_value);
System.out.println ("INFO:" + new_value);

Fireeditingcanceled ();
}
}

//
Defaulttreecellrenderer renderer = (defaulttreecellrenderer) rtp_tree.getcellrenderer ();
JTextField TextField = new JTextField ();
Captreecelleditorlistener editor = new Captreecelleditorlistener (TextField);
Editor.edit_tree = Rtp_tree;
Rtp_tree.setcelleditor (editor);
JTable Selected Row Events
public void ValueChanged (Listselectionevent E1) {

if (0 < Datatable.getselectedrow ())
{

int index = (Integer) datatable.getvalueat (Datatable.getselectedrow (), 0); Rows + Columns
Mframe.update_tree (index);
}
}

ListSelectionModel SelectMode = Packet_table.getselectionmodel ();
Captablelistselectionlistener Caplistlistener = new Captablelistselectionlistener (this, packet_table);
Selectmode.addlistselectionlistener (Caplistlistener);
Selectmode.setselectionmode (listselectionmodel.single_selection);

JTree all expand by default
Expand Tree
private void ExpandAll (JTree tree, TreePath Parent, Boolean expand) {
TreeNode node = (TreeNode) parent.getlastpathcomponent ();
if (Node.getchildcount () > 0) {
For (Enumeration E = Node.children (); e.hasmoreelements ();) {
TreeNode n = (TreeNode) e.nextelement ();
TreePath Path = Parent.pathbyaddingchild (n);
ExpandAll (tree, Path, expand);
}
}
if (expand) {
Tree.expandpath (parent);
} else {
Tree.collapsepath (parent);
}
}
JTable set to select only one row at a time
Selectmode.setselectionmode (listselectionmodel.single_selection);


21. Update the node value of the tree
((Defaulttreemodel) Rtp_tree.getmodel ()). Valueforpathchanged (Dst_path, "Destination Mac:" + SELECTED_PACKET.GET_DST _mac ());

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.