-2

I know I still need to convert string to int and to double for providerID and providerPrice so I can set them and then store them later; I expect I can figure that out just fine. What I can't seem to find is how to make the JLabels align properly. The cloudBack label should be the main background, and each set of JLabel and JTextField should be on a separate line overlapping the main background, with the 3 JButtons on bottom. How do I make them go in the right places? btw the source .png is 400x224 pixels package newprovider;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/*@Michael Christopher,  author/owner */
public class NewProvider {
/** @param args the command line arguments*/
public static void createGUI() {
    // Create the window
    JFrame mainFrame = new JFrame ("NewProvider");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create the MenuBar
    JMenuBar maintenanceMenuBar = new JMenuBar ();
    maintenanceMenuBar.setOpaque(true);
    maintenanceMenuBar.setBackground(new Color(176,224,230));
    maintenanceMenuBar.setPreferredSize(new Dimension(400, 20));

    //create variables
    int providerID;
    String providerName;
    Double providerPrice;
    int addProviderCheck;
    String[] options = {"Yes","No"};

    //create image icon, labels, and JTextFields
    JLabel cloudBack;
    cloudBack = new JLabel (new ImageIcon("images/CloudBack.png"));
    JLabel labelID = new JLabel ("New ProviderID");
    JTextField textID = new JTextField("providerID \n", 10);
    JLabel labelName = new JLabel ("New Provider Name");
    JTextField textName = new JTextField("Provider Name \n", 20);
    JLabel labelPrice = new JLabel ("New Provider Price");
    JTextField textPrice = new JTextField ("Price \n", 5);
    //make Submit, Clear, & Exit buttons
    JButton submit = new JButton("Submit");   
    JButton clear = new JButton("Clear");
    JButton exit = new JButton("Exit");

    /*Set the FlowLayout to arrange the window, setting labels, text fields,
    menubar, background, and orientation*/
    FlowLayout newProviderLayout = new FlowLayout();
    mainFrame.setLayout(newProviderLayout);
    mainFrame.setJMenuBar(maintenanceMenuBar);
    mainFrame.add(cloudBack, BorderLayout.CENTER);
    mainFrame.add(labelID);
    mainFrame.add(textID);
    mainFrame.add(labelName);
    mainFrame.add(textName);
    mainFrame.add(labelPrice);
    mainFrame.add(textPrice);
    mainFrame.add(submit);
    mainFrame.add(clear);
    mainFrame.add(exit);

    //ActionListener for Submit button
    submit.addActionListener(new ActionListener()
    {           public void actionPerformed(ActionEvent e) {
                    String ID = textID.getText();
                    String Name = textName.getText();
                    String Price = textPrice.getText();
            int submitPress = JOptionPane.showOptionDialog(null,
     "ProviderID: " + ID + "\n" + "Provider Name: " + Name +"\n" + "Provider Price: " + Price,
     "Please Verify Content",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null, options,options[0]); 
if (submitPress <= 0) {                  
    //Store Displayed data
    System.out.println(ID);
    System.out.println(Name);
    System.out.println(Price);
    /*providerID = ID;
    providerName = Name;
    providerPrice = Price;  */
    textID.setText("Confirmed");
}
else if (submitPress > 0) {      
            textID.setText("providerID");
            textName.setText("Provider Name");
            textPrice.setText("Price"); }
        }
    });
    //ActionListener for clear button
    clear.addActionListener(new ActionListener()
    {    @Override
        public void actionPerformed(ActionEvent e) {
            textID.setText("providerID");
            textName.setText("Provider Name");
            textPrice.setText("Price"); }
    });
    //ActionListener for Exit Button
    exit.addActionListener(new ActionListener() 
    {   @Override
        public void actionPerformed(ActionEvent e) {
            mainFrame.setVisible(false);
            mainFrame.dispose(); }
    });
//verify intent to add new provider to system before continuing
addProviderCheck = JOptionPane.showOptionDialog(null,
     "This will add a new service provider to the database.\n" 
    + "Are you sure you wish to continue?",
     "Please Verify Intent",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]); 
if (addProviderCheck <= 0) {
              //Display Window
    mainFrame.pack();
    mainFrame.setSize(400, 400);
    mainFrame.setVisible(true);}
else { //else close app
    mainFrame.setVisible(false);
    mainFrame.dispose();
}
}
public static void main(String[] args){
    //draw and show the GUI
    javax.swing.SwingUtilities.invokeLater(() -> {
        createGUI();
    });        
    //store new provider data
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Note that you've posted non-compilable code. Please fix this. – Hovercraft Full Of Eels Apr 12 '15 at 23:45
  • 1
    For [example](http://stackoverflow.com/questions/24176008/background-image-for-a-jpanel-not-working/24176183#24176183), [example](http://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430), [example](http://stackoverflow.com/questions/18393971/having-images-as-background-of-jpanel/18394047#18394047) – MadProgrammer Apr 12 '15 at 23:45
  • @Hovercraft Full Of Eels - package newprovider; was supposed to be in the code block as the top line; aside from that, it's a copy>paste of my working code - is that all you meant or is there a bigger problem I'm missing? MadProgrammer - looking over your examples, ty. Hovercraft 's second answer - thank you. I'm going to spend today looking at JPanel and paintComponent, plus the other 2 layouts you referenced. – Michael Christopher Apr 13 '15 at 13:10
  • Apparently I was wrong about being able to figure out the int and double. I can parse them to new variables, but I can't get them to set to the input values. Netbeans keeps telling me that "Local variables referenced from an inner class must be final or effectively final." If I set the 3 variables to final up where they are defined, netbeans then tells me to remove final, then restarts the cycle. – Michael Christopher Apr 13 '15 at 13:48
  • in the if block for the submit button: int pID = Integer.parseInt(ID); double dPrice = Double.parseDouble(Price); providerID = pID; providerName = Name; providerPrice = dPrice; – Michael Christopher Apr 13 '15 at 13:55
  • Make the key variables non-final instance fields in the class, not local variables, and this will rid you of the final-non-final issue. This is what I was referring to above when I mentioned that your code was non-compilable, this and the other un-posted classes. – Hovercraft Full Of Eels Apr 13 '15 at 16:40

1 Answers1

2

Instead of displayiing your CloudBack.jpg in a JLabel, consider drawing it in a JPanel's paintComponent method using g.drawImage(...), and then placing your other components onto this drawing JPanel. Note that if you add any other JPanels onto the drawing JPanel, be sure that the non-drawing JPanels are set to be non-opaque via setOpaque(false).

I also suggest that you don't use FlowLayout as your main GUI layout since it is not going to add your components in a pleasing, easy to use and view way. Consider looking into use of GridBagLayout or MigLayout in addition to BorderLayout.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I've fixed everything functionally, though I haven't gotten to the layout change yet - I have to look up JPanels tomorrow. Aside from that, any problems in the new code that would be created by implementing a JPanel with drawImage and GridBagLayout? (I did look that part up lol) - edit - I can't post the new code because its too long for a comment... suggestions? – Michael Christopher Apr 14 '15 at 05:22