0

I'm just starting to learn how to deal with Java Swing.

I want to make a UI that has some Labels (and corrosponding outputs, like "total amount: xxx" and "rest money: xxx"), whereby the "rest money" should be calculated by the Inputbox with the given cash.

I just thought to use a JOptionPane, because its modal. My first try was by just creating a InputDialog.. Then I tried to add some components to this InputDialog, just to test if it works, but nothing happens.

I don't know if I get the idea of this swing-ui-stuff right.. As I know, we got some Panels where we could put some other stuff on it, like buttons. So I tried to create a frame and put a panel on it and put some stuff on the panel and fuse it together with my inputbox.. doesnt seem to work, I only get the Inputbox with ok and cancel buttons.

I just spend hours trying to google around, but nothing could help me.. If somebody is willing to explain where I have the error in my thoughts, I would be very glad :) (and sorry if there are some other mistakes in the code, I'm just learning all that stuff right now... :D ..hope its ok that all my variables are german?)

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class JBarzahlungsDialogUI{

JPanel _barzahlPanel;
JFrame _barzahlFrame;

String _barzahlungseingabe;
int _barzahlungseingabeAlsInt;

JTextField _betrag; 


public JBarzahlungsDialogUI()
{   
    _barzahlPanel = new JPanel();
    _barzahlFrame = new JFrame();
    setPanelUi();
    _barzahlFrame.add(_barzahlPanel);
    _barzahlungseingabe = JOptionPane.showInputDialog(_barzahlFrame, "Betrag, den man dir gegeben hat, eingeben!", "Wert der Barzahlung");
}

//to get the String as Int, so that I can calculate the rest money later
public void getBareingabeAlsInt()
{
    _barzahlungseingabeAlsInt = Integer.valueOf(JOptionPane.showInputDialog(_barzahlFrame, "Betrag, den man dir gegeben hat, eingeben!", "Wert der Barzahlung"));
}

//my trys to get something new added to the input dialog..
public void setPanelUi()
{
    _barzahlPanel.setLayout(new GridLayout());

    JLabel testLabel = new JLabel("supertoller Test");      
    _barzahlPanel.add(testLabel);       
    _barzahlPanel.add(new JButton("Geiler Testbutton"));
}

}

Larry
  • 1
  • 1
  • You're adding no components to the JOptionPane but rather are adding them all to a non-displayed JFrame. How would you expect the JPanel to show when you do this? Instead add the JPanel to the JOptionPane itself. Please look at my answer [here](https://stackoverflow.com/a/9852059/522444) for an example of how to add complex JPanels to a JOptionPane. – Hovercraft Full Of Eels Jun 23 '17 at 00:41
  • If you want to display the contents of the JFrame, then **display** it. Set it visible with `setVisible(true)`. – Hovercraft Full Of Eels Jun 23 '17 at 00:44

0 Answers0