Results 1 to 14 of 14
  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Adding tabs from classes

    Hi

    I'm new here and I started to learn Java last week.
    I am trying to make a converter for temperature and distance.
    The classes are Main, Converter, Temperature and Distance.

    In Main, I make a new Converter() and a frame and call to the functions of Converter to do the configuration (title, size, ...).
    In Converter, I make a TabbedPane and a Temperature() and a Distance().
    In Temperature and Distance I make a JPanel with the content.
    Then in Converter, I do this:

    Java Code:
    TabbedPane.addTab("Temperature", Temperature.PTemperature);
    TabbedPane.addTab("Distance", Distance.PDistance);
    But when I run the application, it doesn't show the content, only the tabs.

    If you need any more code, just ask, I hope you can help.


    Thanks in advance.

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

    Default

    You'll likely have to show more code I think. It appears that you're using static objects here, and this is something you should strive to avoid.

  3. #3
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Ok, thank you for your message.

    Main.java

    Java Code:
    import javax.swing.JFrame;
    
    public class Main {
    	/**
    	 * @param args
    	 */
    
    	public static void main(String[] args) {
    		Converter Converter = new Converter();
    		Converter.Window = new JFrame();
    		Converter.Title = "Converter";
    		Converter.ConfigWindow();
    	}
    }
    Converter.java

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    
    
    public class Converter {
    	JFrame Window;
    	String Title;
    	JTabbedPane TabbedPane = new JTabbedPane();
    	Temperature Temperature = new Temperature();
    	Distance Distance = new Distance();
    	
    	public void ConfigWindow() {
    		Window.setTitle(Title);
    		Window.setSize(500,500);
    		TabbedPane.addTab("Temperature", Temperature.PTemperature);
    		TabbedPane.addTab("Distance", Distance.PDistance);
    		Window.setContentPane(TabbedPane);
    		Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Window.setVisible(true);
    	}
    }
    Pieces of Temperature.java and Distance.java.

    Java Code:
    	JPanel PTemperature = new JPanel();
    	GroupLayout Layout = new GroupLayout(PTemperature);
    Java Code:
    	JPanel PDistance = new JPanel();
    	GroupLayout Layout = new GroupLayout(PDistance);

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

    Default

    Do you have to do this in a GUI? Is that part of the requirement of this project? If so, I'd recommend drastic changes here, but my preference given your stage of Java development is that this be a much more simple console program. And again, you appear to be using PDistance and PTemperature JPanels in a static way? Is this so, and if so, why? You also appear to be using NetBeans to do the layouts for you, which I don't recommend at your stage of Java development as it will teach bad habits.

  5. #5
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    How would I do this otherwise, in a command prompt?
    A GUI is easier to use, no?

    Wich drastic changes do you recommend?
    And why can't the panels be static?

    I first used Netbeans but now I use Eclipse, I had problems with the layout so i made one with the netbeans GUI editor and used that "template" in eclipse.
    I find it really hard to get the layouts nice.
    Last edited by bubbless; 03-07-2009 at 10:05 PM.

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

    Default

    To properly do a GUI is much more difficult than a simple command line app.

    To do layouts well, you would benefit by reading the Sun Swing tutorial - layout manager section. It would tell you most of what you need to know. I'd only add that I disagree with the tutorials recommendations to use GridBagLayout as it is difficult to learn. You're far better off nesting JPanels and using the simpler layouts in these nested panels.

    What are the precise requirements of the assignment?

  7. #7
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Thanks.
    I've read the layout manager section already but I still found it difficult to get them the way I want.

    It's not really an issignment.
    It's my first application and I assumed that the major part of the java applications are written with a GUI.
    I've never used a command line application, maybe that's the reason.

    I've learned php for about a year so I know the basics about programming (quite similar syntax) and I wanted to make something that looked like a "program", it's hard to explain.

    Do you have any recommandations about the parts I posted?
    Should I start over again?

    I assume that a command line application only uses the print function to show things?
    If so, I don't see the point of it because I already know the basics.
    (I don't want say I know everything already, I just don't see the point.)

    In my opinion, it's more interesting to write a GUI.
    If you disagree, please say so.

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

    Default

    I've read the layout manager section already but I still found it difficult to get them the way I want.
    How are you trying to set up the GUI? Can you post a picture of the desired layout?

    In my opinion, it's more interesting to write a GUI.
    If you disagree, please say so.
    Oh, I agree, but it significantly increases the learning curve because not only do you have to learn Java syntax, but you also have to learn the intricacies of GUI coding.

    I've learned php for about a year so I know the basics about programming (quite similar syntax) and I wanted to make something that looked like a "program", it's hard to explain.
    Coming from a PHP background means that you will have to unlearn some habits in order to do proper Java coding. I'm not that familiar with PHP but I believe that as a scripting language, it's a little looser with type checking. I know that OOP capabilities are now part of PHP, and I'm not sure how much of this you are using, but they are a cornerstone of Java programming (and thus one of my reasons for recommending against use of static type unless absolutely necessary).

    I assume that a command line application only uses the print function to show things?
    If so, I don't see the point of it because I already know the basics.
    But do you know the basics of OOPs, the use of Java interfaces, the use and structure of generic types, design patterns such as the observer pattern and the MVC pattern, etc...? I see these things as more important than GUI programming.

    Do you have any recommandations about the parts I posted?
    It may seem like overkill, but I try to put a little MVC in any GUI if at all possible. The sooner you learn how to do this, the better off you'll be. You may be using Swing now, but in the future, your GUI may be something completely different, and so if you learn to separate out the logic portion of your program from the GUI, you will learn to create programs that are more flexible, robust, and potentially longer-lived than otherwise.

    Other recommendations:
    1) Go through the Sun Swing tutorials for the best overall education in Swing programming. It is pretty well written and has lots of decent sample code.
    2) Specifically, I would again recommend against use of static variables unless you know why it should be static in a particular place. Using non-static classes will make your code more reusable and flexible.
    3) I would avoid making your class fields public and allow other classes to directly manipulate them, but instead strive to keep them private. Instead have your constructors do more, have your classes hold public methods that allow outside classes to interact with the class but in a limited and controlled way.
    4) Gear your Swing code towards creating JPanels more than JFrames or JApplets. JPanels can be placed anywhere -- into JFrames, JApplets, JDialogs, JOptionPanes, other JPanels,... -- and so creating them from the start will improve the flexibility of your Swing coding. You don't have a class that overrides JFrame, and that's a good thing (there's usually no need for that, despite the sample codes that show this).
    5) Do all Swing coding on the event dispatch thread or EDT. This is Swing's main thread that handles GUI painting and user interaction (i.e., keyboard and mouse). You would do this initially by calling your GUI creation code from within something like so (see the tutorials for details)
    Java Code:
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
         Converter converter = new Converter();
    
         // assuming Converter and XxxConverter objects have the public methods below
         converter.addToTabbedPane(new DistanceConverter().getPanel());
         converter.addToTabbedPane(new TemperatureConverter().getPanel());
    
         // assuming that Converter has a getPanel() method that returns a JPanel
         JPanel converterPanel = converter.getPanel();
         converterPanel.setPreferredSize(new Dimension(500, 500));
    
         JFrame frame = new JFrame("Conversion Program");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPanel().add(converterPanel);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
      }
    });
    edit: I'm at work and do not have access to a Java compiler. Above code may have mistakes.
    Last edited by Fubarable; 03-07-2009 at 11:48 PM.

  9. #9
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Thanks alot, this really helps.
    I do have one more question.
    What do you mean when you say I'll probably won't use Swing so much.
    For example, if someone needs to write an application wich let you store an adress book on your computer.
    Is there any other way then a GUI?

    About the learning process.
    I think I'm starting to get to know OOP, exept why some things should be private.
    With the basics, I mean variables (types, declaring, using), if, else if, else, loops (for, while, foreach).
    Most because these things are almost exactly thesame as in php.

    About the layout, it's a quite simple layout but I can't seem to get it right.
    I'm not worried about that, I'll have a look at some of the layout tutorials and see if that will help.

    The idea is getting to know the basics and then move on to J2EE because it looks like that's a requirement for a java developper to find a job.
    (According to the job descriptions)

    I can't post the layout directly because I only have 4 posts so remove the space.
    http: //img17.imageshack.us/my.php?image=layoutm.png

    I really appreciate your help.



    Edit.

    I still can't get it to work.

    Main.java

    Java Code:
    import javax.swing.JFrame;
    
    public class Main {
    	/**
    	 * @param args
    	 */
    
    	public static void main(String[] args) {
    		Converter Converter = new Converter();
    		Converter.Window = new JFrame();
    		Converter.Title = "Converter";
    		Converter.ConfigWindow();
    	}
    }
    Converter.java

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    
    
    public class Converter {
    	JFrame Window;
    	String Title;
    	JTabbedPane TabbedPane = new JTabbedPane();
    	Temperature Temperature = new Temperature();
    	Distance Distance = new Distance();
    	
    	public void ConfigWindow() {
    		Window.setTitle(Title);
    		Window.setSize(500,500);
    		TabbedPane.addTab("Temperature", Temperature.GetPanel());
    		TabbedPane.addTab("Distance", Distance.GetPanel());
    		Window.setContentPane(TabbedPane);
    		Window.setLocationRelativeTo(null);
    		Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Window.setVisible(true);
    	}
    }
    Pieces of Temperature.java (does the same as in Distance.java)

    Java Code:
    	JPanel PTemperature = new JPanel();
    	GroupLayout Layout = new GroupLayout(PTemperature);
    
    	public JPanel GetPanel() {
    		return PTemperature;
    	}
    Last edited by bubbless; 03-08-2009 at 01:01 AM.

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

    Default

    What do you mean when you say I'll probably won't use Swing so much.
    I didn't say that actually. I said you may not be using it.

    What if Sun for example decides that it doesn't want to expend resources to support and update Swing? It would then age, and possibly fall out of favor, and you will likely use something else for a GUI, perhaps JavaFX for your killer rich internet app. Or what if Swing is completely upgraded into Swing2, and you wanted your new app to use the same logic but new fancy widgets?

    If you keep your logic separate from the GUI, then you'll have an easier time porting your code to these platforms. Also, by keeping things separate, you make it much easier to debug or enhance your current GUI app.

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

    Default

    I still can't get it to work.
    If your problem is that the JTabbedPanes don't show the panels, I'd make sure that the panels are in fact being created correctly. I'd create one of your Temp panels and try to place it into a JOptionPane or in a JFrame and see if it can be seen. In other words try to isolate the problem.

  12. #12
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    I've changed Converter.java to this and the new tab displays the label.

    Converter.java

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    
    public class Converter {
    	JFrame Window;
    	String Title;
    	JTabbedPane TabbedPane = new JTabbedPane();
    	Temperature Temperature = new Temperature();
    	Distance Distance = new Distance();
    	JPanel TestPanel = new JPanel();
    	JLabel TestLabel = new JLabel("Testlabel");
    	
    	public void ConfigWindow() {
    		Window.setTitle(Title);
    		Window.setSize(500,500);
    		TabbedPane.addTab("Temperature", Temperature.GetPanel());
    		TabbedPane.addTab("Distance", Distance.GetPanel());
    		TestPanel.add(TestLabel);
    		TabbedPane.addTab("Test", TestPanel);
    		Window.setContentPane(TabbedPane);
    		Window.setLocationRelativeTo(null);
    		Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Window.setVisible(true);
    	}
    }

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

    Default

    One way to layout your sub-apps would be to place the JLabels in a JPanel that uses a GridLayout(0, 1, 0, vGap) where the first 0 and 1 represent variable number of rows (the 0) and 1 column. The second 2 numbers would be ints for the horizontal gap and the vertical gap, the latter you'd have to play with to set right. You'd place the JTextFields in a similarly set up JPanel with the exact same vertical gap.

    Likewise, the buttons could be placed into their own JPanel using a GridLayout(1, 0, hGap, 0): one row, variable columns, horizontal gap to be determined by playing with numbers, and 0 vertical gap.

    Then create a JPanel that uses BorderLayout(x, y) where x is its horizontal gap int and y the vertical gap. Place the label panel into this BorderLayout.WEST, the jtextfield panel at BorderLayout.CENTER, and finally the buttonPanel BorderLayout.SOUTH.

    Finally, if this panel is to go into a larger tabbed pane, I'd place the panel above into another JPanel that uses FlowLayout(FlowLayout.LEFT) or FlowLayout(FlowLayout.CENTER). This would prevent the BorderLayout using panel from expanding too much (if this is not desired).

    The key is to play with various layouts til you find a mixture that suits your purpose.

  14. #14
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    So you think it's more a layout problem?
    Maybe the sub apps aren't created correctly, however I first had only temperature and it was in Converter.java and it worked but when I wanted to add a distance converter I thought it would be more ordened when they where in different classes.

    Do you need Temperature.java?

    Edit.

    I've tested some things and when I use public static void main(String[] args) in Temperature.java to create a JFrame and I add the panel PTemperature and show the JFrame, it works.
    But it doesn't work when I try the same in Converter.java.

    Edit 2.

    I used the same with Converter.java and it also worked so I used the same in Main and it worked.
    Then I tried the tabs and they also worked so I deleted Converter.java.
    It now works, thank you.
    Last edited by bubbless; 03-08-2009 at 02:56 AM.

Similar Threads

  1. setSelectedIndex (How to move between tabs)
    By lolshooter in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:15 PM
  2. JMenu with Tabs using swing
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 03-03-2009, 04:29 PM
  3. Tabs Won't Work In Right Aligned JPanel
    By davidedwardgill in forum AWT / Swing
    Replies: 0
    Last Post: 08-08-2008, 08:45 PM
  4. Adding Tabs to JTabbedPane
    By blackstone in forum AWT / Swing
    Replies: 2
    Last Post: 11-14-2007, 04:15 PM
  5. resize tabs in jtabbedpane
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 04:02 AM

Posting Permissions

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