Having an issue with attaching Scroll Pane to my searchResults Text Area. Any one able to help me determine why and how I can make this happen? What mistake am i making.
--- Update ---
Pastie
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Having an issue with attaching Scroll Pane to my searchResults Text Area. Any one able to help me determine why and how I can make this happen? What mistake am i making.
--- Update ---
Pastie
Last edited by Eric Cauley; July 13th, 2021 at 08:07 PM.
How can the code be compiled and executed for testing? It is incomplete and is missing a main method.
If you don't understand my answer, don't ignore it, ask a question.
import static java.nio.file.StandardOpenOption.CREATE; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class realEstate extends JFrame implements ActionListener{ private JPanel contentPane; private JPanel panel1; private JPanel panel2; private JPanel panel3; private JTextField tfContactFirstName; private JTextField tfContactLastName; private JTextField tfContactPhoneNumber; private JTextField tfContactEmail; private JTextField tfFirstContact; private JTextField tfContactBudget; private JLabel lblContactFirstName; private JLabel lblContactLastName; private JLabel lblContactEmail; private JTextField tfNCContactFirstName; private JTextField tfNCContactLastName; private JTextField tfNCContactEmail; private JLabel lblNCContactFirstName; private JLabel lblNCContactLastName; private JLabel lblNCContactEmail; private JCheckBox cbContactPreApp; private JTextField tfApprovedAmmount; private JButton acceptButton; private JButton searchContacts; private JButton searchAgain; private String customerInformation; private ArrayList<String> searchedContacts = new ArrayList<String>(); private JTextArea searchResults = new JTextArea(); private JScrollPane sp; protected GridBagConstraints gbc = new GridBagConstraints(); public static void main(String[] args) { realEstate frame = new realEstate(); frame.setVisible(true); } public realEstate() { super("Real Estate Made Easy"); setDefaultCloseOperation(EXIT_ON_CLOSE); setBounds(300,150,700,400); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); //Menu Bar and Options JMenuBar menuBar = new JMenuBar(); menuBar.setBounds(0, 0, 596, 22); contentPane.add(menuBar); JMenu menuBarFile = new JMenu("File"); menuBar.add(menuBarFile); JMenuItem fileItemNew = new JMenuItem("New"); menuBarFile.add(fileItemNew); JMenuItem fileItemOpen = new JMenuItem("Open"); menuBarFile.add(fileItemOpen); JMenuItem fileItemSave = new JMenuItem("Save"); menuBarFile.add(fileItemSave); JMenuItem fileItemSaveAs = new JMenuItem("Save as.."); menuBarFile.add(fileItemSaveAs); JMenuItem fileItemExit = new JMenuItem("Exit"); fileItemExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menuBarFile.add(fileItemExit); JMenu mnNewMenu = new JMenu("Help"); menuBar.add(mnNewMenu); JMenuItem mntmNewMenuItem = new JMenuItem("Information"); mnNewMenu.add(mntmNewMenuItem); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); contentPane.add(panel1, BorderLayout.NORTH); JLabel title = new JLabel("Real Estate Made Easy"); title.setFont(new Font("Serif", Font.BOLD, 32)); panel1.add(title); contentPane.add(tabbedPane, BorderLayout.CENTER); tabbedPane.addTab("Previous Customer", panel2); tabbedPane.addTab("New Customer",panel3); //Panel 2 - Searchs for Previous Customers panel2.setLayout(new GridBagLayout()); gbc.gridx = 0; gbc.gridy = 0; lblContactFirstName = new JLabel("First Name"); panel2.add(lblContactFirstName, gbc); gbc.gridx = 1; tfContactFirstName = new JTextField(10); panel2.add(tfContactFirstName, gbc); gbc.gridy = 1; gbc.gridx = 0; lblContactLastName = new JLabel("Last Name"); panel2.add(lblContactLastName, gbc); gbc.gridx = 1; tfContactLastName = new JTextField(10); panel2.add(tfContactLastName, gbc); gbc.gridy = 2; gbc.gridx = 0; lblContactEmail = new JLabel("Contact's Email "); panel2.add(lblContactEmail, gbc); gbc.gridx = 1; tfContactEmail = new JTextField(10); panel2.add(tfContactEmail, gbc); gbc.gridy = 3; gbc.gridx = 0; searchContacts = new JButton("Search"); panel2.add(searchContacts, gbc); searchContacts.addActionListener(this); gbc.gridx = 0; gbc.gridy = 2; sp = new JScrollPane(searchResults, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); sp.setBounds(0, 0, 100, 100); panel2.add(sp, gbc); panel2.add(searchResults, gbc); searchResults.setVisible(false); gbc.gridx = 0; gbc.gridy = 3; searchAgain = new JButton("Search Again"); panel2.add(searchAgain, gbc); searchAgain.setVisible(false); searchAgain.addActionListener(this); //Panel 3 - Saves New Customer Information panel3.setLayout(new GridBagLayout()); tfNCContactFirstName = new JTextField(10); lblNCContactFirstName = new JLabel("First Name"); tfNCContactLastName = new JTextField(10); lblNCContactLastName = new JLabel("Last Name"); tfContactPhoneNumber = new JTextField(10); JLabel lblContactPhoneNumber = new JLabel("Phone Number"); tfNCContactEmail = new JTextField(10); lblNCContactEmail = new JLabel("Email"); tfFirstContact = new JTextField(10); JLabel lblFirstContact = new JLabel("Date Received"); tfContactBudget = new JTextField(10); JLabel lblContactBudget = new JLabel("Budget"); cbContactPreApp = new JCheckBox(); JLabel lblContactPreApp = new JLabel("Pre-Approved"); tfApprovedAmmount = new JTextField(10); JLabel lblApprovedAmmount = new JLabel("Pre-Approved Ammount"); acceptButton = new JButton("Save Contact"); gbc.gridx = 0; gbc.gridy = 0; gbc.ipady = 10; gbc.ipadx = 10; gbc.anchor = GridBagConstraints.WEST; panel3.add(lblNCContactFirstName, gbc); gbc.gridx = 1; gbc.gridy = 0; panel3.add(tfNCContactFirstName, gbc); gbc.gridx = 0; gbc.gridy = 1; panel3.add(lblNCContactLastName, gbc); gbc.gridx = 1; gbc.gridy = 1; panel3.add(tfNCContactLastName, gbc); gbc.gridx = 0; gbc.gridy = 2; panel3.add(lblContactPhoneNumber, gbc); gbc.gridx = 1; gbc.gridy = 2; panel3.add(tfContactPhoneNumber, gbc); gbc.gridx = 0; gbc.gridy = 3; panel3.add(lblNCContactEmail, gbc); gbc.gridx = 1; gbc.gridy = 3; panel3.add(tfNCContactEmail, gbc); gbc.gridx = 0; gbc.gridy = 4; panel3.add(lblContactBudget, gbc); gbc.gridx = 1; gbc.gridy = 4; panel3.add(tfContactBudget, gbc); gbc.gridx = 0; gbc.gridy = 5; panel3.add(lblContactPreApp, gbc); gbc.gridx = 1; gbc.gridy = 5; panel3.add(cbContactPreApp, gbc); gbc.gridx = 0; gbc.gridy = 6; panel3.add(lblApprovedAmmount, gbc); gbc.gridx = 1; gbc.gridy = 6; panel3.add(tfApprovedAmmount, gbc); gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 0; gbc.anchor = GridBagConstraints.CENTER; panel3.add(acceptButton, gbc); acceptButton.addActionListener(this); }
--- Update ---
private static Scanner x; public static ArrayList<String> readRecord(String firstName, String lastName, String email) { boolean found = false; ArrayList<String> List = new ArrayList<String>(); String contactFirstName; String contactLastName; String contactPhoneNumber; String contactEmail; String firstContact; String contactBudget; String contactPreApp; String ApprovedAmmount; int y = 0; try { x = new Scanner(new File("Real Estate Contacts.csv")); x.useDelimiter("[,\n]"); while(x.hasNext() && !found) { contactFirstName = x.next(); contactLastName = x.next(); contactPhoneNumber = x.next(); contactEmail = x.next(); firstContact = x.next(); contactBudget = x.next(); contactPreApp = x.next(); ApprovedAmmount = x.next(); if(contactFirstName.equals(firstName) && lastName.length() > 0 && email.length() > 0) { if(contactLastName.equals(lastName) && contactEmail.equals(email)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(contactFirstName.equals(firstName) && lastName.length() > 0 && email.length() == 0) { if(contactLastName.equals(lastName)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n");} } else if(contactFirstName.equals(firstName) && lastName.length() == 0 && email.length() > 0) { if(contactEmail.equals(email)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n");} } else if(contactFirstName.equals(firstName) && lastName.length() == 0 && email.length() == 0) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } //// else if(firstName.length() > 0 && contactLastName.equals(lastName) && email.length() > 0) { if(contactFirstName.equals(firstName) && contactEmail.equals(email)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() > 0 && contactLastName.equals(lastName) && email.length() == 0) { if(contactFirstName.equals(firstName)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() == 0 && contactLastName.equals(lastName) && email.length() > 0) { if(contactEmail.equals(email)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() == 0 && contactLastName.equals(lastName) && email.length() == 0) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } //// else if(firstName.length() > 0 && lastName.length() > 0 && contactEmail.equals(email)) { if(contactFirstName.equals(firstName) && contactLastName.equals(lastName)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() > 0 && lastName.length() == 0 && contactEmail.equals(email)) { if(contactFirstName.equals(firstName)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() == 0 && lastName.length() > 0 && contactEmail.equals(email)) { if(contactLastName.equals(lastName)) { List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } } else if(firstName.length() == 0 && lastName.length() == 0 && contactEmail.equals(email)) { System.out.println(contactFirstName + contactLastName); List.add(contactFirstName + " " + contactLastName + " Email: " + contactEmail + "\nDate Received: " + firstContact + " Budget: " + contactBudget + " PreApproved: " + contactPreApp + " Approval Ammount: " + ApprovedAmmount + "\n\n"); } if(!x.hasNext()) { found = true; } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return List; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource() == acceptButton) { customerInformation = tfNCContactFirstName.getText().toUpperCase() + "," + tfNCContactLastName.getText().toUpperCase() + "," + tfContactPhoneNumber.getText() + "," + tfNCContactEmail.getText().toUpperCase() + "," + tfFirstContact.getText() + "," + tfContactBudget.getText() + "," + cbContactPreApp.isSelected() + "," + tfApprovedAmmount.getText() ; tfNCContactFirstName.setText(""); tfNCContactLastName.setText(""); tfContactPhoneNumber.setText("");tfNCContactEmail.setText(""); tfFirstContact.setText(""); tfContactBudget.setText(""); tfApprovedAmmount.setText(""); if(cbContactPreApp.isSelected()) cbContactPreApp.setSelected(false); System.out.println(customerInformation); File file = new File("Real Estate Contacts.csv"); String saveMe; try { if(!file.exists()) { file.createNewFile(); FileWriter fileWriter = new FileWriter(file, true); BufferedWriter writer = new BufferedWriter(fileWriter); String headerData = "First Name, Last Name, Phone Number"; writer.write(headerData, 0, headerData.length()); writer.newLine(); } FileWriter fileWriter = new FileWriter(file, true); BufferedWriter writer = new BufferedWriter(fileWriter); saveMe = customerInformation; writer.write(saveMe, 0, saveMe.length()); writer.newLine(); writer.close(); } catch(Exception er) { System.out.println("Message " + er); } } if(e.getSource() == searchContacts) { searchedContacts = readRecord(tfContactFirstName.getText().toUpperCase(), tfContactLastName.getText().toUpperCase(), tfContactEmail.getText().toUpperCase()); lblContactFirstName.setVisible(false); tfContactFirstName.setVisible(false); lblContactLastName.setVisible(false); tfContactLastName.setVisible(false); lblContactEmail.setVisible(false); tfContactEmail.setVisible(false); searchContacts.setVisible(false); searchResults.setVisible(true); searchAgain.setVisible(true); sp.setVisible(true); searchResults.setEditable(false); for(int i = 0; i < searchedContacts.size(); i++) { searchResults.append(searchedContacts.get(i) + " "); } repaint(); } if(e.getSource() == searchAgain) { tfContactFirstName.setText(""); tfContactLastName.setText(""); tfContactEmail.setText(""); lblContactFirstName.setVisible(true); tfContactFirstName.setVisible(true); lblContactLastName.setVisible(true); tfContactLastName.setVisible(true); lblContactEmail.setVisible(true); tfContactEmail.setVisible(true); searchContacts.setVisible(true); searchAgain.setVisible(false); searchResults.setVisible(false); sp.setVisible(false); searchResults.setText(""); } } }
--- Update ---
Had to post twice due to length.
Ok, that compiles and executes. Now what is needed to see the problem?
If you don't understand my answer, don't ignore it, ask a question.
If you compile and execute program, and click Previous Customer Tab, search for contact, I have a JTextArea box displayed. I want to attach a JScrollPane top this TextArea box.
In the code the text area box is searchResults, and the ScrollPane is sp. I can not seem to write the code in a way that the scroll bar is correctly displayed in the GUI. I am not sure what I am missing.
Is it something Im not coding correctly in my action listener method? Or some type of typo Im not seeing...
gbc.gridx = 0; gbc.gridy = 2; sp = new JScrollPane(searchResults, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); sp.setBounds(0, 0, 100, 100); panel2.add(sp, gbc); panel2.add(searchResults, gbc);
The first thing I see is the component: searchResults being added to more than one container. A component can only be in one container.something Im not coding correctly
I made this change to allow for testing:// searchedContacts = readRecord(tfContactFirstName.getText().toUpperCase(), tfContactLastName.getText().toUpperCase(), tfContactEmail.getText().toUpperCase()); searchedContacts.add("adasds\ndaddsd\n3333\n44444\n5555\n6666\n7777"); //>>>>>>>>> ADDED for testing
If you don't understand my answer, don't ignore it, ask a question.
I thought potentially that could be an issue. How ever even when I only attached searchedResults to sp, and not add searchedResults to panel2 component it still does not display. I am driving myself nuts trying to figure out the cause behind not being able to add a simplistic scroll pane.
I think a problem is with the use of setVisible to hide/show components. It may be confusing the layout manager.
I think a better solution would be to use CardLayout to swap between showing an input panel and the results panel.
If you don't understand my answer, don't ignore it, ask a question.
Ok, I will give that a try. Now that I have you on the phone, over all the rest of program looks solid? Or do you have suggestions tips on any thing that may help myself on my development?
One comment on the code: many lines are too long. They should be folded so all of the statement can be seen on the screen without having to scroll right-left.
If you don't understand my answer, don't ignore it, ask a question.
Was able to fix by adding this line of coding.
searchedContacts = readRecord(tfContactFirstName.getText().toUpperCase(), tfContactLastName.getText().toUpperCase(), tfContactEmail.getText().toUpperCase()); lblContactFirstName.setVisible(false); tfContactFirstName.setVisible(false); lblContactLastName.setVisible(false); tfContactLastName.setVisible(false); lblContactEmail.setVisible(false); tfContactEmail.setVisible(false); searchContacts.setVisible(false); searchResults.setVisible(true); searchAgain.setVisible(true); searchResults.setEditable(false); for(int i = 0; i < searchedContacts.size(); i++) { searchResults.append(searchedContacts.get(i) + " "); } sp = new JScrollPane(searchResults, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); gbc.gridx = 0; gbc.gridy = 0; gbc.ipadx = 400; gbc.ipady = 225; panel2.add(sp, gbc); repaint();