Boolean doesn't return right value - java

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.

Related

Change button's text with an external combobox java

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 ()

A basic logic issue whilst using setters and getters with Swing

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.

how do i setVisible(true) a JLabel from another class

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);

Issues with JButtons and ActionEvent

For some reason, when I click my buttons, it makes it into the actionPerformed method, but the program can't match any of the buttons to an actionevent, so nothing works when any of my buttons are pressed. I took the code out of paintcomponent and put it in the constructor, but now the buttons don't show up. Can someone please help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//
public class game extends JFrame//header
{
private CardPanel mp;
private Font big = new Font("Verdana", Font.BOLD, 100);
private Font small = new Font("Verdana", Font.BOLD, 90);
private Font norm = new Font("Verdana", Font.PLAIN, 23);
private CardLayout cards;
private JButton back;
private boolean clairewang=false;
private boolean wangfam=false;
private boolean cl=false;
private JButton next;
private JButton instruction;
private JButton startb;
private JButton done;
private JTextArea title;
private Color c=new Color(186,225,200);
private int a=0;;
private JButton yes;
private JButton no;
private int x;
private int y;
private JButton claire= new JButton("PLAY!");
private JButton fambam=new JButton("PLAY!");
private JButton sickbob=new JButton("PLAY!");
private String pc="Play as Claire (medium)- an able bodied 20 yearold";
private String pfb="Play as a family (hard)- receive more waterper week.";
private String psb="Play as Bob (easy)- he is connected to thecity's water" ;
private String psb2="supply because he has brain cancer.";
public static void main(String [] args)
{
game b = new game();//create constructor
}
public game()//Bio constructor
{
//set everything for JFrame
setSize(900,600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(0,0);
setResizable(false);//set to false
mp = new CardPanel();
cards = new CardLayout();
back = new JButton("BACK");
done= new JButton("DONE");
next = new JButton("NEXT");
next.setBounds(275,200, 315, 220);
Start beg = new Start();
instruct1 inst1 = new instruct1();
instruct2 inst2 = new instruct2();
choosepanel ch = new choosepanel();
sb1 sb= new sb1();
fb1 f1= new fb1();//FINISH THIS
fb2 f2= new fb2();
fb3 f3= new fb3();
fb4 f4= new fb4();
c1 s= new c1();
mp.setLayout(cards);
mp.add(beg,"start");
mp.add(inst1,"i1");
mp.add(inst2, "i2");
mp.add(ch,"choose");
mp.add(s, "c1");
mp.add(sb, "sb");
mp.add(f1, "fb1");
mp.add(f2, "fb2");
mp.add(f3, "fb3");
mp.add(f4, "fb4");
setContentPane(mp);
setVisible(true);
}
class CardPanel extends JPanel
{
public CardPanel()
{
}
}
class Start extends JPanel implements ActionListener
{
public Start()
{
setLayout(null);
startb = new JButton("START");
startb.setBounds(255,200, 315, 100);
instruction = new JButton("INSTRUCTIONS");
instruction.setBounds(255,320, 315, 100);
//make text area uneditable
instruction.addActionListener(this);
startb.addActionListener(this);
add(startb);
add(instruction);
}
public void paintComponent(Graphics g)
{
setBackground(c);
super.paintComponent(g); //import image
setFont(big);
g.setColor(Color.WHITE);
g.drawString("THIRST", 230,150);
// g.setColor(c);
// g.drawString("THIRST",235,153);
// g.setColor(Color.WHITE);//have loop that draws waves
for( y=0;y<600;y+=200){
for(x=0;x<800;x+=100){
g.drawArc(x,y, 100,50,0,-180);
}
}
for(y=100;y<600;y+=200){
for(x=-50;x<800;x+=100){
g.drawArc(x,y, 100,50,0,-180);
}
}
//for( y=0; y<600;x+=100){
// for(x=0; x<800;x+=50){
// g.drawArc(x, y, 50,50,0,-180);
// }
//}
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==instruction) {
cards.show(mp,"i1"); }
else if(evt.getSource()==startb){
cards.show(mp, "choose");
}
}
}
/* class instruct extends JPanel implements ActionListener
{//header
public instruct()
{
setLayout(null);
next = new JButton("NEXT");
next.setBounds(600,500,100,50);
//instruction.setBounds(225,500, 275, 575);
back = new JButton("BACK");
back.setBounds(100,500,100,50);
// instruction.setBounds(225,500, 375, 575);
next.addActionListener(this);
back.addActionListener(this);
add(next);
add(back);
}
public void paintComponent(Graphics g){
setBackground(c);
super.paintComponent(g);
setFont(big);
g.setColor(Color.WHITE);
g.drawString("THING0", 230,150);
//draw the instructions in the center using blackor white
}
public void actionPerformed(ActionEvent evt){
// if(((JButton)(evt.getSource()).getText()).equals("BACK"))
// cards.show(mp,"start");
// System.out.println("X");
// }
//if ((((JButton)(evt.getSource()).getText()).equals("NEXT")) ){
//cards.show(mp,"i1");
//System.out.println("X");
//}
if (evt.getSource()==next ){
cards.show(mp,"i1");
System.out.println("X");
}
else if (evt.getSource()==back) {
cards.show(mp,"start");
System.out.println("X");
}
}
} */
class instruct1 extends JPanel implements ActionListener{
public instruct1(){
setLayout(null);
next = new JButton("NEXT");
next.setBounds(600,500,100,50);
//instruction.setBounds(225,500, 275, 575);
back = new JButton("BACK");
back.setBounds(100,500,100,50);
next.addActionListener(this);
back.addActionListener(this);
add(next);
add(back);
}
public void paintComponent(Graphics g){
setBackground(c);
super.paintComponent(g); //import image
setFont(big);
g.setColor(Color.WHITE);
g.drawString("THING1", 230,150);
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource()==back) {
cards.show(mp,"start"); }
else if(evt.getSource()==next){
cards.show(mp,"i2");
}
}
}
class instruct2 extends JPanel implements ActionListener{
public instruct2(){
setLayout(null);
done = new JButton("DONE");
back = new JButton("BACK");
back.addActionListener(this);
done.addActionListener(this);
done.setBounds(600,500,100,50);
//instruction.setBounds(225,500, 275, 575);
back.setBounds(100,500,100,50);
add(done);
add(back);
}
public void paintComponent(Graphics g){
setBackground(c);
super.paintComponent(g); //import image
setFont(big);
g.setColor(Color.WHITE);
g.drawString("THING2", 230,150);
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource()==back) {
cards.show(mp,"i1"); }
else if(evt.getSource()==done){
cards.show(mp,"start");
}
}
}
class choosepanel extends JPanel implements ActionListener{
public choosepanel(){
setLayout(null);
sickbob.addActionListener(this);
fambam.addActionListener(this);
claire.addActionListener(this);
sickbob.setBounds(600,200,100,50);
claire.setBounds(600,300,100,50);
fambam.setBounds(600,400,100,50);
add(sickbob);
add(claire);
add(fambam);
//instruction.setBounds(225,500, 275, 575);
}
public void paintComponent(Graphics g){
setBackground(c);
super.paintComponent(g);
setFont(small);
g.setColor(Color.WHITE);
g.drawString("CHOOSE PLAYER", 0,150);
g.setFont(norm);
g.drawString(psb,50,225);
g.drawString(psb2,50,250);
g.drawString(pc,50,325);
g.drawString(pfb,50,425);
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==claire){
cards.show(mp, "c1");
clairewang=true;
}
else if(evt.getSource()==sickbob){
cards.show(mp, "c1");
cl=true;}
else if(evt.getSource()==fambam){
cards.show(mp, "c1");
wangfam=true;
}
}
/* public boolean stuff(){
if(wangfam) return wangfam;
else if(cl)return cl;
else if(clairewang)return clairewang;
} */
}
class c1 extends JPanel implements ActionListener{
public void c1(){
/* setLayout(null);
next=new JButton("NEXT");
no=new JButton("NO");
yes=new JButton("YES");
yes.addActionListener(this);
no.addActionListener(this);
next.addActionListener(this);
yes.setBounds(100,200,100,50);
no.setBounds(300,300,100,50);
next.setBounds(700,400,100,50);
add(yes);
add(no);
add(next); */
next=new JButton("NEXT");
no=new JButton("NO");
yes=new JButton("YES");
yes.addActionListener(this);
no.addActionListener(this);
next.addActionListener(this);
yes.setBounds(100,200,100,50);
no.setBounds(300,200,100,50);
next.setBounds(700,500,100,50);
add(yes);
add(no);
add(next);
}
public void paintComponent(Graphics g){
setBackground(c);
super.paintComponent(g);
setLayout(null);
setFont(norm);
g.setColor(Color.WHITE);
g.drawString("Your flowers are wilting. Water your lawn?",20,100);
//print situation yn
//jbutton yes and j button no
g.fillRect(700, 75, 100, 400);//draw the bar that shows how much wateris left
g.fillArc(700,65,100,20,0,180);
g.fillArc(700,465,100,20,0,-180);
g.setColor(c);
g.fillRect(705,82, 90,390);
g.fillArc(705, 463, 90, 18, 0 , -180);
g.setColor(Color.WHITE);
g.fillRect(705,82, 90,a);
// if(clairewang)System.out.println(a);
if(wangfam)System.out.println("wangfam");
else if(cl)System.out.println("cl");
}
public void actionPerformed(ActionEvent evt){
System.out.println("stuff");
if(evt.getSource()==yes){
if(clairewang){
a=15;
System.out.println(a);}
else if(wangfam)a+=20;
else if(cl)a+=10;
//clairewang=true;
repaint();
}
else if(evt.getSource()==next){
cards.show(mp, "sb");
// cl=true;}
//yes gives an int a value
//no stays 0
}
}
}
}
Move the creation and init code out of paintComponent() into constructor
The code
next=new JButton("NEXT");
no=new JButton("NO");
yes=new JButton("YES");
yes.addActionListener(this);
no.addActionListener(this);
next.addActionListener(this);
yes.setBounds(100,200,100,50);
no.setBounds(300,200,100,50);
next.setBounds(700,500,100,50);
add(yes);
add(no);
add(next);
In fact on each repaint it recreates the buttons and readd them. Thus in actionPerformed() button state is changed (to show pressed state) and the button is recreated. And of course it != clicked one.

Trouble Updating AbstractTableModel from another Class

I'm trying to update a JTable that pulls in data from an ArrayList. I have two frames in my program. The first frame is a JTable (AbstractTableModel) that displays the contents of the ArrayList. I click the "New" button on that frame to bring up the second window, which lets me add to the aforementioned ArrayList. When I click my "Save" button, the second window closes and the first is supposed to refresh with the new row. I don't have any syntactical errors in my code, and it looks conceptually right. I think the first place to start troubleshooting would be in the NoteCntl class. I'm under the impression that getNoteTableUI() should update the view with the new data when it's called, but I'm stumped as to what's going on. I'm new to the concept of Model View Controller, but I'd like to follow that as closely as possible.
Here is the Controller class:
public class NoteCntl {
private NoteTableModel theNoteTableModel = new NoteTableModel();;
private NoteTableUI theNoteTableUI;
private NoteDetailUI theNoteDetailUI;
public NoteCntl(){
theNoteTableUI = new NoteTableUI(this);
}
public NoteTableModel getNoteTableModel(){
return theNoteTableModel;
}
public void getNoteDetailUI(Note theNote){
if (theNoteDetailUI == null || theNote == null){
theNoteDetailUI = new NoteDetailUI(this,theNote);
}
else{
theNoteDetailUI.setVisible(true);
}
}
public NoteTableUI getNoteTableUI(){
theNoteTableModel.fireTableDataChanged(); //why doesn't this do anything?
theNoteTableUI.setVisible(true);
return theNoteTableUI;
}
public void deleteNote(int noteToDelete){
theNoteTableModel.removeRow(noteToDelete);
}
}
The First UI (Table):
public class NoteTableUI extends JFrame{
NoteTableModel noteModel;
NoteCntl theNoteCntl;
JPanel buttonPanel;
JPanel tablePanel;
JTable theNoteTable;
JScrollPane theScrollPane;
JButton backButton;
JButton deleteButton;
JButton editButton;
JButton newButton;
public NoteTableUI(NoteCntl theParentNoteCntl){
theNoteCntl = theParentNoteCntl;
this.initComponents();
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setTitle("NoteTableUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
buttonPanel = new JPanel();
tablePanel = new JPanel();
backButton = new JButton("Back");
newButton = new JButton("New");
newButton.addActionListener(new newButtonListener());
editButton = new JButton("Edit");
deleteButton = new JButton("Delete");
deleteButton.addActionListener(new deleteButtonListener());
noteModel = theNoteCntl.getNoteTableModel();
theNoteTable = new JTable(theNoteCntl.getNoteTableModel());
theScrollPane = new JScrollPane(theNoteTable);
theNoteTable.setFillsViewportHeight(true);
tablePanel.add(theScrollPane);
buttonPanel.add(backButton);
buttonPanel.add(deleteButton);
buttonPanel.add(editButton);
buttonPanel.add(newButton);
this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
this.getContentPane().add(tablePanel, BorderLayout.CENTER);
}
public class deleteButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
int selectedRow = theNoteTable.getSelectedRow();
if (selectedRow == -1){
System.out.println("No row selected");
}
else{
noteModel.removeRow(selectedRow);
}
revalidate();
repaint();
}
}
public class newButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
NoteTableUI.this.setVisible(false);
NoteTableUI.this.theNoteCntl.getNoteDetailUI(null);
/*
NoteDetailCntl theNoteDetailCntl = new NoteDetailCntl();
lastRow++;
long newRow = lastRow;
noteModel.addRow(newRow, 0, "", "");
revalidate();
repaint();
*/
}
}
The 2nd UI (Detail editor)
public class NoteDetailUI extends JFrame{
private final int FRAME_WIDTH = 700;
private final int FRAME_HEIGHT = 500;
private final int FIELD_WIDTH = 10;
JButton saveButton;
JButton backButton;
JTextField idField;
JTextField dateField;
JTextField nameField;
JTextField descriptionField;
JTextArea noteDetail;
JLabel idLabel;
JLabel dateLabel;
JLabel nameLabel;
JLabel descriptionLabel;
JPanel buttonPanel;
JPanel textFieldPanel;
JPanel textAreaPanel;
JPanel mainPanel;
NoteTableModel theNoteTableModel;
NoteDetailCntl theNoteDetailCntl;
NoteCntl theNoteCntl;
Note theCurrentNote;
public NoteDetailUI(){
this.initComponents();
this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
this.setLocationRelativeTo(null);
this.setTitle("NoteDetailUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public NoteDetailUI(NoteCntl parentNoteCntl, Note theSelectedNote){
theNoteCntl = parentNoteCntl;
theCurrentNote = theSelectedNote;
this.initComponents();
this.setSize(400,500);
this.setLocationRelativeTo(null);
this.setTitle("Note");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
saveButton = new JButton("Save");
saveButton.addActionListener(new saveButtonListener());
backButton = new JButton("Back");
backButton.addActionListener(new backButtonListener());
idField = new JTextField(FIELD_WIDTH);
theNoteTableModel = new NoteTableModel();
idField.setText("10");
idField.setEditable(false);
dateField = new JTextField(FIELD_WIDTH);
dateField.setText("20131108");
nameField = new JTextField(FIELD_WIDTH);
nameField.setText("Untitled");
descriptionField = new JTextField(FIELD_WIDTH);
descriptionField.setText("not described");
idLabel = new JLabel("ID");
dateLabel = new JLabel("Date");
nameLabel = new JLabel("Name");
descriptionLabel = new JLabel("Description");
noteDetail = new JTextArea(25,60);
buttonPanel = new JPanel();
textFieldPanel = new JPanel();
textAreaPanel = new JPanel();
mainPanel = new JPanel(new BorderLayout());
buttonPanel.add(backButton);
buttonPanel.add(saveButton);
textFieldPanel.add(idLabel);
textFieldPanel.add(idField);
textFieldPanel.add(dateLabel);
textFieldPanel.add(dateField);
textFieldPanel.add(nameLabel);
textFieldPanel.add(nameField);
textFieldPanel.add(descriptionLabel);
textFieldPanel.add(descriptionField);
textAreaPanel.add(noteDetail);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.add(textAreaPanel, BorderLayout.CENTER);
add(mainPanel);
}
public ArrayList<String> getNoteDetails(){
ArrayList<String> newData = new ArrayList<String>();
newData.add(idField.getText());
newData.add(dateField.getText());
newData.add(nameField.getText());
newData.add(descriptionField.getText());
return newData;
}
public class saveButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
/*
* Access the noteTableData array in NoteTableModel
* Add the newData fields in order
*/
if(theCurrentNote == null){
int newNoteNumber = Integer.parseInt(NoteDetailUI.this.idField.getText());
int newNoteDate = Integer.parseInt(NoteDetailUI.this.dateField.getText());
String newNoteName = NoteDetailUI.this.nameField.getText();
String newNoteDescription = NoteDetailUI.this.descriptionField.getText();
NoteDetailUI.this.theCurrentNote = new EssayNote(newNoteNumber,newNoteDate,newNoteName,newNoteDescription);
NoteDetailUI.this.setVisible(false);
NoteDetailUI.this.dispose();
NoteDetailUI.this.theNoteCntl.getNoteTableUI();
}
else{
//if it's a current Note
}
//Refresh the JTable
}
}
public class backButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
Thanks for all the help. I can provide the other classes if you want to just run the program and see what's happening, but I suspect it's either a problem with the fireTableDataChanged() call in the controller class or a problem with updating the contents of the ArrayList in the SaveButtonListener.

Categories