Results 1 to 3 of 3

Thread: a layout question (swing classes) --> (I' m learning ...)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Belgium
    Posts
    98

    a layout question (swing classes) --> (I' m learning ...)

    Hi,

    I can' t understand how to handle the layouts with the swing classes.

    I need to be able to put different objects (JButton, JLabel, ...) on different locations on an interface, and I can' t understand how it is done.

    I have I code example that I understand. Can somebody give me a better version where the layout has changed.

    It looks lik this:

    BUTTON
    BUTTON
    TEXTFIELD

    I would like to have:

    BUTTON LABEL TEXTFIELD
    BUTTON
    BUTTON
    BUTTON
    BUTTON

    Thanks for looking at my problem ....

    this is the code that I have:

    Code:
    //v 1.3
    import javax.swing.*;          
    import java.awt.*;
    import java.awt.event.*;
    
    public class SwingApplication {
    
            public Component createOtherComponents() {
            	
    		final JTextField textField = new JTextField(10);
    		
            JButton Addbutton = new JButton("ADD");
            JButton DeleteButton = new JButton("DELETE");
            
            
            
            Addbutton.setMnemonic(KeyEvent.VK_I);
            Addbutton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
    
                    textField.setText(textField.getText() + "test" + "\n");
    
                }
                
     
            });
            
            DeleteButton.setMnemonic(KeyEvent.VK_I);
            DeleteButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
    
                    textField.setText("");
    
                }
                
     
            });
    
    
            JPanel pane = new JPanel();
    
            pane.setBorder(BorderFactory.createEtchedBorder() //right
                                            );
            pane.setLayout(new GridLayout(0, 1));
            pane.add(Addbutton);
            pane.add(DeleteButton);
            pane.add(textField);
    
    
            return pane;
        }
        
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(
                    UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception e) {}
    
            JFrame frame = new JFrame("TestApplication");
            SwingApplication app = new SwingApplication();
    
            Component Othercontents = app.createOtherComponents();
            
    		
            frame.getContentPane().add(Othercontents);
            
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
        
        
    }

  2. #2
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Angry

    goto the sun tutorial first
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It really matters how you want the components laid out. Panel uses the FlowLayout as it's default layout manager. Container uses the BorderLayout for it's default manger. You would probably want to use the GridLayout Manager for what you want to do.

    Instead of doing this.
    frame.getContentPane().add(Othercontents);

    You could do.

    JFrame jf = new JFrame();
    Container c = jf.getContentPane;
    c.setLayoutManager(new GridLayout(rows,cols));

    You might have some component sizing issues because if
    i am not mistaken components laid out with a GridLayout
    fill the entire area of their encompassing cell. Also when
    the frame is resized the components will stretch with the frame
    so i would set jf.setResizable(false);

Posting Permissions

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



Click Here to Expand Forum to Full Width