Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Beginner java programmer!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Beginner java programmer!

    Hello, I am taking a class in Java programming right now, I am not looking for the answers for my code, but I am looking for a little nudge in the right direction. Here is my code, and I will ask the questions near the end.
    package synch;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Synch extends JFrame {
     
     
        private JLabel targetLabel = new JLabel("Target");
        private JLabel sourceLabel = new JLabel("Label");
        private JButton browseButton = new JButton("Browse");
        private JButton copyButton = new JButton("Copy!");
        private JTextField textField = new JTextField(" ");
        private JTextField textField1 = new JTextField(" ");
     
        private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About;
     
     
        public Synch(){
            JFrame prog = new JFrame("Chris's file synchronization");
            prog.setSize(500,300);
            prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JMenuBar jmb = new JMenuBar();
            prog.setJMenuBar(jmb);
            JMenu fileMenu = new JMenu("File");
            jmb.add(fileMenu);
            fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch "));
            fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch "));
            fileMenu.add(Exit = new JMenuItem("Exit"));
     
            JMenu helpMenu = new JMenu("Help");
            jmb.add(helpMenu);
            helpMenu.add(Help = new JMenuItem("Help"));
            helpMenu.add(Help = new JMenuItem("About"));        
     
     
            prog.setLayout(new GridLayout(4,1)); 
     
            JPanel Pane = new JPanel();                 
            Pane.add(targetLabel, FlowLayout.LEFT); 
            prog.add(Pane , new GridLayout(1,1));      
     
            Pane.add(textField, FlowLayout.CENTER);     
            prog.add(Pane, new GridLayout(1,1));        
            textField.setPreferredSize(new Dimension(200,30));      
     
            Pane.add(browseButton, FlowLayout.RIGHT);   
            prog.add(Pane, new GridLayout(1,1));       
     
     
            Pane.add(sourceLabel, FlowLayout.LEFT);     
            prog.add(Pane , new GridLayout(2,1));       //puts it on the 2nd column which it does not do??
     
            Pane.add(textField, FlowLayout.CENTER);
            prog.add(Pane, new GridLayout(2,1));
            textField.setPreferredSize(new Dimension(200,30));
     
            Pane.add(browseButton, FlowLayout.RIGHT);
            prog.add(Pane, new GridLayout(2,1));
     
            prog.setVisible(true);       
     
            }
     
     
    }
    What I am trying to accomplish is a File Synchronization program. But here is my on going problem for now, is where I left a comment. I want the program itself to be in GridLayout, but I want to use the FlowLayout to flow my buttons, labels, and texts inside the program. But when I want to get to the next line, I'd think GridLayout(2,1) would put it there, but it put it on the top line again. So if any guidance on getting it to the next line is greatly appreciated! Or if there is any thing anyone can see that looks like shabby code, I'd love to hear your responses.

    Thank you!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Beginner java programmer!

    You can use multiple panels each with their own layout manager. What I do is have one main panel. Then add as many subpanels as needed. You can go as deep as you like with how many panels you use.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java programmer!

    My teacher did specify the possibility of doing that. For my very first program, I'd like to make it as simple as possible.

    Do you think the 4'1 grid layout, then flowing what I want in each column would be the wrong way to go about things? It sounds simple, but I just can't figure out how to get to 2'1 instead of 1'1 using the code I specified.

    Thank you for the advice though!

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Beginner java programmer!

    If you use a GridLayout there is now "flowing" in each cell. Unless you do as I say and add a JPanel with FlowLayout in the grid. Otherwise whatever you add to the grid at that location will occupy as much space as allowed. You might need to investigate other layout managers to find one that does what you want.

  5. The Following User Says Thank You to Junky For This Useful Post:

    chrisivey1980 (April 20th, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java programmer!

    Do you mind showing an example of adding a JPanel with the FlowLayout using a few lines of code I used? It'd be a great help to send me in the right direction. Thank you very much!

  7. #6
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Beginner java programmer!

    your code is like puzzle.

    You are creating Pane one time and adding the same pane to different locations.
    Same with your other components.

    Dear if your are creating one object of any component , you can add it to only one location.

    and in your case.When you add a component at different location , your component will be add to the last location where you added your component.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  8. The Following User Says Thank You to DanBrown For This Useful Post:

    chrisivey1980 (April 21st, 2011)

  9. #7
    Junior Member
    Join Date
    Apr 2011
    Location
    Pune, India
    Posts
    11
    My Mood
    Cool
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Beginner java programmer!

    Hi,

    Very first thing, when you are a biginner; don't try to complete your code in brief.
    Properly declare the variable.
    find the below code.

    package com.java.main;
     
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Synch extends JFrame {
     
    	private JLabel targetLabel = new JLabel("Target");
    	private JLabel sourceLabel = new JLabel("Source");
    	private JButton browseButton = new JButton("Browse");
    	private JButton browseButton1 = new JButton("Browse");
    	private JButton copyButton = new JButton("Copy!");
    	private JTextField textField = new JTextField(" ");
    	private JTextField textField1 = new JTextField(" ");
     
    	private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About;
     
    	public Synch() {
    		JFrame prog = new JFrame("Chris's file synchronization");
    		prog.setSize(500, 500);
    		prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JMenuBar jmb = new JMenuBar();
    		prog.setJMenuBar(jmb);
    		JMenu fileMenu = new JMenu("File");
    		jmb.add(fileMenu);
    		fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch "));
    		fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch "));
    		fileMenu.add(Exit = new JMenuItem("Exit"));
     
    		JMenu helpMenu = new JMenu("Help");
    		jmb.add(helpMenu);
    		helpMenu.add(Help = new JMenuItem("Help"));
    		helpMenu.add(Help = new JMenuItem("About"));
     
    		GridLayout gdl = new GridLayout(2, 0);
     
    		prog.setLayout(gdl);
     
    		JPanel pane1 = new JPanel();
    		FlowLayout fl1 = new FlowLayout();
    		pane1.setLayout(fl1);
    		pane1.add(targetLabel, fl1);
    		pane1.add(textField, fl1);
    		textField.setPreferredSize(new Dimension(100, 30));
    		pane1.add(browseButton, fl1);
     
    		prog.add(pane1);
     
    		JPanel pane2 = new JPanel();
    		FlowLayout fl2 = new FlowLayout();
    		pane2.setLayout(fl2);
    		pane2.add(sourceLabel, fl2.LEFT);
    		pane2.add(textField1, fl2.CENTER);
    		textField1.setPreferredSize(new Dimension(100, 30));
    		pane2.add(browseButton1, fl2.RIGHT);
     
    		prog.add(pane2);
     
    		prog.setVisible(true);
     
    	}
     
    }
    Hardik Jadhav

  10. The Following User Says Thank You to hardikjadhav For This Useful Post:

    chrisivey1980 (April 21st, 2011)

  11. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java programmer!

    I took the advice of what you guys said, how does this look? Any other advice? I finally got it how I want it to be. But I am also having one last problem on getting the file chooser to actually fit in the full grid section that is 3'1.

    Am I doing it wrong?



    This is my sync class.

    package synch;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Synch extends JFrame {
     
     
        private JLabel targetLabel = new JLabel("Source");
        private JLabel sourceLabel = new JLabel("Target");
        private JButton browseButton = new JButton("Browse");
        private JButton browseButton1 = new JButton("Browse");
        private JButton copyButton = new JButton("Copy!");
        private JTextField textField = new JTextField(" ");
        private JTextField textField1 = new JTextField(" ");
        private JFileChooser fc = new JFileChooser();
     
        private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About, HelpMe;
     
     
        public Synch(){
            JFrame prog = new JFrame("Chris's file synchronization");
            prog.setSize(500,300);
            prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JMenuBar jmb = new JMenuBar();
            prog.setJMenuBar(jmb);
            JMenu fileMenu = new JMenu("File");
            jmb.add(fileMenu);
            fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch "));
            fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch "));
            fileMenu.add(Exit = new JMenuItem("Exit"));
     
            JMenu helpMenu = new JMenu("Help");
            jmb.add(helpMenu);
            helpMenu.add(HelpMe = new JMenuItem("Help"));
            helpMenu.add(About = new JMenuItem("About"));        
     
     
            prog.setLayout(new GridLayout(4,1));        //declares the program to be in a 4,1 grid
     
            JPanel Pane = new JPanel();                 //creates the JPanel
            Pane.add(targetLabel, FlowLayout.LEFT);     //adds the target label on the left side
            prog.add(Pane , new GridLayout(1,1));       //puts it in the 1,1 section of the grid
     
            Pane.add(textField, FlowLayout.CENTER);     //adds the text field in the center of the FlowLayout
            prog.add(Pane, new GridLayout(1,1));        //puts it in 1,1 layout again
            textField.setPreferredSize(new Dimension(200,30));      // sets the size of the text field
     
            Pane.add(browseButton, FlowLayout.RIGHT);   // makes the browse button the right side of the flow
            prog.add(Pane, new GridLayout(1,1));        // puts it in 1,1
     
     
     
     
            JPanel Pane2 = new JPanel();                // START OF PANE 2 WOOT!
            Pane2.add(sourceLabel, FlowLayout.LEFT);     //puts it in Flow
            prog.add(Pane2 , new GridLayout(2,1));       //puts it on the 2nd column which it does not do??
     
            Pane2.add(textField1, FlowLayout.CENTER);
            textField1.setPreferredSize(new Dimension(200,30));
            prog.add(Pane2 , new GridLayout(2,1));
     
            Pane2.add(browseButton1, FlowLayout.RIGHT);
            prog.add(Pane2 , new GridLayout(2,1));
     
     
     
            JPanel Pane3 = new JPanel();
            Pane3.add(fc, FlowLayout.LEFT);                //THIS IS WHERE I NEED HELP :(
            prog.add(Pane3, new GridLayout(3,1));
     
     
     
            JPanel Pane4 = new JPanel();
            Pane4.add(copyButton, FlowLayout.LEFT);
            prog.add(Pane4, new GridLayout(4,1));
     
     
            Exit.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   exitMethod();
               }
            });
     
             oneWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   oneWayMethod();
               }
            });       
     
            twoWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   twoWayMethod();
               }
            });
     
             HelpMe.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   helpMethod(); 
                }
                });
     
             About.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   aboutMethod();             
                }
                });           
     
             browseButton.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   browseButtonMethod();             
                }
                }); 
     
             browseButton1.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   browseButton1Method();             
                }
                }); 
     
     
             copyButton.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   copyMethod();             
                }
                });    
     
            prog.setVisible(true); //Display the window to the user!        
     
        } //end of method Synch()
     
            public void exitMethod() {
            if(JOptionPane.showConfirmDialog(null,"Are you sure you want it to close?", "Confirm Exit", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
                System.exit(0);    
            }
     
     
            public void oneWayMethod() {
            JOptionPane.showMessageDialog(null, "One way sync has been activated.");
     
     
            }
            public void twoWayMethod() {
            JOptionPane.showMessageDialog(null, "Two way sync has been activated.");
     
            } 
     
            public void aboutMethod() {
            JOptionPane.showMessageDialog(null, "This is a file synchronization program, to speed things up, and over all make it easier for you.");
                   }
     
            public void helpMethod() {
            JOptionPane.showMessageDialog(null, "If you are needing assistance please contact me via email: egamespaypal@gmail.com or by phone 1-901-619-9413.");
     
     
            }
     
            public void browseButtonMethod() {
            JOptionPane.showMessageDialog(null, "The browse button has been clicked!");
     
     
            }
     
     
            public void browseButton1Method() {
            JOptionPane.showMessageDialog(null, "The browse button has been clicked!");
     
     
            }
     
            public void copyMethod() {
            JOptionPane.showMessageDialog(null, "Copying has commenced, watch the magic happen!");
     
     
            }           
    }


    And here is my main.

     
    package synch;
     
     
    import javax.swing.JFrame;
    import java.awt.*;
     
    public class Main {
     
     
     
     
    public static void main(String[] args) {        
     
            try{
     
                Synch myProg = new Synch();
     
     
            }
     
            catch(Exception Ex){
     
     
        }
     
     
     
        }
     
    }

  12. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java programmer!

    bump please, any help?

Similar Threads

  1. Java programmer for coursework help
    By dcshoecousa in forum Paid Java Projects
    Replies: 3
    Last Post: January 6th, 2012, 09:35 AM
  2. Beginner programmer... no idea how to implement this properly
    By rkrajnov in forum Java Theory & Questions
    Replies: 1
    Last Post: January 31st, 2011, 08:02 AM
  3. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  4. Hello everybody from armenian java programmer
    By planmaster in forum Member Introductions
    Replies: 0
    Last Post: April 12th, 2010, 03:01 PM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM