How to prevent JPanel losing focus ?

  • Follow


I have a JPanel with many JTextFields, JButtons ...
I want to disable the  JPanel losing focus (like switching to another
tab , if the panel is on a tabbedPane; or a user clicking on a
JMenuItem) until a particular condition occurs.
What is the best way to acomplish this ?

0
Reply bobjan (7) 11/8/2005 9:17:04 AM

"bobjan" <bobjan@eunet.yu> wrote in message 
news:1131441424.063678.76400@o13g2000cwo.googlegroups.com...
>I have a JPanel with many JTextFields, JButtons ...
> I want to disable the  JPanel losing focus (like switching to another
> tab , if the panel is on a tabbedPane; or a user clicking on a
> JMenuItem) until a particular condition occurs.
> What is the best way to acomplish this ?

this might be one way
click the toggleButton - 'In progress'
click again - 'Finished'

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
{
  boolean conditionsMet = true;
  public Testing()
  {
    setSize(200,200);
    setLocation(300,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final JTabbedPane tp = new JTabbedPane();
    JPanel p = new JPanel();
    final JToggleButton btn = new JToggleButton("In Progress/Finished");
    p.add(btn);
    tp.addTab("Tab 1",p);
    tp.addTab("Tab 2",new JPanel());
    JMenu menu = new JMenu("Tabs");
    JMenuItem menuItem1 = new JMenuItem("Tab 1");
    JMenuItem menuItem2 = new JMenuItem("Tab 2");
    menu.add(menuItem1);
    menu.add(menuItem2);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    setJMenuBar(menuBar);
    getContentPane().add(tp);
    menuItem1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        if(conditionsMet) tp.setSelectedIndex(0);}});
    menuItem2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        if(conditionsMet) tp.setSelectedIndex(1);}});
    btn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        tp.setEnabled(!btn.isSelected());
        conditionsMet = !btn.isSelected();}});
  }
  public static void main(String[] args){new Testing().setVisible(true);}
}



0
Reply Michael 11/8/2005 10:23:16 AM


Thanks for a try, but this is not what I am looking for.
I want the behaviour to be within the panel.
Your example is a nice enable/disable demo, but is not for a large
application nor is in accordance with OO paradigm.

0
Reply bobjan 11/8/2005 11:39:29 AM

On 8 Nov 2005 01:17:04 -0800, "bobjan" <bobjan@eunet.yu> wrote, quoted
or indirectly quoted someone who said :

>JPanel losing focus (like switching to another
>tab 

There is an event for the JPanel losing focus.  See
http://mindprod.com/jgloss/event11.html

In your handler you could drag focus back to the erroneous field or
pop up a modal dialog to complain of premature abandonment.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply Roedy 11/8/2005 1:49:11 PM

Roedy Green wrote:
> On 8 Nov 2005 01:17:04 -0800, "bobjan" <bobjan@eunet.yu> wrote, quoted
> or indirectly quoted someone who said :
> 
> 
>>JPanel losing focus (like switching to another
>>tab 
> 
> 
> There is an event for the JPanel losing focus.  See
> http://mindprod.com/jgloss/event11.html
> 
> In your handler you could drag focus back to the erroneous field or
> pop up a modal dialog to complain of premature abandonment.

Or you could disable everything else so it can't lose focus.

-- 

Knute Johnson
email s/nospam/knute/
0
Reply Knute 11/8/2005 5:01:35 PM

On Tue, 08 Nov 2005 09:01:35 -0800, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>> In your handler you could drag focus back to the erroneous field or
>> pop up a modal dialog to complain of premature abandonment.
>
>Or you could disable everything else so it can't lose focus.

Instead of a JPanel you could  build on a modal JDialog to keep focus
from wandering.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply Roedy 11/9/2005 6:19:41 AM

5 Replies
267 Views

(page loaded in 0.605 seconds)

Similiar Articles:













7/23/2012 4:10:03 AM


Reply: