Results 1 to 16 of 16
Thread: need help with GUI
- 04-23-2012, 03:23 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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???
-
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.
-
Re: need help with GUI
... or you could use a CardLayout if you find that easier. Both could work just fine.
- 04-23-2012, 04:40 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: need help with GUI
cool...tks...seems that cardlayout is working well......
- 04-23-2012, 06:31 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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.
- 04-23-2012, 07:09 AM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-23-2012, 10:37 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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????
-
Re: need help with GUI
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.
- 04-24-2012, 01:26 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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); } }
-
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.
- 04-24-2012, 03:57 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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); } }
-
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.
- 04-24-2012, 05:16 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: need help with GUI
cool, that worked, thanks
-
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(); } }); } }
- 04-24-2012, 11:31 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: need help with GUI
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
- 04-24-2012, 11:32 PM #16
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: need help with GUI
forgot to postJava 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); } }


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks