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);
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 ()
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.
I'm having really hard time splitting up my class into two classes, right now I have the GUI and the logic all together in one class and I want it to be separate, I had a read on objects and classes but I still don't understand how I would split it up for my example, my only "logic" in this program is 3 inner classes which determine what the buttons do (Delete record, save record etc...)
Heres my code, can anyone point me in the right direction?
private String name;
private String surname;
private String phone;
private String address;
private String postcode;
private String email;
static String summary;
private JPanel panel;
private JPanel buttonspanel;
private JPanel labelspanel;
private JPanel tablepanel;
private JFrame frame;
private JLabel lblName;
private JLabel lblEmail;
private JLabel lblPostcode;
private JLabel lblAddress;
private JLabel lblSurname;
private JLabel lblPhone;
private JTextField txtName;
private JTextField txtSurname;
private JTextField txtPostcode;
private JTextField txtEmail;
private JTextField txtPhone;
private JTextField txtAddress;
private JButton btnSave;
private JButton btnUpload;
private JButton btnDelete;
String columns[] = {"Name","Surname","Phone","Address","Postcode","Email"};
private DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
public AddressBook() {
createForm();
createLabels();
createTextField();
createButton();
createTable();
frame.add(panel);
frame.setVisible(true);
}
public void createLabels(){
lblName = new JLabel ("Name");
lblName.setBounds(10, 30, 100, 20);
labelspanel.add (lblName);
lblSurname = new JLabel ("Surname");
lblSurname.setBounds(10, 50, 100, 20);
labelspanel.add (lblSurname);
lblAddress = new JLabel ("Address");
lblAddress.setBounds(10, 70, 100, 20);
labelspanel.add (lblAddress);
lblPhone = new JLabel ("Phone");
lblPhone.setBounds(10, 90, 100, 20);
labelspanel.add (lblPhone);
lblPostcode = new JLabel ("Postcode");
lblPostcode.setBounds(10, 110, 100, 20);
labelspanel.add (lblPostcode);
lblEmail = new JLabel ("Email");
lblEmail.setBounds(10, 130, 100, 20);
labelspanel.add (lblEmail);
}
public void createTextField(){
txtName = new JTextField (null);
txtName.setBounds(110, 30, 150, 20);
labelspanel.add (txtName);
txtSurname = new JTextField (null);
txtSurname.setBounds(110, 50, 150, 20);
labelspanel.add (txtSurname);
txtAddress = new JTextField (null);
txtAddress.setBounds(110, 70, 150, 20);
labelspanel.add (txtAddress);
txtPhone = new JTextField (null);
txtPhone.setBounds(110, 90, 150, 20);
labelspanel.add (txtPhone);
txtPostcode = new JTextField (null);
txtPostcode.setBounds(110, 110, 150, 20);
labelspanel.add (txtPostcode);
txtEmail = new JTextField (null);
txtEmail.setBounds(110, 130, 150, 20);
labelspanel.add (txtEmail);
}
public void createForm(){
frame = new JFrame();
frame.setTitle("Address Book");
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
buttonspanel = new JPanel();
buttonspanel.setLayout(new FlowLayout());
buttonspanel.setBorder (BorderFactory.createEtchedBorder ());
labelspanel = new JPanel();
labelspanel.setBorder (BorderFactory.createEtchedBorder ());
labelspanel.setLayout(null);
labelspanel.setPreferredSize(new Dimension (400,200));
tablepanel = new JPanel();
tablepanel.setLayout(new BorderLayout());
panel.add(buttonspanel);
panel.add(labelspanel);
panel.add(tablepanel);
}
public void createButton(){
btnSave = new JButton ("Save to a file");
btnSave.addActionListener(new SaveHandler());
buttonspanel.add (btnSave);
btnDelete = new JButton ("Delete from the table");
btnDelete.addActionListener(new DeleteHandler());
buttonspanel.add (btnDelete);
btnUpload = new JButton ("Add details to table");
btnUpload.addActionListener(new AddHandler());
buttonspanel.add (btnUpload);
}
public void createTable(){
model.setColumnIdentifiers(columns);
JScrollPane scrollPane = new JScrollPane(table);
tablepanel.add(scrollPane, BorderLayout.SOUTH);
}
class SaveHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnSave)
{
name = ("");
surname = ("");
phone = ("");
address = ("");
postcode = ("");
email = ("");
name = txtName.getText();
surname = txtSurname.getText();
phone = txtPhone.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
email = txtEmail.getText();
summary = ("Name:" +name)+("Surname:" +surname)+("Phone:" + phone)+("Address:" + address)+("Postcode:" + postcode)+("Email:" + email);
String saveFile = summary;
try {
BufferedWriter reader = new BufferedWriter(new FileWriter(new File("userinfo.txt"), true));
reader.write(saveFile);
reader.newLine();
reader.close();
JOptionPane.showMessageDialog(frame, "The details were sucessfuly saved");
} catch (IOException e1) {
JOptionPane.showMessageDialog(frame, "Something went wrong, please try again");
}
}
}
}
class AddHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnUpload)
{
String nameRow = txtName.getText();
String surnameRow = txtSurname.getText();
String phoneRow = txtPhone.getText();
String addressRow = txtAddress.getText();
String postcodeRow = txtPostcode.getText();
String emailRow = txtEmail.getText();
Object[] row = {nameRow,surnameRow,phoneRow,addressRow,postcodeRow,emailRow};
model.addRow(row);
}
}
}
class DeleteHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnDelete)
{
int i = table.getSelectedRow();
if(i >= 0){
model.removeRow(i);
}
}
}
}
public static void main(String[] args) {
new AddressBook();
}
class GUI{
Jbutton btnsave;
Jbutton Delete;
jbutton update
HandleAll btnHandler = new HandleAll();
btnSave.addActionListener(btnHandler);
Deletea.ddActionListener(btnHandler);
update.addActionListener(btnHandler);
class HandleAll implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnSave){
//on click save button
}else if(e.getSource()==Delete){
//on click delete button
}else if(e.getSource()==update){
//on click update button
}
}
}
}
you can follow above approach to seperate logic from Gui . by the way i have made the HandleAll class a inner class. HandleAll implements actionListner interface . on every button press actionperformed on this class will get call but by selecting e.getsource you can check which button was pressed exactly.try this approach
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.
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
}
}
});