Results 1 to 13 of 13
  1. #1
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default porblem with components

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    
    public class TextConversion extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	private JRadioButton convertFrom[] = {new JRadioButton("Text", true),
    			                              new JRadioButton("Binary"),
    			                              new JRadioButton("Hexa-Decimal")};
    	private JRadioButton convertTo[] = {new JRadioButton("Text"),
                							new JRadioButton("Binary", true),
                							new JRadioButton("Hexa-Decimal")};
    	private JTextArea jtaFrom = new JTextArea();
    	private JTextArea jtaTo = new JTextArea();
    	private JButton jbtConvert = new JButton("Convert");
    	
    	public TextConversion()
    	{	
    		//frame information
    		setLayout(null);
    		setTitle("Text Conversion");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		setSize(400, 400);
    		setBackground(Color.WHITE);		
    		//add components to the frame
    		for (int i = 0; i < convertFrom.length; i++)
    		{
    			convertFrom[i].setLocation (20, 20 + 5*(i+1));
    			add(convertFrom[i]);
    			convertTo[i].setLocation(190, 20 + 5*(i+1));			
    			add(convertTo[i]);
    		}
    		jtaFrom.setLocation(20, 70);
    		jtaFrom.setSize(150, 200);
    		jtaFrom.setBorder(new LineBorder(Color.BLACK, 1));
    		jtaFrom.setBorder(new TitledBorder("Text To Convert"));
    		add(jtaFrom);		
    		jtaTo.setLocation(190, 70);
    		jtaTo.setSize(150, 200);
    		jtaTo.setBorder(new LineBorder(Color.BLACK, 1));
    		jtaTo.setBorder(new TitledBorder("The Convert Text"));
    		add(jtaTo);
    		jbtConvert.setLocation(160, 220);
    		add(jbtConvert);
    		
    		//create button groups to radio-buttons
    		ButtonGroup convFrom = new ButtonGroup();
    		ButtonGroup convTo = new ButtonGroup();
    		for (int i = 0; i < convertFrom.length; i++)
    		{
    			convFrom.add(convertFrom[i]);
    			convTo.add(convertTo[i]);
    		}		
    		
    		//events
    		jbtConvert.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				String name = "", name2 = "";
    				for (int i = 0; i < convertFrom.length; i++)
    				{
    					if (convertFrom[i].isSelected()) name = convertFrom[i].getText();
    					if (convertTo[i].isSelected()) name2 = convertTo[i].getText();
    				}
    				
    				if (name.equals("Text"))
    				{
    					if (name2.equals("Binary")) jtaTo.setText(Conversion.TxtToBin(jtaFrom.getText()));
    				}
    				else
    				{
    					if (name2.equals("Text")) jtaTo.setText(Conversion.BinToTxt(jtaFrom.getText()));
    				}
    			}
    		});		
    	}
    	
    	public static void main(String[] args)
    	{
    		new TextConversion().setVisible(true);
    	}
    }
    why the components do not appear on the frame?
    look:

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    What components? You need to provide the details of your question as we can't read your mind.

    I also strongly urge you to read the Sun tutorial on how to use layout managers as knowing and using these will save you a lot of worry: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Check how your layout manager is working.
    For one thing he's not being consistent.

  4. #4
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    What components? You need to provide the details of your question as we can't read your mind.

    I also strongly urge you to read the Sun tutorial on how to use layout managers as knowing and using these will save you a lot of worry: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    man, i know how to work with GUI..
    i dont know why now that dont work..

    Quote Originally Posted by Norm View Post
    Check how your layout manager is working.
    For one thing he's not being consistent.
    i dont use a layout manger (setLayout(null)) because i want to add the
    components dynamicly..

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    You are the "layout manager" I'm talking about.
    Compare the settings for the components that show with the settings for those that don't.
    By settings I mean methods that start with set...

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by 0xHexaDecimal View Post
    man, i know how to work with GUI..
    We can debate this point.

    i dont use a layout manger (setLayout(null)) because i want to add the
    components dynamicly..
    Then you need to understand layout managers better, because this is what they excel at. But it's your dollar, do as you please.

  7. #7
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    We can debate this point.
    i do a lot of projects with GUI so enough with that..


    Quote Originally Posted by curmudgeon View Post
    Then you need to understand layout managers better, because this is what they excel at. But it's your dollar, do as you please.
    0__0
    i thought that i explained why i didn't use a layout manger..

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

    Default

    I'd also try to solve this using layout managers. For instance, you can:

    1) Place your JTextArea in the BorderLayout.CENTER position of a JPanel that uses BorderLayout,
    2) Place your JRadioButtons in a JPanel that uses GridLayout(1, 0) -- once row, a variable number of columns, and then place this JPanel in the BorderLayout-using JPanel above, BorderLayout.SOUTH
    3) Your overall application can have a JPanel in the contentPane that uses GridLayout(1, 2) -- one row, two columns, and then place the BorderLayout-using JPanels into this JPanel so you'll have one next to the other
    etc....

    You can read up more on the layout managers here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

    Default

    For example:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class QuickLayoutDemo {
      private static void createAndShowUI() {
        int gap = 10;
        JPanel centerPanel = new JPanel(new GridLayout(1, 2, gap/2, 0));
        
        centerPanel.add(createConvertPane("Left Title"));
        centerPanel.add(createConvertPane("Right Title"));
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(new JButton("Do it!"));
        
        JPanel mainPanel = new JPanel(new BorderLayout(gap, gap));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    
        JFrame frame = new JFrame("QuickLayoutDemo");
        frame.getContentPane().add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      private static JPanel createConvertPane(String title) {
        ButtonGroup btnGroup = new ButtonGroup();
        JRadioButton rBtn1 = new JRadioButton("Button 1");
        JRadioButton rBtn2 = new JRadioButton("Button 2");
        JPanel radioPanel = new JPanel(new GridLayout(1, 0));
        radioPanel.add(rBtn1);
        radioPanel.add(rBtn2);
        btnGroup.add(rBtn1);
        btnGroup.add(rBtn2);
        JTextArea area = new JTextArea(25, 25);
        JPanel convertPane = new JPanel(new BorderLayout());
        convertPane.setBorder(BorderFactory.createTitledBorder(title));
        convertPane.add(new JScrollPane(area), BorderLayout.CENTER);
        convertPane.add(radioPanel, BorderLayout.SOUTH);
        return convertPane;
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  10. #10
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'd also try to solve this using layout managers. For instance, you can:

    1) Place your JTextArea in the BorderLayout.CENTER position of a JPanel that uses BorderLayout,
    2) Place your JRadioButtons in a JPanel that uses GridLayout(1, 0) -- once row, a variable number of columns, and then place this JPanel in the BorderLayout-using JPanel above, BorderLayout.SOUTH
    3) Your overall application can have a JPanel in the contentPane that uses GridLayout(1, 2) -- one row, two columns, and then place the BorderLayout-using JPanels into this JPanel so you'll have one next to the other
    etc....

    You can read up more on the layout managers here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    but i do a project without a layout manger and it worked fine..

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by 0xHexaDecimal View Post
    but i do a project without a layout manger and it worked fine..
    ... but not now. The more complex the layout required the more the layout managers will help you.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Have you read my post? I made changes to your code and got it to work. I asked you to look at your code to see the differences for those components that do show and those that don't. This is a basic debugging skill: Asking the question "Why does this one work and this one not work?" Then start making changes until you find the answer.

  13. #13
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have you read my post? I made changes to your code and got it to work. I asked you to look at your code to see the differences for those components that do show and those that don't. This is a basic debugging skill: Asking the question "Why does this one work and this one not work?" Then start making changes until you find the answer.
    sorry i didn't saw your post..

Similar Threads

  1. Two components in a JScrollPane
    By carderne in forum New To Java
    Replies: 9
    Last Post: 09-10-2009, 07:24 AM
  2. arrangement of components
    By masa in forum AWT / Swing
    Replies: 7
    Last Post: 12-12-2008, 11:17 AM
  3. Porblem Executing Queries in Java
    By ajit.mishra@gs.com in forum New To Java
    Replies: 1
    Last Post: 09-05-2008, 08:31 AM
  4. GUI components not display
    By Eranga in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2008, 03:16 AM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 05:04 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
  •