| | |
JTextArea problems
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 69
Reputation:
Solved Threads: 0
Hi,
I'm having a small yet annoying problem that I've been trying to get my head around for quite some time. Any help would be greatly appreciated.
I am recalling data off a Vector and displaying it on a JTextArea using code like this:
etc... etc....
However, instead of displaying this text in the JTextArea within a JFrame, it opens up a whole new window of the same program to display it.
My code is attached if you should need to look at it.
Thanks for your time.
Crisko.
I'm having a small yet annoying problem that I've been trying to get my head around for quite some time. Any help would be greatly appreciated.
I am recalling data off a Vector and displaying it on a JTextArea using code like this:
Java Syntax (Toggle Plain Text)
menu.genreTextArea.append("Found the following CD(s), whilst searching for: " + search); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Name: " + displayCD.name);
However, instead of displaying this text in the JTextArea within a JFrame, it opens up a whole new window of the same program to display it.
My code is attached if you should need to look at it.
Thanks for your time.
Crisko.
•
•
Join Date: Nov 2006
Posts: 69
Reputation:
Solved Threads: 0
Anything from anyone would be great. Here is my code just in the window incase that makes it easier:
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.io.*; import java.util.Vector; import java.awt.event.*; class MainMenu extends JFrame implements ActionListener{ int rating = 0; int YearSearch = 0; int i = 0; Integer iyear = 0; String yearFieldint = null; String criteria = null; String asearch = null; String gsearch = null; String rsearch = null; String GenreSearch = null; String ArtistSearch = null; JPanel westPanel = new JPanel(); JPanel menuPanel = new JPanel(); JPanel defaultPanel = new JPanel(); JPanel addPanel = new JPanel(); JPanel genrePanel = new JPanel(); JPanel artistPanel = new JPanel(); JPanel yearPanel = new JPanel(); JLabel titleLabel = new JLabel("CD DATABASE", JLabel.CENTER); JLabel defaultLabel = new JLabel("Please select an option from the menu on the left to continue", JLabel.CENTER); JLabel nameLabel = new JLabel("CD Name"); JLabel artistLabel = new JLabel("CD Artist"); JLabel genreLabel = new JLabel("CD Genre"); JLabel yearLabel = new JLabel("CD Year"); JLabel ratingLabel = new JLabel("CD Rating"); JLabel oneLabel = new JLabel("1"); JLabel twoLabel = new JLabel("2"); JLabel threeLabel = new JLabel("3"); JLabel fourLabel = new JLabel("4"); JLabel fiveLabel = new JLabel("5"); JLabel addLabel = new JLabel("Enter your CD details below. Once completed, click SAVE to store details"); JLabel searchgenreLabel = new JLabel ("Enter the genre of the CD you wish to search for below, then click SEARCH"); JLabel searchartistLabel = new JLabel ("Enter the artist of the CD you wish to search for below, then click SEARCH"); JLabel searchyearLabel = new JLabel ("Enter the year from which you wish to search below, then click SEARCH"); JLabel genreLabel2 = new JLabel ("CD Genre"); JLabel yearLabel2 = new JLabel ("CD Year"); JLabel artistLabel2 = new JLabel ("CD Artist"); JButton addButton = new JButton("Add New CD Details"); JButton artistButton = new JButton("Search for an Artist"); JButton genreButton = new JButton("Search for a Genre"); JButton yearButton = new JButton("Search for a Year"); JButton exitButton = new JButton("Exit"); JButton saveButton = new JButton("Save"); JButton searchgenreButton = new JButton("Search"); JButton searchartistButton = new JButton("Search"); JButton searchyearButton = new JButton("Search"); JButton searchratingButton = new JButton("Search"); JTextField nameField = new JTextField(30); JTextField artistField = new JTextField(30); JTextField genreField = new JTextField(30); JTextField yearField = new JTextField(3); JTextField searchgenreField = new JTextField(30); JTextField searchartistField = new JTextField(30); JTextField searchyearField = new JTextField(3); JTextField searchratingField = new JTextField(1); JTextArea genreTextArea = new JTextArea(10, 36); JTextArea artistTextArea = new JTextArea(""); JTextArea ratingTextArea = new JTextArea(""); JTextArea yearTextArea = new JTextArea(""); JScrollPane genreScrollPane = new JScrollPane(genreTextArea); JRadioButton oneButton = new JRadioButton(); JRadioButton twoButton = new JRadioButton(); JRadioButton threeButton = new JRadioButton(); JRadioButton fourButton = new JRadioButton(); JRadioButton fiveButton = new JRadioButton(); ButtonGroup ratingButtonGroup = new ButtonGroup(); public MainMenu() { super("CD System ver 2.0"); setSize(588,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); menuPanel.setLayout(new BorderLayout()); defaultPanel.setLayout(new FlowLayout()); addPanel.setLayout(new FlowLayout()); westPanel.setLayout(new GridLayout(5,0)); genrePanel.setLayout(new FlowLayout()); yearPanel.setLayout(new FlowLayout()); artistPanel.setLayout(new FlowLayout()); menuPanel.add("West", westPanel); menuPanel.add("Center", defaultPanel); defaultPanel.add("Center", defaultLabel); menuPanel.add("North", titleLabel); //Components required for menu /*genreTextArea.setEditable(false); genreTextArea.setLineWrap(true); genreTextArea.setCaretPosition(genreTextArea.getDocument().getLength()); genreScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); genreScrollPane.setPreferredSize(new Dimension(10,30));*/ defaultLabel.setFont(new Font("Serif", Font.BOLD, 14)); titleLabel.setPreferredSize(new Dimension (200, 50)); titleLabel.setFont(new Font("Serif", Font.BOLD, 24)); addButton.addActionListener(this); westPanel.add(addButton); artistButton.addActionListener(this); westPanel.add(artistButton); genreButton.addActionListener(this); westPanel.add(genreButton); yearButton.addActionListener(this); westPanel.add(yearButton); exitButton.addActionListener(this); westPanel.add(exitButton); saveButton.addActionListener(this); oneButton.setMnemonic(KeyEvent.VK_C); oneButton.addActionListener(this); twoButton.setMnemonic(KeyEvent.VK_C); twoButton.addActionListener(this); threeButton.setMnemonic(KeyEvent.VK_C); threeButton.addActionListener(this); fourButton.setMnemonic(KeyEvent.VK_C); fourButton.addActionListener(this); fiveButton.setMnemonic(KeyEvent.VK_C); fiveButton.addActionListener(this); searchgenreButton.addActionListener(this); searchartistButton.addActionListener(this); searchyearButton.addActionListener(this); ratingButtonGroup.add(oneButton); ratingButtonGroup.add(twoButton); ratingButtonGroup.add(threeButton); ratingButtonGroup.add(fourButton); ratingButtonGroup.add(fiveButton); addPanel.add(addLabel); addPanel.add(nameLabel); addPanel.add(nameField); addPanel.add(artistLabel); addPanel.add(artistField); addPanel.add(genreLabel); addPanel.add(genreField); addPanel.add(yearLabel); addPanel.add(yearField); addPanel.add(ratingLabel); addPanel.add(oneLabel); addPanel.add(oneButton); addPanel.add(twoLabel); addPanel.add(twoButton); addPanel.add(threeLabel); addPanel.add(threeButton); addPanel.add(fourLabel); addPanel.add(fourButton); addPanel.add(fiveLabel); addPanel.add(fiveButton); addPanel.add(saveButton); genrePanel.add(searchgenreLabel); genrePanel.add(genreLabel2); genrePanel.add(searchgenreField); genrePanel.add(searchgenreButton); genrePanel.add(genreTextArea); artistPanel.add(searchartistLabel); artistPanel.add(artistLabel2); artistPanel.add(searchartistField); artistPanel.add(searchartistButton); yearPanel.add(searchyearLabel); yearPanel.add(yearLabel2); yearPanel.add(searchyearField); yearPanel.add(searchyearButton); setContentPane(menuPanel); setVisible(true); } public void actionPerformed(ActionEvent event) { if (event.getSource() == exitButton) { System.exit(0); } if (event.getSource() == addButton) { defaultPanel.setVisible(false); genrePanel.setVisible(false); yearPanel.setVisible(false); artistPanel.setVisible(false); menuPanel.add("Center", addPanel); addPanel.setVisible(true); } if (event.getSource() == saveButton){ CD tempcd = new CD(); tempcd.name = nameField.getText(); tempcd.artist = artistField.getText(); tempcd.genre = genreField.getText(); yearFieldint = yearField.getText(); iyear = Integer.valueOf(yearFieldint); tempcd.year = iyear; tempcd.rating = rating; CDsystemv2.vCDDatabase.add(tempcd); nameField.setText(""); artistField.setText(""); genreField.setText(""); yearField.setText("");} if (event.getSource() == artistButton){ defaultPanel.setVisible(false); addPanel.setVisible(false); genrePanel.setVisible(false); yearPanel.setVisible(false); menuPanel.add("Center", artistPanel); artistPanel.setVisible(true); } if (event.getSource() == genreButton){ defaultPanel.setVisible(false); addPanel.setVisible(false); yearPanel.setVisible(false); artistPanel.setVisible(false); menuPanel.add("Center", genrePanel); genrePanel.setVisible(true); } if (event.getSource() == yearButton){ genrePanel.setVisible(false); artistPanel.setVisible(false); addPanel.setVisible(false); defaultPanel.setVisible(false); menuPanel.add("Center", yearPanel); yearPanel.setVisible(true); } if (event.getSource()==oneButton) { rating = 1;} if (event.getSource()==twoButton) { rating = 2;} if (event.getSource()==threeButton) { rating = 3;} if (event.getSource()==fourButton) { rating = 4; } if (event.getSource()==fiveButton) { rating = 5; } if (event.getSource() == searchgenreButton) { String criteria = searchgenreField.getText(); DisplayCDDetails.printgenredetails(criteria); } if (event.getSource() == searchartistButton) { String criteria = searchartistField.getText(); DisplayCDDetails.printartistdetails(criteria); } if (event.getSource() == searchratingButton) { String criteria = searchratingField.getText(); DisplayCDDetails.printratingdetails(criteria); } } } class DisplayCDDetails { public static void printgenredetails(String gsearch){ CD displayCD = new CD(); MainMenu menu = new MainMenu(); for (int i=0; i<CDsystemv2.vCDDatabase.size();i++) { displayCD = (CD)CDsystemv2.vCDDatabase.get(i); if ((gsearch).equals(displayCD.genre)) { menu.genreTextArea.append("Found the following CD(s), whilst searching for: " + gsearch); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Name: " + displayCD.name); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Artist: " + displayCD.artist); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Genre: " + displayCD.genre); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Year: " + displayCD.year); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Rating: " + displayCD.rating); menu.genreTextArea.append("\n"); menu.genreTextArea.append("**************************"); } else { menu.genreTextArea.append("No CDs found in database"); menu.genreTextArea.append("\n"); menu.genreTextArea.append("**************************"); } } } public static void printartistdetails(String asearch){ CD displayCD = new CD(); MainMenu menu = new MainMenu(); for (int i=0; i<CDsystemv2.vCDDatabase.size();i++) { displayCD = (CD)CDsystemv2.vCDDatabase.get(i); if ((asearch).equals(displayCD.genre)) { menu.artistTextArea.append("Found the following CD(s), whilst searching for: " + asearch); menu.artistTextArea.append("\n"); menu.artistTextArea.append("Name: " + displayCD.name); menu.artistTextArea.append("\n"); menu.artistTextArea.append("Artist: " + displayCD.artist); menu.artistTextArea.append("\n"); menu.artistTextArea.append("Genre: " + displayCD.genre); menu.artistTextArea.append("\n"); menu.artistTextArea.append("Year: " + displayCD.year); menu.artistTextArea.append("\n"); menu.artistTextArea.append("Rating: " + displayCD.rating); menu.artistTextArea.append("\n"); menu.artistTextArea.append("**************************"); } else { menu.artistTextArea.append("No CDs found in database"); menu.artistTextArea.append("\n"); menu.artistTextArea.append("**************************"); } } } public static void printratingdetails(String rsearch){ CD displayCD = new CD(); MainMenu menu = new MainMenu(); for (int i=0; i<CDsystemv2.vCDDatabase.size();i++) { displayCD = (CD)CDsystemv2.vCDDatabase.get(i); if ((rsearch).equals(displayCD.genre)) { menu.ratingTextArea.append("Found the following CD(s), whilst searching for: " + rsearch); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("Name: " + displayCD.name); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("Artist: " + displayCD.artist); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("Genre: " + displayCD.genre); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("Year: " + displayCD.year); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("Rating: " + displayCD.rating); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("**************************"); } else { menu.ratingTextArea.append("No CDs found in database"); menu.ratingTextArea.append("\n"); menu.ratingTextArea.append("**************************");; } } } public static void prinyeardetails(String search) { MainMenu menu = new MainMenu(); CD displayCD = new CD(); menu.genreTextArea.append("Found the following CD(s), whilst searching for: " + search); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Name: " + displayCD.name); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Artist: " + displayCD.artist); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Genre: " + displayCD.genre); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Year: " + displayCD.year); menu.genreTextArea.append("\n"); menu.genreTextArea.append("Rating: " + displayCD.rating); menu.genreTextArea.append("\n"); menu.genreTextArea.append("**************************"); } public static void printoutfail() { MainMenu menu = new MainMenu(); CD displayCD = new CD(); menu.genreTextArea.append("No CDs found in database"); menu.genreTextArea.append("\n"); menu.genreTextArea.append("**************************"); } } class CD { String name; String artist; String genre; Integer year; int rating; } public class CDsystemv2 { static Vector vCDDatabase = new Vector(); public static void main (String[] args) { MainMenu start = new MainMenu(); } }
![]() |
Similar Threads
- [J]TextArea problems in SuSE 10.1 (Java)
- two Java GUI problems - please help (Java)
- Connection Problems (Networking Hardware Configuration)
- Pop up ads (Web Browsers)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: need help to solve this problem
- Next Thread: download jar files using java code
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui homework html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql sqlite stop string swing testautomation threads time tree ui unicode validation windows





