Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

GUI

Thread view: 
L - 15 Mar 2006 16:11 GMT
Hi, Iam new to this programming life[Basically a biology student] and
learning the GUI stuff for one of my course project work. I did  run
the program with the below code, but was able to open ONLY a window as
you see in first HelloWorldSwing program. i didnot get any text boxes,
button, and scrol panes. Please let me know what i need to do further.
Here is my code,

import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

/**
* @abc
*
*/
public class Assistant extends JPanel {

       private static Assistant m_instance;
       private JTextArea         jTextArea = null;
       private JButton           jButton = null;
       private JList             list = null;
       private JList             list1 = null;
       private JTextPane         jTextPane = null;
       private JScrollPane       jScrollPane = null;
       private JLabel            label       = null;
       private JLabel            label1      = null;
       private JLabel            label2      = null;
       public static Assistant getInstance() {
               if (m_instance == null)
                       m_instance = new Assistant();
               return m_instance;
       }

       /**
        * This method initializes this
        *
        * @return void
        */
       private void initialize() {
               label2 = new JLabel();
               label1 = new JLabel();
               label = new JLabel();
               this.setLayout(null);

               this.setSize(1000, 700);
               label.setBounds(154, 20, 171, 23);
               label.setText("Please enter query terms:");
               label1.setBounds(11, 173, 194, 23);
               label1.setText("Abstracts returned: 0");
               label2.setBounds(11, 426, 196, 23);
               label2.setText("Abstracts: 0");
               this.add(getJTextArea(), null);
               this.add(getList1(), null);
               this.add(getJScrollPane(), null);
               this.add(label, null);
               this.add(label1, null);
               this.add(getJButton(), null);
               this.add(getList(), null);
               this.add(label2, null);
       }

       /**
        * This method resets jList, jList1 and jTextPane
        *
        * @void
        */
       private void reset()
       {
               list.removeAll();
               list1.removeAll();
               label1.setText("Please enter query terms: 0");
               label2.setText("Abstracts returned: 0");

               jTextPane.setText("");
       }

       /**
        * This method set button enable or not
        *
        * @void
        */
       public void setButtonEnable(boolean enable)
       {
               jButton.setEnabled(enable);
       }

       /**
        * This method add Abstracts to list
        *
        * @void
        */
       public void addSequenceAbstracts()
       {
               label1.setText(" Abstracts returned: " +
list.getModel().getSize());
       }

       /**
        * This method returns jTextArea
        *
        * @return javax.swing.JTextArea
        */
       private JTextArea getJTextArea()
       {
               if (jTextArea == null)
               {
                       jTextArea = new JTextArea();
                       jTextArea.setText("This " + "text area " +
"display " + "in any
font");
                       jTextArea.setFont(new Font("Serif",
Font.ITALIC,16));
                       jTextArea.setLineWrap(true);
                       jTextArea.setWrapStyleWord(true);
               }
               return jTextArea;
       }

       /**
        * This method returns list1
        *
        * @return java.swt.List
        */
       private JList getList1()
       {
               if (list1 == null)
               {
                       list1 = new JList();
                       list1.setBounds(12, 453, 182, 201);
               }
               return list1;
       }

       /**
        * This method returns jTextPane
        *
        * @return javax.swing.JTextPane
        */
       private JTextPane getJTextPane() {
               if (jTextPane == null) {
                       jTextPane = new JTextPane();
                       jTextPane.setContentType("text/html");
               }
               return jTextPane;
       }

       /**
        * This method returns jButton
        *
        * @return javax.swing.JButton
        */
       private JButton getJButton()
       {
               if (jButton == null)
               {
                       jButton = new JButton();
                       jButton.setText("Get Abstracts");
                       jButton.setBounds(440, 19, 164, 28);
               }
               return jButton;
       }

       /**
        * This method returns jList
        *
        * @return java.swt.List
        */
       private JList getList()
       {
               if (list == null)
               {
                       list = new JList();
                       list.setBounds(11, 200, 182, 215);
               }
               return list;
       }

       /**
        * This method returns jScrollPane
        *
        * @return javax.swing.JScrollPane
        */
       private JScrollPane getJScrollPane()
       {
               if (jScrollPane == null) {
                       jScrollPane = new JScrollPane();
                       jScrollPane.setBounds(212, 70, 770, 585);
                       jScrollPane.setViewportView(getJTextPane());
               }
               return jScrollPane;
       }

   private static void createAndShowGUI() {
       //Make sure we have nice window decorations.
       JFrame.setDefaultLookAndFeelDecorated(true);

       //Create and set up the window.
       JFrame frame = new JFrame("Assistant");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Add the ubiquitous "Assistant" label.
       JLabel label = new JLabel("Assistant");
       frame.getContentPane().add(label);

       //Display the window.
       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {
       //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }
}

Thanks,
Learner
Oliver Wong - 15 Mar 2006 16:51 GMT
> Hi, Iam new to this programming life[Basically a biology student] and
> learning the GUI stuff for one of my course project work. I did  run
> the program with the below code, but was able to open ONLY a window as
> you see in first HelloWorldSwing program. i didnot get any text boxes,
> button, and scrol panes. Please let me know what i need to do further.
> Here is my code,

[Code snipped except for the relevant parts]
> public class Assistant extends JPanel {
>
[quoted text clipped - 53 lines]
>    }
> }

   You add all the stuff to "this", but display a JFrame called "frame". So
the frame you're showing and the frame you're adding stuff to are different.

   - Oliver
Rhino - 15 Mar 2006 17:06 GMT
>> Hi, Iam new to this programming life[Basically a biology student] and
>> learning the GUI stuff for one of my course project work. I did  run
[quoted text clipped - 65 lines]
> So the frame you're showing and the frame you're adding stuff to are
> different.

Oliver has correctly identified the problem but his explanation is a bit
terse so I'll try to explain the problem a bit more fully.

Your initialize() method is populating a JPanel but it never gets executed;
you never invoke the initialize() method. Even if you had invoked it, you
don't have any code to add the result of the initialize() method, your
JPanel, to the JFrame.

--
Rhino
L - 16 Mar 2006 04:53 GMT
Thanks Oliver and Rhino. I


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.