Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Darkzombies

Thread: Help with swing

  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Help with swing

    Since I've recently discovered, swing is extremely complicated. There are like 1000 functions and half the stuff I don't even begin to understand. Example:

    Java Code:
    JFrame frame = new JFrame("Text Adventure");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JTextField label = new JTextField();
    		JTextField label2 = new JTextField();
            frame.getContentPane().add(label);
            frame.getContentPane().add(label2);
            frame.pack();
            frame.setVisible(true);
    only one textfield is there, even though 2 should pop up, do I have to set size and location, or an entirely different function?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help with swing

    Moved from New to Java.

    The API provides descriptions of all methods, Swing or otherwise. But before you get into Swing programming, it's good to go through the tutorials you will find linked from the API of most Swing classes. Your immediate problem is a result of not understanding how layout managers work, and which layout manager is the default for each class.

    Oh, and Java doesn't have functions. Java has methods.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Wow, theres a forum for swing? I never knew, anyways, I think I'm going to get netBeans and study the GUI maker thing. The tutorials on oracle confused me too much.

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Or maybe not, this may take awhile...

    Java Code:
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSeparator1)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addComponent(jScrollPane3)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 205, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(152, 152, 152))))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(208, 208, 208))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addGap(16, 16, 16))
            );

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help with swing

    Quote Originally Posted by Darkzombies View Post
    I think I'm going to get netBeans and study the GUI maker thing. The tutorials on oracle confused me too much.
    And you think the visual designer will be any less confusing?

    You can only make proper use of a visual designer when you can understand the code it generates. And the only way to get there is to first learn to hand-code your GUIs. Indeed, unless you're headed towards a career in Swing UI design, it's hardly worth the trouble of learning what you can and cannot do in the visual designer, and how exactly to do what you can do.

    I recommend going through the introductory parts of the Swing tutorial, then the section on Concurrency in Swing (this is very important to know and understand), and the first 2 or 3 parts of the section on layout managers. The rest you can refer to whenever you find you need more specific information.

    And to learn faster, don't just read; actually do the exercises.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Yay~ Progress!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class GridLayoutDemo extends JFrame{
    	
    	static String input;
    	static JTextField textField1 = new JTextField();
    	
    	public static void main(String[] args){
    		
    		JFrame frame = new JFrame("Test");
    		
    		JButton textButton = new JButton("Enter Command");
    		
    		GridLayout layout = new GridLayout(2, 2);
    		JPanel panel = new JPanel(layout);
    		
    		panel.add(textField1);
    		panel.add(textButton);
    		
    		frame.add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    		
    		textButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    textButtonActionPerformed(evt);
                }
            });
    	}
    	
    	private static void textButtonActionPerformed(ActionEvent evt) {
    		input = textField1.getText();
    		if(input.equalsIgnoreCase("north")){
    			System.out.println("You go north!");
    		}
    	}
    }
    Took me at least 5 hours of tutorialing (Is that a word?) but I got this much down.
    DarrylBurke likes this.

  7. #7
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Geez its hard to organize things.

    I want my GUI to look like:

    Enemy info---------Player info

    ---------Room Info-------------------




    --------------------------------------

    ---------------Input

    ----------Button to analyze input
    ----------and do the command.


    But it ends up looking like:

    enemy info Player Info
    Room info
    input
    button


    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Main{
    	
    	private static boolean RIGHT_TO_LEFT = false;
    	private static boolean shouldFill = true;
    	private static boolean shouldWeightX = true;
    	
    	public static void addCompToPane(Container pane){
    		
    		if(RIGHT_TO_LEFT) {
                pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
    		
    		JTextArea enemyHealth = new JTextArea("Enemy health: ");
    			enemyHealth.setEditable(false);
    		JTextArea playerHealth = new JTextArea("Your health: ");
    			playerHealth.setEditable(false);
    		JTextArea roomInfo = new JTextArea("BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH ");
    			roomInfo.setEditable(false);
    		JTextArea inputSub = new JTextArea("Enter command: ");
    			inputSub.setEditable(false);
    		JTextField input = new JTextField();
    		
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		if(shouldFill){
    		//natural height, maximum width
    		c.fill = GridBagConstraints.HORIZONTAL;
    		}
    		if (shouldWeightX) {
    			c.weightx = 0.5;
    		}
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(enemyHealth, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 1;
    		c.gridy = 0;
    		pane.add(new JTextArea("                "), c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 2;
    		c.gridy = 0;
    		pane.add(playerHealth, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 50;
    		c.weighty = 10;
    		c.gridwidth = 3;
    		c.gridx = 0;
    		c.gridy = 1;
    		pane.add(roomInfo, c);
    		
    	}
    	
    	private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("GridBagLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addCompToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    	
    	public static void main(String[] args){
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				createAndShowGUI();
    			}
    		});
    	}
    }
    I based it on the example given in the GridBagLayout tutorial. (It's not done yet)
    Last edited by Darkzombies; 12-13-2012 at 08:34 AM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help with swing

    GridBagLayout can take some time to get used to. Two important 'gotcha's regarding the GridBagConstraints weightx/y are (1) they determine how extra space is allotted, after allotting all components their preferred width/height and (2) they are not applied to any column/row that does not contain a component unique to that column/row (i.e. with gridwidth/height == 1).

    Oh, and any time GBL can't find enough space for the preferred width/height, it collapses the component directly to the minimum width/height.

    The rest of the fields are fairly intuitive.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Help with swing-ifailatswing.png

    m(_ _)m

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Main{
    	
    	private static boolean RIGHT_TO_LEFT = false;
    	private static boolean shouldFill = true;
    	private static boolean shouldWeightX = true;
    	
    	public static void addCompToPane(Container pane){
    		
    		if(RIGHT_TO_LEFT) {
                pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
    		
    		JTextArea enemyHealth = new JTextArea("Enemy health: ");
    			enemyHealth.setEditable(false);
    		JTextArea playerHealth = new JTextArea("Your health: ");
    			playerHealth.setEditable(false);
    		JTextArea roomInfo = new JTextArea("BLAH");
    			roomInfo.setEditable(false);
    		JTextArea inputSub = new JTextArea("Enter command: ");
    			inputSub.setEditable(false);
    		JTextField input = new JTextField();
    		JButton button = new JButton("Enter command");
    		
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipadx = 50;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(enemyHealth, c);
    		
    		c.ipadx = 50;
    		c.gridx = 2;
    		c.gridy = 0;
    		pane.add(playerHealth, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		c.gridwidth = 3;
    		pane.add(new JSeparator(), c);
    		
    		c.ipady = 75;
    		c.gridwidth = 3;
    		c.gridx = 0;
    		c.gridy = 1;
    		pane.add(roomInfo, c);
    
    		c.ipady = 20;
    		c.ipadx = 20;
    		c.gridx = 0;
    		c.gridy = 2;
    		pane.add(inputSub, c);
    		
    		c.ipadx = 20;
    		c.gridx = 1;
    		c.gridy = 2;
    		pane.add(input, c);
    		
    		c.ipadx = 20;
    		c.gridx = 1;
    		c.gridy = 2;
    		pane.add(button, c);
    		
    	}
    	
    	private static void createAndShowGUI() {
            JFrame frame = new JFrame("GridBagLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            addCompToPane(frame.getContentPane());
    
            frame.pack();
            frame.setVisible(true);
        }
    	
    	public static void main(String[] args){
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				createAndShowGUI();
    			}
    		});
    	}
    }
    I screwed this up. Really bad. It was going fine until the last row, the input.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with swing

    Please re-read Darryl's post above as he told you one major thing that was wrong -- you're still not setting weightx and weighty GridBagConstraints to non-zero values and seem to have either missed it or ignored it.
    Last edited by Fubarable; 12-14-2012 at 07:54 AM.

  11. #11
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Oh, I thought he was saying weightx and weighty only applied extra space or something like that.

    EDIT: Ohhh, I think the problem was that I didn't put c.gridwidth back to 0 xD

    EDIT 2: Ok maybe not, but this fixes it, I'm still confused on what weight does.

    Java Code:
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weighty = 50;
    		c.weightx = 50;
    		c.ipadx = 50;
    		c.ipady = 20;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(enemyHealth, c);
    		
    		c.ipadx = 50;
    		c.gridx = 2;
    		c.gridy = 0;
    		pane.add(playerHealth, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		c.gridwidth = 3;
    		pane.add(new JSeparator(), c);
    		
    		c.ipady = 65;
    		c.gridwidth = 3;
    		c.gridx = 0;
    		c.gridy = 2;
    		pane.add(roomInfo, c);
    		
    		c.gridwidth = 0;
    		c.ipady = 10;
    		c.gridx = 0;
    		c.gridy = 3;
    		pane.add(input, c);
    		
    		c.gridx = 0;
    		c.gridy = 4;
    		pane.add(button, c);
    Last edited by Darkzombies; 12-14-2012 at 07:38 AM.

  12. #12
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help with swing

    Hmm, so okay. How would I find components from a pane in order to, for example, change the text displayed? I know theres a way to set a number for everything, but does that work with GridBagConstraints?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help with swing

    Just maintain a reference to any instance of any class -- not just text components -- on which you need to invoke a method.

    You do know about class and instance fields, don't you?

    I'm still confused on what weight does.
    It takes time -- and practice -- to gain understanding. Read and re-read the API for GBC.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. need some help in Swing
    By skuskusas in forum AWT / Swing
    Replies: 4
    Last Post: 08-29-2012, 07:46 PM
  2. I need help about swing...
    By CANN1 in forum AWT / Swing
    Replies: 2
    Last Post: 03-31-2012, 03:50 PM
  3. Swing?
    By kyle_maddisson in forum New To Java
    Replies: 9
    Last Post: 11-14-2011, 03:54 AM
  4. Swing
    By tmotse in forum AWT / Swing
    Replies: 0
    Last Post: 03-31-2010, 11:18 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 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
  •