Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NullLayout and JWindow

Posted on 2002-06-08
12
Medium Priority
?
533 Views
Last Modified: 2013-11-23
Hi, all
I have noticed some problems using JWindow instead fo JFrame.

The problems are three

1. The JTextArea cannot be edited under the JWindow
   Under the JFrame (extending) it works ok.

2. The JList do not display the JScrollPane under both the
   JWindow and JFrame

3. Finally setting the background color with setBackground
   does not seem to work under both JWindow & JFrame.
   Instead I set a JPanel and set the background there
   that works. Why is that ???

The code follows. (Just change the JFame with JWindow to see the problems)

Thank you in advance.

import java.awt.*;
import javax.swing.*;

public class testScrollPane extends JFrame
{
     public testScrollPane()
     {
          setSize( 500, 500 );
          getContentPane().setLayout( null );

          JPanel backPanel = new JPanel();
          backPanel.setBackground( Color.green );
          backPanel.setBounds(30 ,30, 100, 110);
         
          JPanel panel = new JPanel();
          panel.setLayout( new BorderLayout() );
          panel.setBounds(80, 30, 150, 100);
          panel.setBackground( Color.red );
         
          JTextArea area = new JTextArea();          
          area.setEditable( true );
          JScrollPane scrollPane = new JScrollPane();
          scrollPane.getViewport().add( area );
         
          panel.add( new JLabel("TextArea"), BorderLayout.NORTH );
          panel.add( scrollPane, BorderLayout.CENTER );

          JPanel lisPanel = new JPanel();
          lisPanel.setLayout( new FlowLayout() );
          String[] players = {"Somebody_1", "Somebody2"};
          JList watchingPlayersList = new JList(players);                    
          JScrollPane scrollPane2 = new JScrollPane(watchingPlayersList);                    
          lisPanel.add( scrollPane2 );          
          lisPanel.setBounds( 150, 150, 50, 50);

         
          getContentPane().add( panel );
          getContentPane().add( lisPanel );
          getContentPane().add( backPanel );          
          setBackground( Color.cyan );
          show();
     }
     
     public static void main( String args[] )
     {
          new testScrollPane();
     }
}
0
Comment
Question by:pouli
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7064707
 JFrame is a top-level window and it is derived from JWindow (basically they are more or less the same but JWindow lacks a border). JPanel is a generic container that is used to group components inside JFrame (or other JPanel components) that's why you have to set the background colour of the JPanel and add it to the JFrame. In other words the JFrame provides the space in which the application GUI is built and when it is closed the application terminates.

 Hope it helps.
0
 

Author Comment

by:pouli
ID: 7065812
girionis thank you for your interest but I think you missed the problem :-)

The problem is that the JWindow has different functionality than the JFrame. Althgouh in theory they should be the same.

The second problem is although the JFrame has a method setBackground( Color c ) that is supposed to set the background color, it does not work. That is why I thought placing a JPanel and set the background color as a workaround, but I do not think this is a very good technique. There must another way.

If your run the program that I posted you will see the result for the JFrame.

If you change the JFrame with JWindow and rerun it you will notice that there is no caret available the textarea.

The last problem is that the JScrollPane is not shown at the JList component.

Do you have any ideas about these ?

I raise the points because I think it is too much to read for the points I give.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 7066363
Let me help out here.  JWindow and JFrame are not the same.  JFrame extends the functionality of a JWindow and it is the JFrame that most people are refering to when they talk about a visible program (GUI) window.  The class JWindow is ususally used for splash screens.  I believe that it is not intended for a JWindow to be interacted with.  

I have also played around with you code a bit, look below for some changes.  Once you start adding layout managers, panel, and other componenets the color selection becomes dependant upon every setting therein.  Think of color settings at the panel level then.  

import java.awt.*;
import javax.swing.*;

public class testScrollPane extends JFrame
{
    public testScrollPane()
    {
         setSize( 500, 500 );
         this.setLocation(50,50);
         getContentPane().setLayout( new GridLayout(3,0, 10,10) );

         JPanel backPanel = new JPanel();
         backPanel.setBackground( Color.green );
         backPanel.add(new JLabel("I'm just a label..."));
         backPanel.setBorder(BorderFactory.createTitledBorder("Panel back-ground coloring"));
         //backPanel.setBounds(30 ,30, 100, 110);

         JPanel panel = new JPanel();
         panel.setLayout( new BorderLayout(10,10) );
         //panel.setBounds(80, 30, 150, 100);
         panel.setBorder(BorderFactory.createTitledBorder("Has a textField"));
         panel.setBackground( Color.red );

         JTextArea area = new JTextArea();
         area.setEditable( true );
         JScrollPane scrollPane = new JScrollPane();
         scrollPane.getViewport().add( area );

         panel.add( new JLabel("TextArea"), BorderLayout.NORTH );
         panel.add( scrollPane, BorderLayout.CENTER );

         JPanel lisPanel = new JPanel();
         //lisPanel.setLayout(new FlowLayout() );
         //only one object in the panel, don't really need to set layout
         String[] players = {"Somebody_1", "Somebody2","Somebody_3", "Somebody4","Somebody_5", "Somebody6"};
         JList watchingPlayersList = new JList(players);
         watchingPlayersList.setVisibleRowCount(3);
         JScrollPane scrollPane2 = new JScrollPane(watchingPlayersList);
         lisPanel.setBorder(BorderFactory.createTitledBorder("Working with a list"));
         lisPanel.add( scrollPane2 );
         //lisPanel.setBounds( 150, 150, 50, 50);


         getContentPane().add( panel );
         getContentPane().add( lisPanel );
         getContentPane().add( backPanel );
         // set the JFrame's background
         this.setBackground( Color.cyan );
         // a close operation
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         show();
    }

    public static void main( String args[] )
    {
         new testScrollPane();
    }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pouli
ID: 7066773
Hi, tomboshell

I have the need to use NullLayout. My real problem is that that you see on the code is just a sample that I made in order to give you understand where I have the problem.

I need to use NullLayout.

I am afraaid that at the end I will have to use JFrame although I want to get rid of the TaskBar.

Are there any suggestions ?

What about the JList that it does not display the JScrollPane ??

Thank you for the interest.
0
 
LVL 92

Expert Comment

by:objects
ID: 7068601
1. Editting the text area under the JWindow works fine for me. You just need to click in it first, as it does not recieve keyboard focus initially.

2. The problem here is you're adding the scrollpane to panel with a flowloayout. If you change the layout of the parent panel to borderlayout it will work ok.

         JPanel lisPanel = new JPanel();
         lisPanel.setLayout( new BorderLayout() );
         String[] players = {"Somebody_1", "Somebody2"};
         JList watchingPlayersList = new JList(players);                    
         JScrollPane scrollPane2 = new JScrollPane(watchingPlayersList);                    
         lisPanel.add( scrollPane2 );          
         lisPanel.setBounds( 150, 150, 50, 50);

3. JFrame and JWindow simply don't paint they're backgrounds by default. Just override paint.

    public void paint(Graphics g)
    {
         Dimension s = getSize();
         g.setColor(getBackground());
         g.fillRect(0, 0, s.width, s.height);
         super.paint(g);
    }
0
 

Author Comment

by:pouli
ID: 7069566
I am almost sure that the JTextArea is not working at me. I will double check and tell you.

0
 

Author Comment

by:pouli
ID: 7073914
No, it is not working.

Instead though of loosing time trying to find why, I left the board like JWindow and made another JFrame which I placed all the swing components.

Another problem that I faced was with two images. Exactly the same type, and at the same location.

From the same class the 1st was loading to the JLabel but the other (with the different name) could not!

I tried everything and nothing was happening, untill I used the Forte For Java in order to check what will happen there.

I saw that the image that I used having problems with was loading now.

Inside the code FFJ specified the files with the full path on the HDD.

I made the same and worked.

Although I didn't had to do the same for the previous JLabel.

Does anybody knows what are the rules that Java follows here ?

With the same logic the image that now works at me maybe not work at someone's else computer (becasue it is not full path specified).


0
 
LVL 92

Expert Comment

by:objects
ID: 7074235
> No, it is not working.

There's no reason that I'm aware of that it shouldn't.
What version of JDK r u using? It works fine here using 1.3.1.

> Does anybody knows what are the rules that Java follows here ?

If you don't specify the full path, then it is treated as a relative path.
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 7074971
This discusses a problem displaying the caret in a JTextArea in a JWindow, which implies it can be done.

http://developer.java.sun.com/developer/bugParade/bugs/4408683.html

Though the evaluation mentions changes in 1.4 which means a JWindow with a null parent may not work.
0
 

Author Comment

by:pouli
ID: 7076659
I use 1.4 and the JWindow is still not focusable :-(
I read the workaround as well.

Too lengthy to consider. I just left the board with the JWindow and placed the buttons and rest to a JPanel.

It is quite good because the player he can move the checkers on the board while having the "roll dice" button for example selected (Because the JWindow is never focusable). I treat evrything like 1 component so for example if the player moves the JWindow then the JPanel will move accordingly as well.

Thanks for the time.
I thought that I gave you the points. Didn't?


0
 

Author Comment

by:pouli
ID: 7076662
I will do it now then :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 7076732
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question