Results 1 to 3 of 3
  1. #1
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default How to place the GUI components in correct order

    Hi,
    I have done following code, it works but always I see the "search" button comes first and then the "text area". Can you please show me how to get this other way around?

    As you can understand I novice in java, so if you can explain me what actually you did, it will be very helpful.

    Thank you very much.

    Java Code:
    package test;
    
    
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class NewClass2 extends JPanel implements ActionListener {
        protected JButton b1;
        protected JTextField textField;
    
        
        public NewClass2() {     
            b1 = new JButton("Search");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.setMnemonic(KeyEvent.VK_D);
            b1.setActionCommand("disable");
    
            //Listen for actions on buttons 1 and 3.
            b1.addActionListener(this);
           
            b1.setToolTipText("Go");
            
    
            //Add Components to this container, using the default FlowLayout.
            TextDemo();
            add(b1);
            
        }
        
        // This is for Button
        public void actionPerformed(ActionEvent e) {
            if ("disable".equals(e.getActionCommand())) {
                
                b1.setEnabled(true);
                
            } else {
               
                b1.setEnabled(true);
                
            }
        }  
    
        /**
         * Text area
         * @return 
         */
        public void TextDemo() {
        
        textField = new JTextField(20);
        textField.addActionListener(this);
        
         }
        
        public void TextDemoactionPerformed(ActionEvent evt) {
        }
            
        
        /**
         * Create the GUI and show it.  For thread safety, 
         * this method should be invoked from the 
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("Version 1.0");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            NewClass2 newContentPane = new NewClass2();
    
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            frame.setSize(new Dimension(600, 600));
            
            frame.add(new TextDemo());
    
            
            //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(); 
                }
            });
        }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Compiling your posted code:
    Java Code:
    C:\jexp>javac newclass2.java
    newclass2.java:92: cannot find symbol
    symbol  : class TextDemo
    location: class NewClass2
            frame.add(new TextDemo());
                          ^
    1 error
    Java Code:
            // This line sets the contentPane to "newContentPane".
            frame.setContentPane(newContentPane);
            ...
            // This line tries to add a new class
            // (that must extend Component) to the
            // center section of the contentPane.
            frame.add(new TextDemo());
    If you are going to have NewClass2 extend JPanel and to use an instance of it as your contentPane then you could add the textComponent to the panels layout in the class constructor. To fix this with minimal changes to your code try:
    Java Code:
            //Add Components to this container, using the default FlowLayout.
            add(getTextComponent());
            add(b1);
        ...
        /**
         * Text area
         * @return 
         */
        public JTextField getTextComponent() {
            textField = new JTextField(20);
            textField.addActionListener(this);
            return textField;
         }
        
        public void TextDemoactionPerformed(ActionEvent evt) {
        }

  3. #3
    Jesdisciple is offline Member
    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Default

    Don't you get a compile error from that?
    Java Code:
            frame.add(new TextDemo());
    There are a few things wrong with this, but ultimately it just needs to be removed. (TextDemo is called in the NewClass2 constructor.)

    For that matter, you don't need the TextDemo method at all, because you never reuse the logic it contains. So put its contents into the NewClass2 constructor, like so:
    Java Code:
        public NewClass2() {     
            //...
            //Add Components to this container, using the default FlowLayout.
            textField = new JTextField(20);
            textField.addActionListener(this);
            add(b1);
            
        }
    Also, I don't see that the text field is ever added to the panel, so pass it to add - before add(b1) so the components are arranged in that order.
    Java Code:
        public NewClass2() {     
            //...
            //Add Components to this container, using the default FlowLayout.
            textField = new JTextField(20);
            textField.addActionListener(this);
            add(textField);
            add(b1);
            
        }
    I believe that will do what you want.

    EDIT: Woops, just looked and saw the post conflict.
    Last edited by Jesdisciple; 05-05-2008 at 12:55 PM.

Similar Threads

  1. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:21 AM
  2. Problem with applying Sql order by to html table
    By sireesha264 in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 11:20 AM
  3. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 12:21 AM
  4. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:10 AM
  5. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •