Results 1 to 7 of 7
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default setBounds not working (no errors; runs and compiles fine)

    Java Code:
    import javax.swing.JFormattedTextField;
    import javax.swing.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
       public class Risk extends JPanel {
       public int leftNumber, rightNumber, setbad;
       public JButton Enter;
       public JFormattedTextField stupid, yummy;
       public JLabel smart, dumb, nook, shook;
       public String nos, yos, setnice;
       public NumberFormat yucker = NumberFormat.getNumberInstance();
       public NumberFormat nucker = NumberFormat.getNumberInstance();
    
       public Risk() {
          setbad = 1;
          smart = new JLabel("First Number:");
          add(smart);
          stupid = new JFormattedTextField(yucker);
          stupid.setColumns(3);
          stupid.setText("0");
          add(stupid);
          boolean isDigit;
          dumb = new JLabel("Second Number:");
          add(dumb);
          yummy = new JFormattedTextField(nucker);
          yummy.setColumns(3);
          yummy.setText("0");
          add(yummy);
          Enter = new JButton("Enter");
          // enter.addActionListener(new ActionListener());
          add(Enter);
          Enter.addActionListener(new ButtonListener());
    	
    	
       }
    
       class ButtonListener implements ActionListener {
          ButtonListener() {
          }
    
          public void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Enter")) {
    		if (setbad == 1) {
    			shook = new JLabel("0");
    			nook = new JLabel("0");
    			add(shook);
    			add(nook);
    				shook.setBounds(0,15,310,40);
    				nook.setBounds(0,50,310,100);
              	String nos = stupid.getText();
    	  	String yos = yummy.getText();
    	   	shook.setText("First Editable Number: " + nos);
    	   	nook.setText("Second Editable Number: " + yos);
    		setbad ++;
    	revalidate();
    }
    else {
    	  String nos = stupid.getText();
    	  String yos = yummy.getText();
    	   shook.setText("First Editable Number: " + nos);
    	   nook.setText("Second Editable Number: " + yos);
    	revalidate();
    	}
    	
    	
    	  
             }
          }
       }
    }
    Program compiles and runs fine. setBounds ain't working. Please help!
    Thanks,
    cc11rocks

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

    Default

    setBounds works if the container uses no layout manager (the "null" layout), and since your class extends JPanel, it will use the default layout manager for JPanel, a FlowLayout. So this is why setBounds is not working. The short solution to this problem is to set the layout of your class to null, but this is also in the long run, the wrong solution. A much better solution is to not use setBounds at all, but rather to learn about and use the layout managers (except avoid GridBaglayout unless necessary). A key to using them is the understanding that you can nest JPanels, each using its own layout manager. So I advise you to check out the layout manager tutorials and study them.

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default More Help?

    Could you give me some pointers? As you may know, I am calling it from "RiskApp.java":
    Java Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class RiskApp {
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Risk");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Risk riskPanel = new Risk();
            frame.getContentPane().add(riskPanel);
            frame.setSize(1000,1000);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }
                    createAndShowGUI();
                }
            });
        }
    }
    Almost all of the examples I am seeing have the Layout things in the "public static void main" args. How do I adapt this into my code? Is it possible to "merge" the two documents (that would make it a lot easier).
    Thanks,
    cc11rocks

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

    Default

    Quote Originally Posted by cc11rocks View Post
    Could you give me some pointers? As you may know, I am calling it from "RiskApp.java":

    The best source for pointers will be the layout tutorial. Please start here: Layout Tutorial

    Please read this first then we can discuss any questions you may have:

  5. #5
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default

    Risk.java:
    Java Code:
    import java.awt.Toolkit;
    import javax.swing.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     //import javax.swing.SwingUtilities;
    
       public class Risk extends JPanel {
       public int leftNumber, rightNumber, setbad;
       public JButton Enter;
       public JFormattedTextField stupid, yummy;
       public JLabel smart, dumb, nook, shook;
       public String nos, yos, setnice;
       public NumberFormat yucker = NumberFormat.getNumberInstance();
       public NumberFormat nucker = NumberFormat.getNumberInstance();
    
       public Risk() {
          setbad = 1;
          smart = new JLabel("First Number:");
          add(smart);
    	      stupid = new JFormattedTextField(yucker);
          stupid.setColumns(3);
          stupid.setText("0");
          add(stupid);
          boolean isDigit;
          dumb = new JLabel("Second Number:");
          add(dumb);
          yummy = new JFormattedTextField(nucker);
          yummy.setColumns(3);
          yummy.setText("0");
          add(yummy);
          Enter = new JButton("Enter");
          // enter.addActionListener(new ActionListener());
          add(Enter);
          Enter.addActionListener(new ButtonListener());
    	
    	
       }
    
       class ButtonListener implements ActionListener {
          ButtonListener() {
          }
    
          public final void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Enter")) {
    		if (setbad == 1) {
    			shook = new JLabel("0");
    			nook = new JLabel("0");
    			add(shook);
    			add(nook);
    				
              	String nos = stupid.getText();
    	  	String yos = yummy.getText();
    	   	shook.setText("First Editable Number: " + nos);
    	   	nook.setText("Second Editable Number: " + yos);
    		setbad ++;
     //Toolkit.getDefaultToolkit().beep(); 
    	revalidate();
    }
    else {
    	  String nos = stupid.getText();
    	  String yos = yummy.getText();
    	   shook.setText("First Editable Number: " + nos);
    	   nook.setText("Second Editable Number: " + yos);
     //Toolkit.getDefaultToolkit().beep(); 
    	revalidate();
    	}
    	
    	
    	  
             }
          }
       }
    }
    RiskApp.java:
    Java Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.SwingUtilities;
    import java.awt.*;
    
    public class RiskApp {
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Risk");
    JPanel panel = new JPanel(new BorderLayout());
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout());
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Risk riskPanel = new Risk();
            frame.getContentPane().add(riskPanel);
            frame.setSize(305,90);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }
                    createAndShowGUI();
                }
            });
        }
    }
    Would you please run this code and see what I want it to look like? It looks like this with or without the GridLayout. When you maximize, I want the components to stay in the same place. That is my main focus. I don't think i need any Layout for this, but I'm not sure.
    Thanks,
    cc11rocks

  6. #6
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    bounds is such a bad idea, imo it should be depracated

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

    Default

    One possible solution is to create all your textfields and labels on GUI creation, and then fill them with data in the ActionListener rather than trying to add components in the ActionListener. For instance you could create JLabels for the text portion of shook and nook (e.g., shookLabel = new JLabel("First Editable Number: ")) and JTextFields that are non-editable (setEditable(false) for the numbers that are currently being put in shook and nook, perhaps called shookField and nookField, and again that you should add these to your GUI at GUI creation, not in the ActionListener. In the ActionListener, you should only change the text held by the non-editable JTextFields, e.g., shookField.setText(stupid.getText()). I would put your data entry JLabels and JTextFields in one JPanel and your data display (the shook and nook stuff) in a second JPanel, and then put both of them in a JPanel that uses GridLayout(0, 1) (for one column variable number of rows). This will put the data entry JPanel right over the data display JPanel.

Similar Threads

  1. Program working fine until I add methods....
    By LadyMonsterFace in forum New To Java
    Replies: 3
    Last Post: 12-04-2010, 04:22 AM
  2. Code runs fine but get compile error
    By BillyB in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 03:18 PM
  3. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 09:35 AM
  4. Replies: 2
    Last Post: 03-16-2009, 08:52 PM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 04:23 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
  •