Results 1 to 3 of 3
  1. #1
    Forum22 is offline Member
    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0

    Angry Grid layout issue URGENT

    //there is an issue with panel.add and the panel orientation
    // if you would be so kind could you rewrite the code where the issues are
    //the problem may be that they are all in one class or there may be a naming issue which causes the red underneath the "add" in panel.add and the setComponentOrientation section.
    //the issues are from line 27-72 thats where my problem is


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.ComponentOrientation;
    import java.awt.GridLayout;
    import java.awt.PopupMenu;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Arrays;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    
                public class TestFrame extends JFrame {
                public static final long serialVersionUID = 1L;
                public static final Logger LOG = Logger.getLogger(TestFrame.class.getName());
                public static final Logger LOGGER = Logger.getLogger(TestFrame.class.getName());
        public static Object panel;
    
                public static void main(String[] args) {
                SwingUtilities.invokeLater(() -> {
                JFrame frame;
                frame = new TestFrame();
                frame.getContentPane().setBackground(Color.BLACK);
                frame.setTitle("Logged In");
                frame.setSize(200,200);
                JButton jbtOkay = new JButton("Next");
                jbtOkay.setLocation(1000,70);
                jbtOkay.setSize(50,50);
                JPanel newPanel = new JPanel();
                newPanel.add(jbtOkay);
                //issue when changing the size and location on the button on the second window. Button does not change size or location
                
                
                
                //grid layout for 2nd window
                JFrame frame = new JFrame("Grid Layout");
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(300, 400);
                panel.JLabel (new GridLayout(3, 4, 5, 10)); //rows, columns, horizontal gap, virtical gap
                JLabel labell= new JLabel("Hello1");
                JLabel label2= new JLabel("Hello2");
                JLabel label3= new JLabel("Hello3");
                JLabel label4= new JLabel("Hello4");
                JLabel label5= new JLabel("Hello5");
                JLabel label6= new JLabel("Hello6");
                JLabel label7= new JLabel("Hello7");
                JLabel label8= new JLabel("Hello8");
                JLabel label9= new JLabel("Hello9");   
                panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);  //orders which way the labels 1-9 is inputted 
                panel.add(label1);
                panel.add(label2);
                panel.add(label3);
                panel.add(label4);
                panel.add(label5);
                panel.add(label6);
                panel.add(label7);
                panel.add(label8);
                panel.add(label9);
                
                frame.add((PopupMenu) panel);      
                //the issue is trying to get the grid layout panels on the second window which pops up but there are issue when i typed the code. I dont know how to assign the code to the 2nd layout.
    
                
                frame.add(newPanel);
    
    
                
                
                
    
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                
                });
                }
    
                public PassWordDialog passDialog;
    
                public TestFrame() {
                passDialog = new PassWordDialog(this, true);
                passDialog.setVisible(true);
                }
    
                public PassWordDialog getPassDialog() {
                return passDialog;
                }
                public void setPassDialog(PassWordDialog passDialog) {
                this.passDialog = passDialog;
                }
    
                }
    
                @SuppressWarnings("Serial")
                class PassWordDialog extends JDialog {
                public static final Logger LOG = Logger.getLogger(PassWordDialog.class.getName());
                public static final long serialVersionUID = 1L;
    
                public final JLabel jlblUsername = new JLabel("Username");
                public final JLabel jlblPassword = new JLabel("Password");
    
                public final JTextField jtfUsername = new JTextField(15);
                public final JPasswordField jpfPassword = new JPasswordField();
    
                public final JButton jbtOk = new JButton("Login");
                public final JButton jbtCancel = new JButton("Cancel");
                
                
    
                public final JLabel jlblStatus = new JLabel(" ");
    
                PassWordDialog() {
                this(null, true);
                }
    
                PassWordDialog(final JFrame parent, boolean modal) {
                super(parent, modal);
    
                JPanel p3 = new JPanel(new GridLayout(2, 1));
                p3.add(jlblUsername);
                p3.add(jlblPassword);
    
                JPanel p4 = new JPanel(new GridLayout(2, 1));
                p4.add(jtfUsername);
                p4.add(jpfPassword);
    
                JPanel p1 = new JPanel();
                p1.add(p3);
                p1.add(p4);
    
                JPanel p2 = new JPanel();
                p2.add(jbtOk);
                p2.add(jbtCancel);
    
                JPanel p5 = new JPanel(new BorderLayout());
                p5.add(p2, BorderLayout.CENTER);
                p5.add(jlblStatus, BorderLayout.NORTH);
                jlblStatus.setForeground(Color.RED);
                jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);
                
                
                setLayout(new BorderLayout());
                add(p1, BorderLayout.CENTER);
                add(p5, BorderLayout.SOUTH);
                pack();
    
    
                setLocationRelativeTo(null);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
                addWindowListener(new WindowAdapter() {  
                @Override
                public void windowClosing(WindowEvent e) {  
                System.exit(0);  
                }  
                });
    
    
                jbtOk.addActionListener((ActionEvent e) -> {
                if (Arrays.equals("mete".toCharArray(), jpfPassword.getPassword())
                && "mete".equals(jtfUsername.getText())) {
                parent.setVisible(true);
                setVisible(false);
                } else {
                jlblStatus.setText("Invalid username or password");
                }});
                jbtCancel.addActionListener((ActionEvent e) -> {
                setVisible(false);
                parent.dispose();
                System.exit(0);
                });
    
                }
                }
    Last edited by Forum22; 11-12-2016 at 01:46 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Grid layout issue URGENT

    there is an issue
    Please explain.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Grid layout issue URGENT

    First, don't subclass JFrame or anything else unless you are going to override it or add functionality.
    In your case, simply create an instance of JFrame and add one or more instances of JPanel. Use the panels
    to hold the components.

    And you have too much going on in your InvokeLater. Just the invocation of the TestFrame is all that is needed.
    The other stuff can be done in your TestFrame constructor other other methods.

    And please stop posting duplicate threads.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. using grid layout
    By droidus in forum AWT / Swing
    Replies: 8
    Last Post: 07-03-2013, 10:06 PM
  2. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 06:11 PM
  3. Image into Grid layout
    By Jaxn77 in forum AWT / Swing
    Replies: 6
    Last Post: 02-10-2011, 04:02 PM
  4. Image into Grid layout
    By Jaxn77 in forum Java Applets
    Replies: 1
    Last Post: 02-10-2011, 04:18 AM
  5. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:03 PM

Tags for this Thread

Posting Permissions

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