Showing posts with label GUI. Show all posts
Showing posts with label GUI. Show all posts

Friday, June 27, 2014

Set focus to a field in window/frame (2): FocusTraversalPolicy

The FocusTraversalPolicy controls the sequence of fields gaining focus when you tapping through the fields and also the field that has the focus when the window/frame is first constructed/activated/visible.

Following is a sample code.

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FocusTest extends JFrame {
    JTextField f1 = null;
    JTextField f2 = null;
    JButton b1 = null;
    JButton b2 = null;
    public FocusTest() {
        getContentPane().setLayout(new GridLayout(3,2));
        JLabel label1 = new JLabel("Input 1: ");
        JLabel label2 = new JLabel("Input 2: ");
        f1 = new JTextField(15);
        f2 = new JTextField(20);
        b1 = new JButton("button 1");
        b2 = new JButton("button 2");
     
        add(label1);
        add(f1);
        add(label2);
        add(f2);
        add(b1);
        add(b2);

        b1.addActionListener(new ButtonListener());
        b2.addActionListener(new ButtonListener());
     
        ArrayList<Component> tp = new ArrayList<Component>();
        tp.add(f1);
        tp.add(f2);
        tp.add(b1);
        tp.add(b2);
     
        setFocusTraversalPolicyProvider(true);
        setFocusTraversalPolicy(new MyTraversalPolicy(tp));
     
        pack();
        setVisible(true);
    }
 
    private class MyTraversalPolicy extends FocusTraversalPolicy {
        ArrayList<Component> components = null;
        public MyTraversalPolicy(ArrayList<Component> list){
            this.components = list;
        }
     
        public Component getComponentAfter(Container focusCycleRoot, Component focused){
            int idx = components.indexOf(focused);
            if (idx >= components.size()-1){
                idx = -1;
            }
            return components.get(idx+1);
        }
     
        public Component getComponentBefore(Container c, Component focused){
            int idx = components.indexOf(focused);
            if (idx <= 0){
                idx = components.size();
            }
            return components.get(idx-1);
        }
     
        public Component getDefaultComponent(Container c){
            return f2;
        }
     
        public Component getFirstComponent(Container c){
            return components.get(0);
        }
     
        //Set the field of focus 
        //when the window is first activated/contructed/visible
        public Component getInitialComponent(Window w){
            return f2;
        }
     
        public Component getLastComponent(Container c){
            return components.get(components.size()-1);
        }
    }
 
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == b1){
                f1.setText("button 1 is pressed");
                f2.setText("");
            } else if (e.getSource() == b2){
                f2.setText("button 2 is pressed");
                f1.setText("");
            }
        }
    }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FocusTest();
            }
        }
        );
    }
}

            Previous <

Reference:

1. Set focus to a field in java swing when a window/JFrame is first activated/constructed (1): requestFocusInWindow

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Thursday, June 26, 2014

Set focus to a field in java swing when a window/JFrame is first activated/constructed/visible (1): requestFocusInWindow

To set focus to a field in a java swing user interface, you can use either the requestFocusInWindow method of java.awt.Component or the java.awt.FocusTraversalPolicy and the setFocusTraversalPolicy method of java.awt.Component.

The requestFocusInWindow method works only after the layout of the component that is going to gain focus in the window/frame is complete, which means it works only after the pack method is called. This can be achieved by either putting the requestFocusInWindow method after the pack method or using the invokeLater method of SwingUtilities to call the requestFocusInWindow method.

Following is a sample code.

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FocusTest extends JFrame {
    public FocusTest() {
        getContentPane().setLayout(new GridLayout(2,2));
        JLabel label1 = new JLabel("Input 1: ");
        JLabel label2 = new JLabel("Input 2: ");
        JTextField f1 = new JTextField(15);
        JTextField f2 = new JTextField(20);
     
        add(label1);
        add(f1);
        add(label2);
        add(f2);
     
        //the following will not work
         //f2.requestFocusInWindow();

        //the following will work
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               f2.requestFocusInWindow();
           }
        });

        pack();
     
        //the following will work
        //f2.requestFocusInWindow();
     
        setVisible(true);
    }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FocusTest();
            }
        }
        );
    }
}

                               > Next

Reference:

1. Set focus to a field in window/frame (2): FocusTraversalPolicy

--------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Monday, January 6, 2014

Use NetBeans IDE to build Swing GUI products

  1. Click "File" in the menu bar, choose "New Project"
  2. In the pop up window, choose "Java" in the Categories and "Java Application" in the Projects, click "Next"
  3. Specify the Name and Location of your new project and click "Finish".
  4. In the Project pane, click on the new project, right click on the "Source Packages", choose "New", and then "Java Package". 
  5. Specify the Name and Location of your new Java package.
  6. right click on your new package, choose "New" and then "JFrame Form".
  7. Specify the Name and Location of your new java class and click "Finish"
  8. Click "Design" for display if it is not chosen. In the Navigator pane, right click your new frame, choose "Set Layout" and choose the layout that you would like to use. The chosen layout is in bold. Netbeans, by default, select "Free Layout" for you. Make sure that you select the layout that you feel comfortable to use.
  9. Drag other components to your frame content pane. Set their locations and other properties by modifying their properties. The Properties pane is usually under the Palette, or a pop up Properties window can be displayed by right clicking on the component and choosing Properties. 
---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.