Im writing a basic program to simulate a conversation between a user and the computer. I am trying to use a setter and getter to change the text in a textField in another class. The button is clicked and nothing appears in the textField. here is my code:
public class DialogueWindow extends JFrame {
SuperDialogue SD = new SuperDialogue();
JTextField textField = new JTextField();
JButton Answer1 = new JButton();
public DialogueWindow() {
initUI();
}
public void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton Answer1 = new JButton();
Answer1.setBounds(102, 149, 113, 30);
Answer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textField.setText(SD.getReply1());
}
});
panel.add(Answer1);
textField = new JTextField();
textField.setBounds(56, 74, 174, 45);
panel.add(textField);
setTitle("Dialogue");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class SuperDialogue {
private String answer;
public String getReply1(){
return this.answer;
}
public void setReply1(String a1){
this.answer = a1;
}
}
public class Conversation1 extends SuperDialogue {
public void Convo(){
String firstLine = "hello";
setReply1(firstLine);
DialogueWindow DW = new DialogueWindow();
DW.setVisible(true);
DW.setSize(300,300);
}
}
public class Main {
public static void main(String[] args) {
Conversation1 c1 = new Conversation1();
c1.Convo();
}
}
The SuperDialogue in your JFrame class is not the same as the one created in your main.
SuperDialogue SD = new SuperDialogue();
This line is creating a separate SuperDialog which does not has the same values. This one is never set, so getReply1() returns nothing.
Related
I've two class. Can i change a text of a button in a class "home" with an actionlistener of the combobox in a class "panelGestisciImpianti"? I don't unterstand becasue don't works.
The code is this:
//home
package s;
public class home extends JFrame {
private JPanel contentPane;
private panelImpostazioni panel5= new panelImpostazioni();
private JButton btnImpostazioni = new JButton("no"); //$NON-NLS-1$
public static void main(String[] args) {
home frame = new home();
frame.setVisible(true);
}
public home() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState( JFrame.MAXIMIZED_BOTH) ;
setBounds(0, 0, 1963, 688);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
btnImpostazioni.setBounds(0, 560, 140, 140);
contentPane.add(btnImpostazioni);
btnImpostazioni.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.add(panel5);
revalidate();
repaint();
}
});
}
public void changetext() {
btnImpostazioni.setText("yes");
}
}
//panelGestisciImpostazioni
package s;
public class panelImpostazioni extends JPanel {
private JComboBox comboboxLingua = new JComboBox();
static home h=new home();
public panelImpostazioni() {
setBounds(140, 0, 800, 560);
setLayout(null);
comboboxLingua.setBounds(100, 24, 150, 45);
comboboxLingua.setModel(new DefaultComboBoxModel(new String[] {"italiano", "inglese"}));
add(comboboxLingua);
comboboxLingua.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
h.changetext();
}
});
}
}
Thank you.
Its because when you create panel5 you have a new home created.
static home h = new home ().
So when you call changetext method you do it in a new invisible frame.
In order to make this work (it is really really bad) you have to pass your visible "home" as an argument to your panel5. Which means you have to initiate it in home's constructor and not as a field.
public panelImpostazioni(home h)
And in your combobox action listener
h.changetext ()
This is my codes. i had a problem on Check1 Label because i want it to be Visible when the Answer is Correct , by the way i am using Card Layout for that.
i remove the not important codes
public class Category1 extends JPanel {
public static JLabel Check1;
public Category1 () {
Check1 = new JLabel(newImageIcon(getClass().getResource("Buttons/Check.png")));
Check1.setBounds(75 , 305, 40, 40);
Check1.setVisible(false);
add(Check1);
}}
and here's the other class, if you click the Submit1 Button, and if the text in the JTextField is correct, i want the Check1 button to be Visible.
public class QuizPanelc1 {
JPanel Quiz1;
JTextField Answer1;
JButton Submit1;
public QuizPanelc1(){
Answer1 = new JTextField();
Answer1.setBounds(180, 480, 200, 40);
Quiz1.add(Answer1);
Submit1 = new JButton(new ImageIcon(getClass().getResource("Buttons/SubmitButton.png")));
Submit1.setBounds(390, 480, 40, 40);
Quiz1.add(Submit1);
ButtonHandler1 events1 = new ButtonHandler1();
Submit1.addActionListener(events1);
Back1.addActionListener(events1)
}
private class ButtonHandler1 implements ActionListener {
public void actionPerformed (ActionEvent eventClick) {
Object event = eventClick.getSource();
Category1 c1 = new Category1();
if(Submit1==event)
{
if(Answer1.getText().equalsIgnoreCase("Fila"))
{
Answer1.setEditable(false);
JOptionPane.showMessageDialog(null, "Correct");
c1.Check1.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Wrong Answer");
}
}
else
{
System.exit(1);
}
}}
Make check1 a field of the class and not static and then make a public method for setting the visibility:
Example:
public class Category1 extends JPanel {
private JLabel check1;
public void setCheck1Visibility(boolean visible) {
check1.setVisible(visible);
}
public Category1() {
check1 = new JLabel(new ImageIcon(getClass().getResource("Buttons/Check.png")));
check1.setBounds(75, 305, 40, 40);
check1.setVisible(false);
add(check1);
}
}
then since you have an instance of Category1, you can do:
Category1 c1 = new Category1();
c1.setCheck1Visibility(true);
or
c1.setCheck1Visibility(false);
I have been working on this for two days and i still can't figure it out.
So i have these classes:
MyContentPane
ParametersPanel
ControlsPanel
The first class looks like this:
public class MyContentPane extends JPanel{
private ParametersPanel parametersPanel;
private ControlsPanel controlPanel;
private CashRegistersPanel cashRegistersPanel;
public MyContentPane() {
parametersPanel = new ParametersPanel();
controlPanel = new ControlsPanel(parametersPanel);
cashRegistersPanel = new CashRegistersPanel();
this.setLayout(null);
this.add(controlPanel);
this.add(parametersPanel);
this.add(cashRegistersPanel);
controlPanel.setBounds(0, 0, 300, 250);
parametersPanel.setBounds(0, 250, 300, 450);
cashRegistersPanel.setBounds(300, 0, 1500, 700);
this.setPreferredSize(new Dimension(1800,700));
}
}
The second class looks like this :
public class ParametersPanel extends JPanel{
private ControlsPanel controlsPanel;
private JButton reset;
public ParametersPanel() {
controlsPanel = new ControlsPanel(this);
this.setBackground(new Color(219,221,255));
reset = new JButton("Reset parameters");
reset.setFont(new Font("Arial", Font.BOLD, 14));
this.setLayout(null);
this.add(reset);
reset.setBounds(10, 245, 280, 30);
reset.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
boolean startIsPressed = controlsPanel.StartisPressed();
System.out.println("Boolean: " + startIsPressed);
}
});
}
The last class is:
public class ControlsPanel extends JPanel{
private JButton start;
private JButton stop;
private boolean startIsPressed;
public ControlsPanel(final ParametersPanel panel) {
start = new JButton("Start");
stop = new JButton("Stop");
start.setFont(new Font("Arial", Font.BOLD, 14));
stop.setFont(new Font("Arial", Font.BOLD, 14));
this.setLayout(null);
this.setBackground(new Color(199,202,255));
this.add(start);
this.add(stop);
start.setBounds(10, 10, 280, 30);
stop.setBounds(10, 50, 280, 30);
stop.setEnabled(false);
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (start.getText().equals("Start")) {
start.setText("Pause");
stop.setEnabled(true);
startIsPressed = true;
System.out.println("Start Button boolean value: " + startIsPressed + stringStartIsPressed);
}
});
public boolean StartisPressed() {
return startIsPressed;
}
Now the problem is that once I press the Start button in the ControlsPanel, the value of the boolean turns in to true. But when i ask the value of this boolean in the ParametersPanel by pressing the reset button, it returns false. I found out that if i change the order of the panel decleration in the first class (MyContentPane), i solve the problem, but then i can't ask for booleans in the ControlsPanel...
Added another class
public class CashRegistersPanel extends JPanel{
private Image img;
private int amount;
private ParametersPanel parametersPanel;
private ControlsPanel controlsPanel;
private boolean startIsPressed;
public CashRegistersPanel() {
parametersPanel = new ParametersPanel();
startIsPressed = controlsPanel.StartisPressed();
this.setBackground(new Color(237,237,237));
this.setLayout(null);
CashRegister cashRegister = new CashRegister();
img = cashRegister.getImg();
amount = parametersPanel.getAmountOfRegisters();
}
public void setControlsPanel(ControlsPanel cp) {
controlsPanel = cp;
}
I changed the MyContentPane as you guys suggested and added something more:
public class MyContentPane extends JPanel{
private ParametersPanel parametersPanel;
private ControlsPanel controlPanel;
private CashRegistersPanel cashRegistersPanel;
public MyContentPane() {
parametersPanel = new ParametersPanel();
controlPanel = new ControlsPanel(parametersPanel);
parametersPanel.setControlsPanel(controlPanel);
cashRegistersPanel = new CashRegistersPanel(parametersPanel, controlPanel);
Thank you guys, question solved!
You're creating 2 ControlsPanel's.
The one in ParametersPanel should not be made, this is the offending line:
controlsPanel = new ControlsPanel(this);
Instead, create a setter method on ParametersPanel:
public void setControlsPanel(ControlsPanel cp) {
controlsPanel = cp;
}
And change the initialization in the first class to:
parametersPanel = new ParametersPanel();
controlPanel = new ControlsPanel(parametersPanel);
parametersPanel.setControlsPanel(controlPanel); // <- new line
cashRegistersPanel = new CashRegistersPanel();
It looks to me like in MyContentPane you instantiate a ControlsPanel, but then the ParametersPanel makes its own ControlsPanel. I would think you should pass the ControlsPanel to the ParametersPanel instead of creating a new one. Keep in mind that every time you say new you create a completely separate object.
change this:
public ParametersPanel() {
controlsPanel = new ControlsPanel(this);
to this:
public ParametersPanel(ControlPanel cp) {
controlsPanel = cp;
and change the MyControlPanel code to:
controlPanel = new ControlsPanel();
parametersPanel = new ParametersPanel(controlPanel);
and the ControlsPanel constructor to:
public ControlsPanel() {
since you don't seem to be using the reference to the ParametersPanel.
Alternately, you could set startIsPressed as a static variable, but I don't think that is what you really want.
I've got this strange thing, I'm trying to add to the ArrayList but it does not add, it get the values and everything. Please check the code and brigthen me up.
The class where I am trying to add:
public class ManualProductGUI extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField barcodeField;
private JTextField idField;
private JTextField nameField;
private JTextField priceField;
private JTextField quantityField;
private JTextField infoField;
BasketContainer bc = new BasketContainer();
private static final long serialVersionUID = 1L;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
ManualProductGUI dialog = new ManualProductGUI();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public ManualProductGUI() {
setTitle("Product search");
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
barcodeField = new JTextField();
barcodeField.setText("Enter barcode");
barcodeField.setBounds(10, 11, 157, 20);
contentPanel.add(barcodeField);
barcodeField.setColumns(10);
barcodeField.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
barcodeField.setText("");
}
});
barcodeField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();
Product prod = prodCtr.searchProductByBarcode(barcode);
buildFields(prod);
}
});
infoField = new JTextField();
infoField.setEditable(false);
infoField.setBounds(177, 133, 86, 20);
contentPanel.add(infoField);
infoField.setColumns(10);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Add");
okButton.setActionCommand("Add");
buttonPane.add(okButton);
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addToBasket();
setVisible(false);
dispose();
}
});
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}
private void addToBasket()
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();
Product prod = prodCtr.searchProductByBarcode(barcode);
bc.addProduct(prod);
}
}
And here is a part of the Container class:
private ArrayList<Product> listOfItems;
public BasketContainer()
{
listOfItems = new ArrayList<>();
}
public void addProduct(Product prod)
{
listOfItems.add(prod);
}
public ArrayList<Product> getProducts()
{
return listOfItems;
}
It prints out the info on the screen, but it does not add. Am I missing something?
Thank you.
You create a new instance of BasketContainer every time, so that new instances will have a new clear ArrayList.
To fix it you should create your BasketContainer instance somewhere (maybe in an init block?) and save the reference, then use it to add the elements.
You're creating a new BasketContainer within the scope of the method.
BasketContainer bc = new BasketContainer();
bc.addProduct(prod);
The addToBasket method should call addProduct on a field of the class it's in.
Trying to develop a GUI, but I've hit a snag:
I am using a submit button, which will look at a txtEnter field. If the user types "yes" in the txtEnter field and clicks submit, it will execute a shell script. If the user types "no" there will be no action. I know the command to run shell script is Runtime.getRuntime().exec(myShellScript);
How I can use an if-else statement in the SubmitListner to check for the user's input?
import javax.swing.*; import javax.swing.event.DocumentListener;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Executer private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious, btSubmit;
private JPanel panel;
public static void main(String[] args) {
new Executer();
}
public Executer() {
JFrame frame = new JFrame("Script Executer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
myPanel();
Text();
Fields();
Buttons();
frame.add(panel);
frame.setVisible(true);
}
public void myPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void Text(){
lblCommand = new JLabel("Enter Here");
lblCommand.setBounds(145, 100, 150, 20);
Font styleOne = new Font("Arial", Font.BOLD, 13);
lblCommand.setFont(styleOne);
panel.add(lblCommand);
}
public void Fields () {
txtEnter = new JTextField();
txtEnter.setBounds(230, 100, 120, 20);
panel.add(txtEnter);
}
public void Buttons() {
btNext = new JButton ("Next");
btNext.setBounds(300,215,100,20);
panel.add(btNext);
btPrevious = new JButton ("Previous");
btPrevious.setBounds(190,215,100,20);
panel.add(btPrevious);
btSubmit = new JButton("Submit");
btSubmit.setBounds(80,215,100,20);
panel.add(btSubmit);
}
class SubmitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}}}
You have to assign your Actionlistener to your button:
btSubmit = new JButton();
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// here the click happend so you can check your Textfield
String userEntered = txtEnter.getText();
if(userEntered.equalsIgnoreCase("yes"))
{
//run your script
}
}
});