Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toolbar won't let focus go

Status
Not open for further replies.

kmcculler

Programmer
Jul 5, 2000
81
0
0
US
Hello all,
I've got a prob with an app I'm building. I've got a JToolbar that has a JButton on it that when pressed calls a extension of PAction. This action creates a new JInternalFrame and gives the user some additional info on the item selected allowing for editing of that info, ect.
All this sofar works, except for a minor inconvience. The focus should be on the first component (usually an instance of some JTextComponent), but the toolbar seems determined to keep it.
I first tried going to the new internalframe and using requestfocus() on the first component as the last line in the constuctor, that didn't work. I then tried putting a listener on the internalframe and requesting focus for first component on the internalFrameActivated event(), still no luck. I've also tried setting the component as the next focusable from the toolbar button and transfering to it, but the focus always remains on that button.
I also have a hotkey (keypressed listner) that calls the same action, when it is used (instead of the toolbar button) the focus is gained by the component just as I want it... so it has to be something to do with the toolbar but I can't think of what. (btw. i checked where I create the toolbar and didn't see anything that I wrote that would redirect forcus)
Any help would be appreciated.
Kris McCuller
kmcculler@yahoo.com
 
//yourJInternalFrame.setSelected(true);
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

public class EditorWithInternalFrames
{
  JFrame jframe;
  JInternalFrame jif;
  JDesktopPane desktopPane;

  EditorWithInternalFrames()
  {
    initGui();

  }
  /**
   * Method initGui.
   */
  private void initGui()
  {
    jframe = new JFrame();
    desktopPane = new JDesktopPane();

    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;

    menuBar = new JMenuBar();
    jframe.setJMenuBar(menuBar);

    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

    menuItem = new JMenuItem("Open", KeyEvent.VK_O);

    ActionListener fileOpenActionListener = new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
			  JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File("C://"));
        fc.setFileFilter(new TextFileFilter());
        fc.setAccessory(new PreviewLable(fc));
        int returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
          File file = fc.getSelectedFile();
          jif = createInternalFrame(file);
          desktopPane.add(jif);
          try
                   {
          jif.setSelected(true);
                   }
               catch (java.beans.PropertyVetoException jbpe) {System.out.println("java.beans.PropertyVetoException");}
        }
      }
    };

    jframe.getContentPane().setLayout(new BorderLayout());

    JToolBar toolBar = new JToolBar();
    JButton jey = new JButton(new ImageIcon("images/Home.gif"));
    jey.addActionListener(fileOpenActionListener);

    JButton secondButton = new JButton("test");
    secondButton.addActionListener(fileOpenActionListener);
    toolBar.add(jey);
    toolBar.add(secondButton);

    jframe.getContentPane().add(toolBar, BorderLayout.NORTH);
    jframe.getContentPane().add(desktopPane, BorderLayout.CENTER);

    menuItem.addActionListener(fileOpenActionListener);
    menuItem.setIcon(new ImageIcon("images/Home.gif"));

    menuItem.setAccelerator(
      KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
    menu.add(menuItem);

    menuItem = new JMenuItem("Save");
    menuItem.setMnemonic(KeyEvent.VK_S);
    menu.add(menuItem);
    menuItem = new JMenuItem("Close");

    menuItem.setMnemonic(KeyEvent.VK_C);
    menu.add(menuItem);
    menuItem = new JMenuItem("Exit");
    menuItem.setMnemonic(KeyEvent.VK_E);
    menu.add(menuItem);

    menuItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
            jframe,
            "Wirklich Beenden",
            "hihi",
            JOptionPane.YES_NO_OPTION))
        {
          jframe.dispose();
          System.exit(0);
        }

      }
    });

    menu = new JMenu("More");
    menu.setMnemonic(KeyEvent.VK_N);
    menuBar.add(menu);

    menuItem = new JMenuItem("Hilfe");
    menuItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        JOptionPane.showMessageDialog(jframe, "Jetzt haste verspielt");
        jframe.dispose();
        System.exit(0);
      }
    });
    menu.add(menuItem);

    jframe.setSize(400, 400);
    jframe.setVisible(true);

  }

  public JInternalFrame createInternalFrame(File file)
  {
    JInternalFrame jit = new JInternalFrame(file.getName(), true, true, true);
    JTextArea j = new JTextArea();

    jit.setVisible(true);

    jit.setSize(200, 200);
    try
    {
      j.read(new FileReader(file), null);
    }
    catch (FileNotFoundException e)
    {
      System.out.println(e);
    }
    catch (IOException e)
    {
      System.out.println(e);
    }

    JScrollPane jsp = new JScrollPane(j);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jit.getContentPane().add(jsp);

    return jit;
  }

  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(
        "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
    }
    catch (Exception e)
    {
    }

    EditorWithInternalFrames ifo = new EditorWithInternalFrames();
  }
}

class TextFileFilter extends FileFilter
{
  /**
   * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
   */
  public boolean accept(File f)
  {

    return f.getName().endsWith(".txt")
      || f.getName().endsWith(".java")
      || f.isDirectory();
  }
  /**
   * @see javax.swing.filechooser.FileFilter#getDescription()
   */
  public String getDescription()
  {
    return "Text- und Javadateien";
  }
}

class PreviewLable extends JLabel implements PropertyChangeListener
{
  JFileChooser jfc;

  PreviewLable(JFileChooser fc)
  {
    fc.addPropertyChangeListener(this);
  }
  public void propertyChange(PropertyChangeEvent e)
  {
    String prop = e.getPropertyName();

    if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop))
    {
      File file = (File) e.getNewValue();
      if (file != null)
        setText("" + file.length());
    }
  }
}
copy from a web site and I have added the statement setSelected(true);
 
Thanks for trying but I'm afraid that didn't solve the problem. I need to select the first focusable JComponent in the InternalFrame, not just the frame itself as the above code does.
Selecting the frame itself has never been a problem, it opens and activates as it should coming to the foreground and selecting itself. However, the button on the toolbar remains highlighted as the selected component, not the first text component on the new internal frame.
In other words the compoent that has focus is not in the selected internal frame, and I'm trying to figure out a way to get it there. Thanks again

Kris McCuller
 
//try this code
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

public class EditorWithInternalFrames
{
  JFrame jframe;
  JInternalFrame jif;
  JDesktopPane desktopPane;

  EditorWithInternalFrames()
  {
    initGui();

  }
  /**
   * Method initGui.
   */
  private void initGui()
  {
    jframe = new JFrame();
    desktopPane = new JDesktopPane();

    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;

    menuBar = new JMenuBar();
    jframe.setJMenuBar(menuBar);

    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(menu);

    menuItem = new JMenuItem("Open", KeyEvent.VK_O);

    ActionListener fileOpenActionListener = new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
          jif = createInternalFrame();
          desktopPane.add(jif);
          try
                   {
          jif.setSelected(true);
                   }
               catch (java.beans.PropertyVetoException jbpe) {System.out.println("java.beans.PropertyVetoException");}
      }
    };

    jframe.getContentPane().setLayout(new BorderLayout());

    JToolBar toolBar = new JToolBar();
    JButton jey = new JButton(new ImageIcon("images/Home.gif"));
    jey.addActionListener(fileOpenActionListener);

    JButton secondButton = new JButton("test");
    secondButton.addActionListener(fileOpenActionListener);
    toolBar.add(jey);
    toolBar.add(secondButton);

    jframe.getContentPane().add(toolBar, BorderLayout.NORTH);
    jframe.getContentPane().add(desktopPane, BorderLayout.CENTER);

    menuItem.addActionListener(fileOpenActionListener);
    menuItem.setIcon(new ImageIcon("images/Home.gif"));

    menuItem.setAccelerator(
      KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
    menu.add(menuItem);

    menuItem = new JMenuItem("Save");
    menuItem.setMnemonic(KeyEvent.VK_S);
    menu.add(menuItem);
    menuItem = new JMenuItem("Close");

    menuItem.setMnemonic(KeyEvent.VK_C);
    menu.add(menuItem);
    menuItem = new JMenuItem("Exit");
    menuItem.setMnemonic(KeyEvent.VK_E);
    menu.add(menuItem);

    menuItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
            jframe,
            "Wirklich Beenden",
            "hihi",
            JOptionPane.YES_NO_OPTION))
        {
          jframe.dispose();
          System.exit(0);
        }

      }
    });

    menu = new JMenu("More");
    menu.setMnemonic(KeyEvent.VK_N);
    menuBar.add(menu);

    menuItem = new JMenuItem("Hilfe");
    menuItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        JOptionPane.showMessageDialog(jframe, "Jetzt haste verspielt");
        jframe.dispose();
        System.exit(0);
      }
    });
    menu.add(menuItem);

    jframe.setSize(400, 400);
    jframe.setVisible(true);

  }

  public JInternalFrame createInternalFrame()
  {
    JInternalFrame jit = new MyInternalFrame();
    jit.setVisible(true);

    jit.setSize(200, 200);

    return jit;
  }

  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(
        "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
    }
    catch (Exception e)
    {
    }

    EditorWithInternalFrames ifo = new EditorWithInternalFrames();
  }
}

class TextFileFilter extends FileFilter
{
  /**
   * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
   */
  public boolean accept(File f)
  {

    return f.getName().endsWith(".txt")
      || f.getName().endsWith(".java")
      || f.isDirectory();
  }
  /**
   * @see javax.swing.filechooser.FileFilter#getDescription()
   */
  public String getDescription()
  {
    return "Text- und Javadateien";
  }
}

class PreviewLable extends JLabel implements PropertyChangeListener
{
  JFileChooser jfc;

  PreviewLable(JFileChooser fc)
  {
    fc.addPropertyChangeListener(this);
  }
  public void propertyChange(PropertyChangeEvent e)
  {
    String prop = e.getPropertyName();

    if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop))
    {
      File file = (File) e.getNewValue();
      if (file != null)
        setText("" + file.length());
    }
  }
}
Code:
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.*;

class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
        JTextField myJText;
        JTextField myJText2;
        JTextField myJText3;

    public MyInternalFrame() {
        super("Document #" + (++openFrameCount), 
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        getContentPane().setLayout(new GridLayout(3,2));
        myJText = new JTextField("25.0");
        myJText2 = new JTextField("12.0");
        myJText3 = new JTextField("0.0");

        JLabel myJLab = new JLabel("+");
        JLabel myJLab2 = new JLabel("=");
        JButton processBtn = new JButton("process");
        ActionListener myListener = new ActionListener()
                                    {
                                     public void actionPerformed(ActionEvent e) 
                                            {
                                             myJText3.setText(String.valueOf( Double.parseDouble(myJText.getText())+Double.parseDouble(myJText2.getText()) ));
                                            }

                                    };
        processBtn.addActionListener(myListener);
    
        getContentPane().add(myJText);
        getContentPane().add(myJLab);
        getContentPane().add(myJText2);
        getContentPane().add(myJLab2);
        getContentPane().add(myJText3);
        getContentPane().add(processBtn);
        //...Create the GUI and put it in the window...

        //...Then set the window size or call pack...
        setSize(300,300);

        //Set the window's location.
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top