Results 1 to 16 of 16
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default need help with GUI

    so i'm writing a little app and this is the plan.........radio buttons on left in buttonggroup, some other functionalities around, but in middle of the screen I want to do this. depending on which radio button is selected on left a different 4 checkbox selection will appear on the screen. Now how do i do this. Is layeredpane good idea? card layout? Not sure how to do this best???

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

    Default Re: need help with GUI

    If it will always be 4 check boxes, then I'd not use a CardLayout or LayeredPane but would simply change the AbstactActions associated with each JCheckBox. This will change the check box's title for you.

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

    Default Re: need help with GUI

    ... or you could use a CardLayout if you find that easier. Both could work just fine.

  4. #4
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    cool...tks...seems that cardlayout is working well......

  5. #5
    Ksammie01 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: need help with GUI

    There is a tool online called JGuiD.. it provides you with an interface to drag and drop components onto a frame, arrange them the way you like and then it generates for you the source code, then you can put functionality behind the Gui..it solved all my problems.if you don't find it, you can let me know and i see how i can email it to you.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: need help with GUI

    It's a bad idea to use a code generator to generate code you're incapable of writing yourself. That way lies confusion.

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

  7. #7
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    I'm using NetBeans to help with the gui. You're saying i shouldn't use it DarrylBurke but should write the code myself????

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

    Default Re: need help with GUI

    Quote Originally Posted by Danieldcc View Post
    I'm using NetBeans to help with the gui. You're saying i shouldn't use it DarrylBurke but should write the code myself????
    You'll find many opinions on this. Myself, I'm with Darryl in that I believe that NetBeans code generator is a great prototyping tool, but find it to be problematic for those learning Swing since it hides the nuts and bolts of what's going on underneath, and that this can cause problems if the student wants to push the GUI beyond more than just the basics. To use the NetBeans code generator well, it helps to first know Swing well, and for that reason I recommend most students of Swing to hand code their GUI's, especially when starting out.

  9. #9
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    hhhmmmm....You're right, it'll be better for me to do it myself so I have started. Ok. Question already as I had a similar problem with ex-assignment but had numerous files in it so it would of been too much to ask you guys. Anyways, how do i set the size of the JRadioButton in my code below? When I use setSize it won't change the size of radiobutton.

    Java Code:
    import java.awt.Dimension;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class NoteTakerGUI extends JPanel {
    	
    	public NoteTakerGUI() {
    		initiateGUI();
    	}
    	private void initiateGUI() {
    		this.setLayout(null);
    		JPanel radioButton = new JPanel();
    		radioButton.setLayout(new BoxLayout(radioButton, BoxLayout.PAGE_AXIS));
    		radioButton.setSize(160,600);
    		
    		//radioButton.setPreferredSize(new Dimension(100,100));
    		radioButton.setBorder(BorderFactory.createRaisedBevelBorder());
    		ButtonGroup radButtonGroup = new ButtonGroup();
    		JRadioButton java = new JRadioButton("Java");
    		java.setSize(50,50);
    		JRadioButton url = new JRadioButton("URL");
    		JRadioButton prdctKnow = new JRadioButton("Product Knowledge");
    		JRadioButton pmntInq = new JRadioButton("Payment Inquiry");
    		
    		radButtonGroup.add(java);
    		radButtonGroup.add(url);
    		radButtonGroup.add(prdctKnow);
    		radButtonGroup.add(pmntInq);
    		radioButton.add(java);
    		radioButton.add(url);
    		radioButton.add(prdctKnow);
    		radioButton.add(pmntInq);
    		
    		this.add(radioButton);
    
    	}
    public static void main(String[] args){
    	NoteTakerGUI note = new NoteTakerGUI();
    	JFrame f = new JFrame();
    	f.setSize(700,700);
    	f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
        f.add(note);
    
    }
    }

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

    Default Re: need help with GUI

    I generally avoid setting the size of any component but instead let the component's preferred size and the layout managers do the sizing for me. I also avoid null layout except in some very specific circumstances.

  11. #11
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    hehe, learning a lot here....yeah, I hear about avoiding the null layout but decided to keep it as i want total control of where things go for this app....let's see how it goes. Ok, so forget the size option. Now I added a new JPanel and used a card layout for it.....I created a class for ActionListener and i wanna be able to switch between the cards depending on which jbutton is selected but don't know how to implement that in the actionperformed method. Can you help me with that?

    Java Code:
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    public class NoteTakerGUI extends JPanel {
    	
    	public NoteTakerGUI() {
    		initiateGUI();
    	}
    	private void initiateGUI() {
    		this.setLayout(null);
    		JPanel radioButton = new JPanel();
    		JPanel checkButtons = new JPanel();
    		checkButtons.setLayout(new CardLayout());
    		checkButtons.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		checkButtons.setLocation(170,90);
    		checkButtons.setSize(500,300);
    		JPanel javaCard = new JPanel();
    		JCheckBox javaMessage1 = new JCheckBox("Page Cannot Be Displayed");
    		javaCard.add(javaMessage1);
    		JPanel urlCard = new JPanel();
    		JCheckBox urlMessage1 = new JCheckBox("context error");
    		urlCard.add(urlMessage1);
    		checkButtons.add(javaCard, "Java");
    		checkButtons.add(urlCard, "url");
    		
    		//checkButtons.add(javaCard);
    		JLabel name = new JLabel("Name and Phone Number");
    		name.setLocation(100,5);
    		name.setSize(150,20);
    		JTextField input = new JTextField();
    		input.setLocation(250,5);
    		input.setSize(300,20);
    		String[] data = {"User", "Administrator", "Contact", "Non-Contact"};
    		JList type = new JList(data);
    		type.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		type.setLocation(580,5);
    		type.setSize(80,80);
    
    		
    		radioButton.setLayout(new BoxLayout(radioButton, BoxLayout.PAGE_AXIS));
    		radioButton.setSize(160,300);
    		radioButton.setLocation(5, 75);
    		//radioButton.setBorder(BorderFactory.createRaisedBevelBorder());
    		radioButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		ButtonGroup radButtonGroup = new ButtonGroup();
    		JRadioButton java = new JRadioButton("Java");
    		java.setSize(50,50);
    		java.addActionListener(new MyActionListener());
    		JRadioButton url = new JRadioButton("URL");
    		JRadioButton prdctKnow = new JRadioButton("Product Knowledge");
    		JRadioButton pmntInq = new JRadioButton("Payment Inquiry");
    		
    		radButtonGroup.add(java);
    		radButtonGroup.add(url);
    		radButtonGroup.add(prdctKnow);
    		radButtonGroup.add(pmntInq);
    		radioButton.add(java);
    		radioButton.add(url);
    		radioButton.add(prdctKnow);
    		radioButton.add(pmntInq);
    		
    		
    		this.add(radioButton);
    		this.add(name);
    		this.add(input);
    		this.add(type);
    		this.add(checkButtons);
    		
    	}
    	public class MyActionListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("I was checked");
    		//how do i set this so i can pick which card i want to display in cardLayout????
    	}
    	}
    public static void main(String[] args){
    	NoteTakerGUI note = new NoteTakerGUI();
    	JFrame f = new JFrame();
    	f.setSize(700,700);
    	f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
        f.add(note);
    
    }
    }

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

    Default Re: need help with GUI

    You have to first add the "card" to the CardLayout using JPanel with a String for its name. Then you can show the card in your ActionListener using that same name via CardLayout's show(...) method. Myself, I'd use the same String that is displayed by the JRadioButton as the name for my card, and I would explicitly set the JRadioButton's actionCommand String with this text.

  13. #13
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    cool, that worked, thanks

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

    Default Re: need help with GUI

    Great, glad you've got it working! Since you have succeeded, I'll post up my short example program.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class CardLayoutEg extends JPanel {
       private static final long serialVersionUID = 1L;
       private CardLayout cardLayout = new CardLayout();
       private JPanel cardPanel = new JPanel(cardLayout);
       private JPanel radioBtnPanel = new JPanel(new GridLayout(0, 1));
       private ActionListener radioBtnListener = new RadioBtnListener();
       private ButtonGroup cardRadioBtnGroup = new ButtonGroup();
    
       public CardLayoutEg() {
          radioBtnPanel.setBorder(BorderFactory.createTitledBorder("Select:"));
          cardPanel.setBorder(BorderFactory.createTitledBorder("Card Panel"));
          
          setLayout(new BorderLayout());
          add(cardPanel, BorderLayout.CENTER);
          add(radioBtnPanel, BorderLayout.LINE_START);
          
          String[] exampleStrings = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
          for (String example : exampleStrings) {
             JPanel panel = new JPanel(new GridBagLayout());
             panel.setPreferredSize(new Dimension(400, 300));
             panel.add(new JLabel(example + " Panel"));
             addCard(panel, example);
          }
       }
       
       // public method that adds a component to the CardLayout-using JPanel
       // with a String name constant
       // that also creates a JRadioButton with the name String as its text
       // and its actionCommand String.
       // Adds JRadioButton to a ButtonGroup and a JPanel
       public void addCard(JComponent component, String name) {
          JRadioButton radioBtn = new JRadioButton(name);
          radioBtn.setActionCommand(name);
          radioBtn.addActionListener(radioBtnListener);
          cardRadioBtnGroup.add(radioBtn);
    
          cardPanel.add(component, name);
          radioBtnPanel.add(radioBtn);
       }
       
       private class RadioBtnListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             String name = e.getActionCommand();
             cardLayout.show(cardPanel, name);
          }
       }
    
       private static void createAndShowGui() {
          CardLayoutEg mainPanel = new CardLayoutEg();
    
          JFrame frame = new JFrame("NoteTakerGui2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

  15. #15
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    Quote Originally Posted by Fubarable View Post
    Great, glad you've got it working! Since you have succeeded, I'll post up my short example program.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class CardLayoutEg extends JPanel {
       private static final long serialVersionUID = 1L;
       private CardLayout cardLayout = new CardLayout();
       private JPanel cardPanel = new JPanel(cardLayout);
       private JPanel radioBtnPanel = new JPanel(new GridLayout(0, 1));
       private ActionListener radioBtnListener = new RadioBtnListener();
       private ButtonGroup cardRadioBtnGroup = new ButtonGroup();
    
       public CardLayoutEg() {
          radioBtnPanel.setBorder(BorderFactory.createTitledBorder("Select:"));
          cardPanel.setBorder(BorderFactory.createTitledBorder("Card Panel"));
          
          setLayout(new BorderLayout());
          add(cardPanel, BorderLayout.CENTER);
          add(radioBtnPanel, BorderLayout.LINE_START);
          
          String[] exampleStrings = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
          for (String example : exampleStrings) {
             JPanel panel = new JPanel(new GridBagLayout());
             panel.setPreferredSize(new Dimension(400, 300));
             panel.add(new JLabel(example + " Panel"));
             addCard(panel, example);
          }
       }
       
       // public method that adds a component to the CardLayout-using JPanel
       // with a String name constant
       // that also creates a JRadioButton with the name String as its text
       // and its actionCommand String.
       // Adds JRadioButton to a ButtonGroup and a JPanel
       public void addCard(JComponent component, String name) {
          JRadioButton radioBtn = new JRadioButton(name);
          radioBtn.setActionCommand(name);
          radioBtn.addActionListener(radioBtnListener);
          cardRadioBtnGroup.add(radioBtn);
    
          cardPanel.add(component, name);
          radioBtnPanel.add(radioBtn);
       }
       
       private class RadioBtnListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             String name = e.getActionCommand();
             cardLayout.show(cardPanel, name); //i thought that i could of done the show operation on JPanel(cardPanel) object didn't know have to do it on the cardLayout one
          }
       }
    
       private static void createAndShowGui() {
          CardLayoutEg mainPanel = new CardLayoutEg();
    
          JFrame frame = new JFrame("NoteTakerGui2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);   //not sure what this means
          frame.pack();
          frame.setLocationByPlatform(true); //not sure what this means
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() { //why are your creating a new thread???
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    hhmmmmm, my code works but yours looks about 500% better. I did it a little differently, I'll post my code but I think I'm gona change some parts of my code to make it look more closely like yours. On a side note, are you guys software developers? How much do I need to know to get a jr. position with a company? I'm doing computer programming cert and then computer science degree

  16. #16
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: need help with GUI

    Java Code:
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class NoteTakerGUI extends JPanel {
    	JPanel checkButtons = new JPanel();
    	public NoteTakerGUI() {
    		initiateGUI();
    	}
    	private void initiateGUI() {
    		this.setLayout(null);
    		JPanel radioButton = new JPanel();
    		MyActionListener listen = new MyActionListener();
    		
    		checkButtons.setLayout(new CardLayout());
    		checkButtons.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		checkButtons.setLocation(170,90);
    		checkButtons.setSize(500,300);
    		JPanel javaCard = new JPanel();
    		JCheckBox javaMessage1 = new JCheckBox("Page Cannot Be Displayed");
    		javaCard.add(javaMessage1);
    		JPanel urlCard = new JPanel();
    		urlCard.setLayout(new BoxLayout(urlCard, BoxLayout.PAGE_AXIS));
    		JCheckBox urlMessage1 = new JCheckBox("Client is getting a context error message");
    		JCheckBox urlMessage2 = new JCheckBox("Client is getting an error of incorrect url");
    		JCheckBox urlMessage3 = new JCheckBox("Navigated throug google to correct RBCx website");
    		JCheckBox urlMessage4 = new JCheckBox("Showed how to bookmark the page correctly through bookmark this page");
    		JCheckBox urlMessage5 = new JCheckBox("Showed client how to create a desktop icon");
    		urlCard.add(urlMessage1);
    		urlCard.add(urlMessage2);
    		urlCard.add(urlMessage3);
    		urlCard.add(urlMessage4);
    		urlCard.add(urlMessage5);
    		checkButtons.add(urlCard, "Url");
    		checkButtons.add(javaCard, "Java");
    		
    		//checkButtons.add(javaCard);
    		JLabel name = new JLabel("Name and Phone Number");
    		name.setLocation(100,5);
    		name.setSize(150,20);
    		JTextField input = new JTextField();
    		input.setLocation(250,5);
    		input.setSize(300,20);
    		String[] data = {"User", "Administrator", "Contact", "Non-Contact"};
    		JList type = new JList(data);
    		type.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		type.setLocation(580,5);
    		type.setSize(80,80);
    		JButton makeNote = new JButton("Create Note");
    		makeNote.setLocation(300, 400);
    		makeNote.setSize(120,40);
    		JTextArea finalNote = new JTextArea();
    		finalNote.setLineWrap(true);
    		finalNote.setLocation(20, 460);
    		finalNote.setSize(640, 100);
    		
    		radioButton.setLayout(new BoxLayout(radioButton, BoxLayout.PAGE_AXIS));
    		radioButton.setSize(160,300);
    		radioButton.setLocation(5, 75);
    		//radioButton.setBorder(BorderFactory.createRaisedBevelBorder());
    		radioButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    		ButtonGroup radButtonGroup = new ButtonGroup();
    		JRadioButton java = new JRadioButton("Java");
    		java.setActionCommand("Java");
    		java.addActionListener(listen);
    		JRadioButton url = new JRadioButton("URL");
    		url.setActionCommand("Url");
    		url.addActionListener(listen);
    		JRadioButton prdctKnow = new JRadioButton("Product Knowledge");
    		JRadioButton pmntInq = new JRadioButton("Payment Inquiry");
    		
    		radButtonGroup.add(java);
    		radButtonGroup.add(url);
    		radButtonGroup.add(prdctKnow);
    		radButtonGroup.add(pmntInq);
    		radioButton.add(url);
    		radioButton.add(java);
    		radioButton.add(prdctKnow);
    		radioButton.add(pmntInq);
    		
    		
    		this.add(radioButton);
    		this.add(name);
    		this.add(input);
    		this.add(type);
    		this.add(checkButtons);
    		this.add(makeNote);
    		this.add(finalNote);
    		
    	}
    	public class MyActionListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		CardLayout a = (CardLayout) checkButtons.getLayout();
    		if("Java".equals(e.getActionCommand())){
    			a.show(checkButtons,"Java");
    		}
    		else if("Url".equals(e.getActionCommand())) {
    			a.show(checkButtons,"Url");
    		}
    		
    	}
    	}
    public static void main(String[] args){
    	NoteTakerGUI note = new NoteTakerGUI();
    	JFrame f = new JFrame();
    	f.setSize(700,700);
    	f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
        f.add(note);
    
    }
    }
    forgot to post

Posting Permissions

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