TECH Java crew: first time using gui, having trouble

cheet

OT Supporter
Mar 22, 2004
13,285
CA
This is part of a function that takes an iterator of book objects and creates a a window that has the title of each book and the ability to check it out. The amount of books in the iterator will vary.

Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](books.hasNext()){[/SIZE][/LEFT]
[INDENT][LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]currentBook[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = books.next();[/SIZE][/LEFT]
 
[LEFT][SIZE=2]JPanel contentPane = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] JPanel();[/SIZE]
[SIZE=2]contentPane.setPreferredSize([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Dimension(500, 300));[/SIZE]
[SIZE=2]contentPane.setLayout([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FlowLayout());[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] JTextField bookName = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] JTextField([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]currentBook[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].toString());[/SIZE]
[SIZE=2]bookName.setEditable([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]false[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE]
[SIZE=2]JButton checkOut = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] JButton([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Check Out"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]checkOut.addActionListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ActionListener() {[/SIZE][/LEFT]
[INDENT][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] actionPerformed(ActionEvent e)[/SIZE]
[SIZE=2]{[/SIZE][/LEFT]
[INDENT][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]currentCardholder[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] != [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][/LEFT]
[INDENT][LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]currentBook[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].checkOut([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]currentCardholder[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/INDENT][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
[INDENT][SIZE=2]Toolkit.[I]getDefaultToolkit[/I]().beep(); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// signal error[/COLOR][/SIZE][/COLOR][/SIZE]
[/INDENT][/INDENT]
[LEFT][SIZE=2]}[/SIZE][/LEFT]
[/INDENT]

[SIZE=2]});[/SIZE]
 

[LEFT][SIZE=2]contentPane.add(bookName);[/SIZE]

[SIZE=2]contentPane.add(checkOut);[/SIZE]
[SIZE=2]setContentPane(contentPane);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/INDENT]
[SIZE=2]pack();[/SIZE]
[LEFT][SIZE=2]repaint();[/SIZE][/LEFT]

I've never worked with GUIs before I just copied the code from an example and have been trying to edit it. When I run this, it only displays the first book. I know it iterates through correctly so the problem is somehow in the way I'm creating the text fields and buttons.​

I have no idea why it's not working, any ideas?​
 

Users who are viewing this thread

About Us

  • Please do not post anything that violates any Local, State, Federal or International Laws. Your privacy is protected. You have the right to be forgotten. Site funded by advertising, link monetization and member support.
OT v15.8.1 Copyright © 2000-2022 Offtopic.com
Served by fu.offtopic.com

Online statistics

Members online
429
Guests online
62
Total visitors
491

Forum statistics

Threads
369,544
Messages
16,893,975
Members
86,873
Latest member
vitalesan