TextArea in Java Swing Giving Exception in thread "AWT-EventQueue-0" - java

I am trying to retrieve the information from a TextArea which has been defined as follows
JTextArea a1 = new JTextArea(15, 50);
a1.setEditable(true);
centerPanel.add(a1);
a1.setVisible(true);
and in my listener class
private class JobHandler implements ActionListener {
boolean c2shortHand = false;
boolean c1translation = false;
boolean c3onsite = false;
String custName = "";
String jobInfo ="";
String line = "";
public void actionPerformed(ActionEvent e) {
jobInfo=a1.getText();
if (c1.isSelected()) {
c1translation = true;
}
if (c2.isSelected()) {
c2shortHand = true;
}
if (c3.isSelected()) {
c3onsite = true;
}jobInfo=a1.getText();
}
}
When i run the GUI it is fine until i added the last part, where i am trying to assign the TextArea information to a String variable called jobInfo
The Error i get is, which seems to be complaining about threading. Is the problem the fact that i am trying to retrieve the information without doing something else first?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JobsGUI$JobHandler.actionPerformed(JobsGUI.java:202)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
The declaration of a1
public class JobsGUI {
private Manager mmm = new Branch("Watford");
private JFrame myFrame = new JFrame("Jobs GUI");
private Container contentPane = myFrame.getContentPane();
private JButton quitButton = new JButton("Quit");
private JButton addJobButton = new JButton("Add Job");
private JButton clearListButton = new JButton("Clear List");
private JPanel eastPanel = new JPanel();
private JPanel westPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JPanel northPanel = new JPanel();
private JPanel southPanel = new JPanel();
JCheckBox c1, c2, c3;
JTextField t1, t2, t3;
JLabel l1, l2;
JTextArea a1;
JScrollPane s1;
public JobsGUI() {
addAllStaff();
makeFrame();
makeMenus(myFrame);
makeTypes();
addJobs();
}
private void addJobs() {
t1 = new JTextField(10); //Customer name
northPanel.add(t1);
l1 = new JLabel("Enter Customer name here");
northPanel.add(l1);
c1 = new JCheckBox("Translation");
westPanel.add(c1);
c2 = new JCheckBox("ShortHand?");
westPanel.add(c2);
c3 = new JCheckBox("OnSite");
westPanel.add(c3);
JLabel l2 = new JLabel("Job Information");
centerPanel.add(l2);
l2.setVisible(true);
JTextArea a1 = new JTextArea(15, 50);
a1.setEditable(true);
centerPanel.add(a1);
a1.setVisible(true);
addJobButton.addActionListener(new JobHandler());

Related

How to take User input in one frame and adding it to an arraylist to display on another frame

how do you get the User input inside JTextFeild and SET/ADD the input to the table in another frame? Student objects are stored in an ArrayList. The array list is added to table view and displayed in another frame. StudentCntl, StudentListUI, StudentUI are shown below. StudentListUI(left) StudentUI(right)
public class StudentCntl {
private static final int STARTING_INDEX_OF_DISPLAY = 0;
StudentCntl studentCntl;
StudentList studentList;
StudentUI studentUI;
StudentListUI studentListUI;
StudentTableModel theStudentTable;
public StudentCntl() {
studentList = new StudentList();
theStudentTable = new StudentTableModel(studentList.getStudentList());
studentUI = new StudentUI(this, STARTING_INDEX_OF_DISPLAY);
studentUI.setVisible(true);
studentListUI = new StudentListUI(this, studentUI);
studentListUI.setVisible(true);
}
public StudentTableModel getStudentTableModel() {
return theStudentTable;
}
Student getStudent(int index) {
Student student = studentList.getStudentList().get(index);
return student;
}
public class StudentUI extends JFrame {
private int indexOfElementToDisplay;
private final JTextField firstNameDisplayValue = new JTextField(15);
private final JTextField lastNameDisplayValue = new JTextField(15);
private final JTextField gpaDisplayValue = new JTextField(15);
private JPanel studentPanel;
private JPanel buttonPanel;
String[] info = {firstNameDisplayValue.getText(),lastNameDisplayValue.getText(),gpaDisplayValue.getText()};
private final StudentCntl studentCntl;
public StudentUI(StudentCntl studentCntl, int startingIndexOfDisplay) {
this.studentCntl = studentCntl;
indexOfElementToDisplay = startingIndexOfDisplay;
initComponents();
setFieldView();
}
private void initComponents() {
setTitle("Student Viewer");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentPanel = new JPanel(new GridLayout(5, 1));
studentPanel.add(new JLabel("First Name"));
studentPanel.add(firstNameDisplayValue);
studentPanel.add(new JLabel("Last Name"));
studentPanel.add(lastNameDisplayValue);
studentPanel.add(new JLabel("GPA"));
studentPanel.add(gpaDisplayValue);
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton nextButton = new JButton("Next");
JButton previousButton = new JButton("Previous");
JButton deleteButton = new JButton("Delete");
JButton newButton = new JButton("New Entry");
JButton saveButton = new JButton("Save");
nextButton.addActionListener(e -> showNext(indexOfElementToDisplay));
buttonPanel.add(nextButton);
previousButton.addActionListener(e -> showPrevious(indexOfElementToDisplay));
buttonPanel.add(previousButton);
deleteButton.addActionListener(e -> showDelete(indexOfElementToDisplay));
buttonPanel.add(deleteButton);
saveButton.addActionListener(e -> addToList(info));
buttonPanel.add(saveButton);
newButton.addActionListener(e -> showNew());
buttonPanel.add(newButton);
setContentPane(new JPanel(new BorderLayout()));
getContentPane().add(studentPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
private void setFieldView() {
firstNameDisplayValue.setText(studentCntl.getStudent(indexOfElementToDisplay).getFirstName());
lastNameDisplayValue.setText(studentCntl.getStudent(indexOfElementToDisplay).getLastName());
gpaDisplayValue.setText(Double.toString(studentCntl.getStudent(indexOfElementToDisplay).getGpa()));
}
private void NewEntryView() {
firstNameDisplayValue.setText(" ");
lastNameDisplayValue.setText(" ");
gpaDisplayValue.setText(" ");
}
private void addToList(String[] info){
studentCntl.studentList.getStudentList().add(new Student(info));
}
private void showNew() {
NewEntryView();
}
void refreshDisplayWithNewValues(int index) {
setFieldView();
this.repaint();
}

StackOverflowError when adding JTextField to the constructor

So I do know what an StackOverflowError is, the problem however is I can't find it here. I am supposed to make a simple GUI with labels, text fields and buttons. One of these buttons is supposed to "clear" the text fields, so I add that by putting text field as an argument in the constructor, but when I actually add the text fields i just get an stack overflow error. Here is the code:
Orders class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Orders extends JFrame {
public Orders() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2, 2, 2));
JLabel name = new JLabel("Item name:");
JLabel number = new JLabel("Number of:");
JLabel cost = new JLabel("Cost:");
JLabel amount = new JLabel("Amount owed:");
JTextField nameJtf = new JTextField(10);
JTextField numberJtf = new JTextField(10);
JTextField costJtf = new JTextField(10);
JTextField amountJtf = new JTextField(10);
panel1.add(name);
panel1.add(nameJtf);
panel1.add(number);
panel1.add(numberJtf);
panel1.add(cost);
panel1.add(costJtf);
panel1.add(amount);
panel1.add(amountJtf);
JPanel panel2 = new JPanel();
JButton calculate = new JButton("Calculate");
JButton save = new JButton("Save");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
panel2.add(calculate);
panel2.add(save);
panel2.add(clear);
panel2.add(exit);
OnClick action = new OnClick(exit, clear, save, calculate, nameJtf, numberJtf, costJtf, amountJtf);
exit.addActionListener(action);
this.setTitle("Otto's Items Orders Calculator");
this.add(panel1, BorderLayout.NORTH);
this.add(panel2, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Orders();
}
}
OnClick class :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OnClick extends Orders implements ActionListener {
private JButton exitModify = null;
private JButton clearModify = null;
private JButton saveModify = null;
private JButton calculateModify = null;
private JTextField nameModify = null;
private JTextField numberModify = null;
private JTextField costModify = null;
private JTextField amountModify = null;
public OnClick (JButton _exitModify, JButton _clearModify, JButton _saveModify, JButton _calculateModify, JTextField _nameModify, JTextField _numberModify, JTextField _costModify, JTextField _amountModify) {
exitModify = _exitModify;
clearModify = _clearModify;
saveModify = _saveModify;
calculateModify = _calculateModify;
nameModify = _nameModify;
numberModify = _numberModify;
costModify = _numberModify;
amountModify = _amountModify;
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == this.exitModify) {
System.exit(0);
} else if (o == this.clearModify) {
amountModify = null;
nameModify = null;
costModify = null;
numberModify = null;
}
}
}
As soon as I add nameJtf I get this error.

Using an ActionListener to output results of calculations with Geometric Calculator

I am in the process of building a Geometric Calculator, but am having difficult implementing the ActionListener. I found some sample code on the Oracle site and modified to fit the visual concept I am trying to do.
I combed through my code looking for typos and incorrect punctuation and either corrected it or did not find anything that stuck out to me. I looked at similar questions on Stack Overflow and in text books, and my code looks similar in structure to what is being done in the examples. I have pasted the relevant section of the code below.
Eclipse gives me this error message: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
CalcButtonListenerA cannot be resolved to a type I don't understand why this is happening. I thought these lines would take care of resolving the type:
`calcButton1 = new JButton("Calculate");
calcButton1.addActionListener(new CalcButtonListenerA());`
The other relevant code is below...
package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GeometryCalculator implements ItemListener {
JPanel calcTools;
final static String CIRCLEPANEL = "Circle Calculator";
final static String RECTANGLEPANEL = "Rectangle Calculator";
final static String TRIANGLEPANEL = "Triangle Calculator";
private JLabel messageLabel1;
private JLabel messageLabel2;
private JLabel messageLabel3;
private JLabel radiusLabel;
private JLabel baseLabel;
private JLabel heightLabel;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel circleAreaLabel;
private JLabel circumferenceLabel;
private JLabel rectanglePerimeterLabel;
private JLabel rectangleAreaLabel;
private JLabel triangleAreaLabel;
private JTextField choiceTextField;
private JTextField radiusTextField;
private JTextField baseTextField;
private JTextField heightTextField;
private JTextField lengthTextField;
private JTextField widthTextField;
private JButton calcButton1;
private JButton calcButton2;
private JButton calcButton3;
JTextField rectanglePerimeterField = new JTextField(15);
JTextField rectangleAreaField = new JTextField(15);
JTextField triangleAreaField = new JTextField(15);
public void addComponentToPane(Container pane) {
JPanel comboBoxPane = new JPanel();
String comboBoxItems[] = { CIRCLEPANEL, RECTANGLEPANEL, TRIANGLEPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "calcTools".
JPanel calcTool1 = new JPanel();
radiusLabel = new JLabel("Radius");
circumferenceLabel = new JLabel("Circumference");
circleAreaLabel = new JLabel("Area");
radiusTextField= new JTextField(10);
messageLabel1 = new JLabel("Let's make some circle calculations.");
final JTextField circumferenceField = new JTextField(15);
circumferenceField.setEditable(false);
final JTextField circleAreaField = new JTextField(15);
circleAreaField.setEditable(false);
calcButton1 = new JButton("Calculate");
calcButton1.addActionListener(new CalcButtonListenerA());
calcTool1.add(messageLabel1);
calcTool1.add(radiusLabel);
calcTool1.add(radiusTextField);
calcTool1.add(circumferenceLabel);
calcTool1.add(circumferenceField);
calcTool1.add(circleAreaLabel);
calcTool1.add(circleAreaField);
calcTool1.add(calcButton1);
class CalcButtonListenerA implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String radius;
double circumference;
double circleArea;
radius = radiusTextField.getText();
circumference = 2*Double.parseDouble(radius)*Math.PI;
String circ = String.valueOf(circumference);
circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI;
String area = String.valueOf(circleArea);
circumferenceField.setText(circ);
circleAreaField.setText(area);
}
}
JPanel calcTool2 = new JPanel();
messageLabel2 = new JLabel("Let's make some rectangle calculations.");
lengthLabel = new JLabel("Length");
widthLabel = new JLabel("Width");
lengthTextField = new JTextField(10);
widthTextField = new JTextField(10);
rectanglePerimeterLabel = new JLabel("Perimeter");
rectangleAreaLabel = new JLabel("Area");
JTextField rectanglePerimeterField = new JTextField(15);
rectanglePerimeterField.setEditable(false);
JTextField rectangleAreaField = new JTextField(15);
rectangleAreaField.setEditable(false);
JButton calcButton2 = new JButton("Calculate");
calcTool2.add(messageLabel2);
calcTool2.add(lengthLabel);
calcTool2.add(lengthTextField);
calcTool2.add(widthLabel);
calcTool2.add(widthTextField);
calcTool2.add(rectanglePerimeterLabel);
calcTool2.add(rectanglePerimeterField);
calcTool2.add(rectangleAreaLabel);
calcTool2.add(rectangleAreaField);
calcTool2.add(calcButton2);
JPanel calcTool3 = new JPanel();
messageLabel3 = new JLabel("Let's make some triangle calculations");
baseLabel = new JLabel("Base");
heightLabel = new JLabel("Height");
baseTextField = new JTextField(10);
heightTextField = new JTextField(10);
triangleAreaLabel = new JLabel("Area");
triangleAreaField = new JTextField(15);
triangleAreaField.setEditable(false);
JButton calcButton3 = new JButton("calculate");
calcTool3.add(messageLabel3);
calcTool3.add(baseLabel);
calcTool3.add(baseTextField);
calcTool3.add(heightLabel);
calcTool3.add(heightTextField);
calcTool3.add(triangleAreaLabel);
calcTool3.add(triangleAreaField);
calcTool3.add(calcButton3);
calcTools = new JPanel(new CardLayout());
calcTools.add(calcTool1, CIRCLEPANEL);
calcTools.add(calcTool2, RECTANGLEPANEL);
calcTools.add(calcTool3, TRIANGLEPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(calcTools, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(calcTools.getLayout());
cl.show(calcTools, (String)evt.getItem());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Geometry Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GeometryCalculator demo = new GeometryCalculator();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The compile error simply says that at that point the compiler does not know what CalcButtonListenerA means. You define the class CalcButtonListenerA inside the method addComponentToPane however the definition is placed after the usage so at that moment the class is not yet defined, this is somewhat equivalent to what would happen with a variable, you can't do the following:
int y = x + 5; //what is x?
int x = 10; //even if it's defined below, compiler error
You can do this properly in a few ways:
Define it in the method, as a "local class" but before the usage:
public void addComponentToPane(Container pane) {
class CalcButtonListenerA implements ActionListener
{
//...
}
//...
calcButton1.addActionListener(new CalcButtonListenerA());
}
Define it in the class GeometryCalculator not in the method:
public class GeometryCalculator implements ItemListener {
public void addComponentToPane(Container pane) {
//...
calcButton1.addActionListener(new CalcButtonListenerA());
}
private class CalcButtonListenerA implements ActionListener
{
//...
}
}
Define it as an anonymous class, this is a compact way to do it if you don't want to use that code in any other actionListener.
public void addComponentToPane(Container pane) {
//...
calcButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//the actionPerformed code of CalcButtonListenerA
}
});
}
If it was a very important class you could also place it in its own file and import it here.
Don't define a method inside a method.
Use an anonymous class like this (much cleaner) :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GeometryCalculator implements ItemListener {
JPanel calcTools;
final static String CIRCLEPANEL = "Circle Calculator";
final static String RECTANGLEPANEL = "Rectangle Calculator";
final static String TRIANGLEPANEL = "Triangle Calculator";
private JLabel messageLabel1;
private JLabel messageLabel2;
private JLabel messageLabel3;
private JLabel radiusLabel;
private JLabel baseLabel;
private JLabel heightLabel;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel circleAreaLabel;
private JLabel circumferenceLabel;
private JLabel rectanglePerimeterLabel;
private JLabel rectangleAreaLabel;
private JLabel triangleAreaLabel;
private JTextField choiceTextField;
private JTextField radiusTextField;
private JTextField baseTextField;
private JTextField heightTextField;
private JTextField lengthTextField;
private JTextField widthTextField;
private JButton calcButton1;
private JButton calcButton2;
private JButton calcButton3;
JTextField rectanglePerimeterField = new JTextField(15);
JTextField rectangleAreaField = new JTextField(15);
JTextField triangleAreaField = new JTextField(15);
public void addComponentToPane(Container pane) {
JPanel comboBoxPane = new JPanel();
String comboBoxItems[] = { CIRCLEPANEL, RECTANGLEPANEL, TRIANGLEPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "calcTools".
JPanel calcTool1 = new JPanel();
radiusLabel = new JLabel("Radius");
circumferenceLabel = new JLabel("Circumference");
circleAreaLabel = new JLabel("Area");
radiusTextField= new JTextField(10);
messageLabel1 = new JLabel("Let's make some circle calculations.");
final JTextField circumferenceField = new JTextField(15);
circumferenceField.setEditable(false);
final JTextField circleAreaField = new JTextField(15);
circleAreaField.setEditable(false);
calcButton1 = new JButton("Calculate");
calcButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String radius;
double circumference;
double circleArea;
radius = radiusTextField.getText();
circumference = 2*Double.parseDouble(radius)*Math.PI;
String circ = String.valueOf(circumference);
circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI;
String area = String.valueOf(circleArea);
circumferenceField.setText(circ);
circleAreaField.setText(area);
}
});
// class CalcButtonListenerA implements ActionListener
// {
//
// public void actionPerformed(ActionEvent e)
// {
// String radius;
// double circumference;
// double circleArea;
//
// radius = radiusTextField.getText();
// circumference = 2*Double.parseDouble(radius)*Math.PI;
// String circ = String.valueOf(circumference);
// circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI;
// String area = String.valueOf(circleArea);
//
// circumferenceField.setText(circ);
// circleAreaField.setText(area);
//
// }
// }
calcTool1.add(messageLabel1);
calcTool1.add(radiusLabel);
calcTool1.add(radiusTextField);
calcTool1.add(circumferenceLabel);
calcTool1.add(circumferenceField);
calcTool1.add(circleAreaLabel);
calcTool1.add(circleAreaField);
calcTool1.add(calcButton1);
JPanel calcTool2 = new JPanel();
messageLabel2 = new JLabel("Let's make some rectangle calculations.");
lengthLabel = new JLabel("Length");
widthLabel = new JLabel("Width");
lengthTextField = new JTextField(10);
widthTextField = new JTextField(10);
rectanglePerimeterLabel = new JLabel("Perimeter");
rectangleAreaLabel = new JLabel("Area");
JTextField rectanglePerimeterField = new JTextField(15);
rectanglePerimeterField.setEditable(false);
JTextField rectangleAreaField = new JTextField(15);
rectangleAreaField.setEditable(false);
JButton calcButton2 = new JButton("Calculate");
calcTool2.add(messageLabel2);
calcTool2.add(lengthLabel);
calcTool2.add(lengthTextField);
calcTool2.add(widthLabel);
calcTool2.add(widthTextField);
calcTool2.add(rectanglePerimeterLabel);
calcTool2.add(rectanglePerimeterField);
calcTool2.add(rectangleAreaLabel);
calcTool2.add(rectangleAreaField);
calcTool2.add(calcButton2);
JPanel calcTool3 = new JPanel();
messageLabel3 = new JLabel("Let's make some triangle calculations");
baseLabel = new JLabel("Base");
heightLabel = new JLabel("Height");
baseTextField = new JTextField(10);
heightTextField = new JTextField(10);
triangleAreaLabel = new JLabel("Area");
triangleAreaField = new JTextField(15);
triangleAreaField.setEditable(false);
JButton calcButton3 = new JButton("calculate");
calcTool3.add(messageLabel3);
calcTool3.add(baseLabel);
calcTool3.add(baseTextField);
calcTool3.add(heightLabel);
calcTool3.add(heightTextField);
calcTool3.add(triangleAreaLabel);
calcTool3.add(triangleAreaField);
calcTool3.add(calcButton3);
calcTools = new JPanel(new CardLayout());
calcTools.add(calcTool1, CIRCLEPANEL);
calcTools.add(calcTool2, RECTANGLEPANEL);
calcTools.add(calcTool3, TRIANGLEPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(calcTools, BorderLayout.CENTER);
}
// class CalcButtonListenerA implements ActionListener
// {
//
// public void actionPerformed(ActionEvent e)
// {
// String radius;
// double circumference;
// double circleArea;
//
// radius = radiusTextField.getText();
// circumference = 2*Double.parseDouble(radius)*Math.PI;
// String circ = String.valueOf(circumference);
// circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI;
// String area = String.valueOf(circleArea);
//
// circumferenceField.setText(circ);
// circleAreaField.setText(area);
//
// }
// }
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(calcTools.getLayout());
cl.show(calcTools, (String)evt.getItem());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Geometry Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GeometryCalculator demo = new GeometryCalculator();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Java button doesn't work

I'm trying to debug my program for homework, but I can't even do that because I don't know why my buttons aren't working.
Any help is appreciated, thanks! (I know that my findnext is screwy for now, but I didn't know what else to do so I'm just debugging it for now)
public class Window extends JFrame implements ActionListener {
private JButton findnext;
private JButton replace;
private JButton delete;
private JButton upper;
private JTextField from,to;
private JTextArea textArea;
final static Color found = Color.PINK;
final Highlighter hilit;
final Highlighter.HighlightPainter painter;
public Window() {
setTitle("Project 8");
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setSize((d.width/4)*3,d.height);
textArea = new JTextArea ("The apple ate the apple.",8,40);
textArea.setLineWrap(true);
Container contentPane = getContentPane();
addWindowListener(new Close());
contentPane.add(textArea);
JPanel panel = new JPanel();
JButton findnext = new JButton("FindNext");
panel.add(findnext);
from = new JTextField(8);
panel.add(from);
findnext.addActionListener(this);
JButton replace = new JButton("Replace");
panel.add(replace);
to = new JTextField(8);
panel.add(to);
findnext.addActionListener(this);
JButton delete = new JButton("Delete");
panel.add(delete);
findnext.addActionListener(this);
JButton upper = new JButton("Upper");
panel.add(upper);
findnext.addActionListener(this);
contentPane.add(panel, "South");
hilit = new DefaultHighlighter();
painter = new DefaultHighlighter.DefaultHighlightPainter(found);
textArea.setHighlighter(hilit);
}
public void actionPerformed(ActionEvent evt) {
String f = from.getText();
String t = to.getText();
int n = textArea.getText().indexOf(f);
Object source = evt.getSource();
if (source == findnext) {
hilit.removeAllHighlights();
String text = textArea.getText();
int index = text.indexOf(f,0);
if (index>0) {
try {
hilit.addHighlight(index, index+f.length(), DefaultHighlighter.DefaultPainter);
}
catch (BadLocationException e) {
;
}
}else if (source == replace) {
if (n>=0 && f.length() > 0) {
textArea.replaceRange(to.getText(),n,n+f.length());
;
}else if (source == delete) {
textArea.setText(" ");
}else if (source == upper) {
f.toUpperCase() ;
}
}
}
}
}
You have a shadowing problem. You declare...
private JButton findnext;
private JButton replace;
private JButton delete;
private JButton upper;
But in your constructor you do...
JButton findnext = new JButton("FindNext");
//...
JButton replace = new JButton("Replace");
//...
JButton delete = new JButton("Delete");
//...
JButton upper = new JButton("Upper");
Which is re-declaring those variables.
This means that when you try and do...
if (source == findnext) {
It's always false
You're also adding the ActionListener (this) to the findnext button four times...I think you mean to be adding it to each of the other buttons
Beware, there is already a class called Window in AWT, which could cause confusion for people. It's also discouraged to extend directly from a top level container like JFrame and instead should start with a JPanel and the add it to an instance of JFrame (or what ever container you like)
try this:
in ur constructor update this lines:
JButton findnext = new JButton("FindNext");
//
JButton replace = new JButton("Replace");
//
JButton delete = new JButton("Delete");
//
JButton upper = new JButton("Upper");
use this one:
findnext = new JButton("FindNext");
//
replace = new JButton("Replace");
//
delete = new JButton("Delete");
//
upper = new JButton("Upper");

How do I get the JLabel to show which button has been selected

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class filecabinet extends JFrame implements ActionListener {
private String folder = "";
//create buttons
private JButton a = new JButton("A");
private JButton b = new JButton("B");
private JButton c = new JButton("C");
private JButton d = new JButton("D");
private JButton e = new JButton("E");
private JButton f = new JButton("F");
private JButton g = new JButton("G");
private JButton h = new JButton("H");
private JButton i = new JButton("I");
private JButton j = new JButton("J");
private JButton k = new JButton("K");
private JButton l = new JButton("L");
private JButton m = new JButton("M");
private JButton n = new JButton("N");
private JButton o = new JButton("O");
private JButton p = new JButton("P");
private JButton q = new JButton("Q");
private JButton r = new JButton("R");
private JButton s = new JButton("S");
private JButton t = new JButton("T");
private JButton u = new JButton("U");
private JButton v = new JButton("V");
private JButton w = new JButton("W");
private JButton x = new JButton("X");
private JButton y = new JButton("Y");
private JButton z = new JButton("Z");
//create panels
private JPanel aF = new JPanel();
private JPanel gL = new JPanel();
private JPanel mR = new JPanel();
private JPanel sX = new JPanel();
private JPanel yZ = new JPanel();
private JPanel readout = new JPanel();
private JLabel notify = new JLabel("You have selected folder " + folder);
public void ComposePane(){
//set buttons to panels
aF.add(a);
aF.add(b);
aF.add(c);
aF.add(d);
aF.add(e);
aF.add(f);
//set layout of panels
aF.setLayout(new GridLayout(2,3));
gL.add(g);
gL.add(h);
gL.add(i);
gL.add(j);
gL.add(k);
gL.add(l);
gL.setLayout(new GridLayout(2,3));
mR.add(m);
mR.add(n);
mR.add(o);
mR.add(p);
mR.add(q);
mR.add(r);
mR.setLayout(new GridLayout(2,3));
sX.add(s);
sX.add(t);
sX.add(u);
sX.add(v);
sX.add(w);
sX.add(x);
sX.setLayout(new GridLayout(2,3));
yZ.add(y);
yZ.add(z);
yZ.add(readout);
readout.add(notify);
yZ.setLayout(new GridLayout(2,3));
}
public filecabinet(){
setTitle("File Cabinet");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5,1));
ComposePane();
add(aF);
add(gL);
add(mR);
add(sX);
add(yZ);
addActionListener();
}
public void actionPerformed(ActionEvent e) {
folder = ((JButton) e.getSource()).getText();
}
public void addActionListener(){
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
e.addActionListener(this);
f.addActionListener(this);
g.addActionListener(this);
h.addActionListener(this);
i.addActionListener(this);
j.addActionListener(this);
k.addActionListener(this);
l.addActionListener(this);
m.addActionListener(this);
n.addActionListener(this);
o.addActionListener(this);
p.addActionListener(this);
q.addActionListener(this);
r.addActionListener(this);
s.addActionListener(this);
t.addActionListener(this);
u.addActionListener(this);
v.addActionListener(this);
w.addActionListener(this);
x.addActionListener(this);
y.addActionListener(this);
z.addActionListener(this);
}
public static void main(String[]args){
filecabinet frame = new filecabinet();
final int WIDTH = 600;
final int HEIGHT = 600;
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
}
I would prefer not to have to write out a listener for every button
and I was wandering if I could just get the text contained in the button.
such as "You have selected foler X", X is the button chosen.
Again, either use arrays and/or a for loop to consolidate your code getting rid of code redundancy. For example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
#SuppressWarnings("serial")
public class FileCabinet2 extends JPanel {
public static final char FIRST_LETTER = 'A';
public static final char LAST_LETTER = 'Z';
private static final float lARGE_FONT_POINTS = 32f;
private static final int GRID_COLS = 3;
private JLabel chosenLabel = new JLabel();
public FileCabinet2() {
JPanel letterPanel = new JPanel(new GridLayout(0, GRID_COLS));
ActionListener btnListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
chosenLabel.setText(evt.getActionCommand());
}
};
for (char c = FIRST_LETTER; c <= LAST_LETTER; c++) {
String buttonTitle = String.valueOf(c);
JButton button = new JButton(buttonTitle);
setFontPoints(button, lARGE_FONT_POINTS);
letterPanel.add(button);
button.addActionListener(btnListener);
}
JLabel selectionLabel = new JLabel("Selection: ", SwingConstants.RIGHT);
setFontPoints(selectionLabel, lARGE_FONT_POINTS);
setFontPoints(chosenLabel, lARGE_FONT_POINTS);
JPanel bottomPanel = new JPanel(new GridLayout(1, 0));
bottomPanel.add(selectionLabel);
bottomPanel.add(chosenLabel);
bottomPanel.setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
add(letterPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private void setFontPoints(JComponent jComp, float points) {
jComp.setFont(jComp.getFont().deriveFont(points));
}
private static void createAndShowGui() {
FileCabinet2 mainPanel = new FileCabinet2();
JFrame frame = new JFrame("File Cabinet");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Now if you want to change how many button columns there are, all you need to do is change one constant, GRID_COLS, and the same for the size of the font, just change LARGE_FONT_POINTS.
For this:
would prefer not to have to write out a listener for every button
Create a method that accept the JPanel as the parameter and then iterate to each component in JPanel, add the action listener if the component is JButton
This one:
I was wandering if I could just get the text contained in the button.
Just set the value in the JLabel. You already get the button content right?
So, the full code is:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class filecabinet extends JFrame implements ActionListener {
private String folder = "";
// create buttons
private JButton a = new JButton("A");
private JButton b = new JButton("B");
private JButton c = new JButton("C");
private JButton d = new JButton("D");
private JButton e = new JButton("E");
private JButton f = new JButton("F");
private JButton g = new JButton("G");
private JButton h = new JButton("H");
private JButton i = new JButton("I");
private JButton j = new JButton("J");
private JButton k = new JButton("K");
private JButton l = new JButton("L");
private JButton m = new JButton("M");
private JButton n = new JButton("N");
private JButton o = new JButton("O");
private JButton p = new JButton("P");
private JButton q = new JButton("Q");
private JButton r = new JButton("R");
private JButton s = new JButton("S");
private JButton t = new JButton("T");
private JButton u = new JButton("U");
private JButton v = new JButton("V");
private JButton w = new JButton("W");
private JButton x = new JButton("X");
private JButton y = new JButton("Y");
private JButton z = new JButton("Z");
// create panels
private JPanel aF = new JPanel();
private JPanel gL = new JPanel();
private JPanel mR = new JPanel();
private JPanel sX = new JPanel();
private JPanel yZ = new JPanel();
private JPanel readout = new JPanel();
private JLabel notify = new JLabel("You have selected folder " + folder);
public void ComposePane() {
// set buttons to panels
aF.add(a);
aF.add(b);
aF.add(c);
aF.add(d);
aF.add(e);
aF.add(f);
// set layout of panels
aF.setLayout(new GridLayout(2, 3));
gL.add(g);
gL.add(h);
gL.add(i);
gL.add(j);
gL.add(k);
gL.add(l);
gL.setLayout(new GridLayout(2, 3));
mR.add(m);
mR.add(n);
mR.add(o);
mR.add(p);
mR.add(q);
mR.add(r);
mR.setLayout(new GridLayout(2, 3));
sX.add(s);
sX.add(t);
sX.add(u);
sX.add(v);
sX.add(w);
sX.add(x);
sX.setLayout(new GridLayout(2, 3));
yZ.add(y);
yZ.add(z);
yZ.add(readout);
readout.add(notify);
yZ.setLayout(new GridLayout(2, 3));
}
public filecabinet() {
setTitle("File Cabinet");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 1));
ComposePane();
add(aF);
add(gL);
add(mR);
add(sX);
add(yZ);
addActionListener(aF);
addActionListener(gL);
addActionListener(mR);
addActionListener(sX);
addActionListener(yZ);
}
public void actionPerformed(ActionEvent e) {
folder = ((JButton) e.getSource()).getText();
// set the label with folder value
notify.setText("You have selected folder " + folder);
}
// Attach event handler to each JButton in JPanel
public void addActionListener(JPanel panel) {
Component[] components = panel.getComponents();
for (Component component : components) {
if (component instanceof JButton) {
((JButton) component).addActionListener(this);
}
}
}
public static void main(String[] args) {
filecabinet frame = new filecabinet();
final int WIDTH = 600;
final int HEIGHT = 600;
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
}
A quick review of your code, brought your code down to this length.
Imagine, what you can achieve if you give it a bit more insight.
Furthermore, when you see that in your code, you are repeating some
lines again and again for the same thingy, you can indeed think of a
pattern that can accomplish that for you in a rightful manner, with
fewer keystrokes.
Try this modified code of yours :-)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class Filecabinet extends JFrame implements ActionListener {
private String folder = "";
//create buttons
private JButton[] button = new JButton[26];
//create panels
private JPanel aF = new JPanel();
private JPanel gL = new JPanel();
private JPanel mR = new JPanel();
private JPanel sX = new JPanel();
private JPanel yZ = new JPanel();
private JPanel readout = new JPanel();
private JLabel notify = new JLabel("You have selected folder " + folder);
public void ComposePane(){
init_Buttons(button);
//set buttons to panels
for (int i = 0; i < 6; i++)
aF.add(button[i]);
//set layout of panels
aF.setLayout(new GridLayout(2,3));
for (int i = 6; i < 12; i++)
gL.add(button[i]);
gL.setLayout(new GridLayout(2,3));
for (int i = 12; i < 18; i++)
mR.add(button[i]);
mR.setLayout(new GridLayout(2,3));
for (int i = 18; i < 24; i++)
sX.add(button[i]);
sX.setLayout(new GridLayout(2,3));
for (int i = 24; i < 26; i++)
yZ.add(button[i]);
yZ.add(readout);
readout.add(notify);
yZ.setLayout(new GridLayout(2,3));
}
private void init_Buttons(JButton[] button)
{
int value = 65;
for (int i = 0; i < button.length; i++)
{
button[i] = new JButton(Character.toString((char) value++));
button[i].addActionListener(this);
}
}
public Filecabinet(){
setTitle("File Cabinet");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5,1));
ComposePane();
add(aF);
add(gL);
add(mR);
add(sX);
add(yZ);
}
public void actionPerformed(ActionEvent e) {
folder = ((JButton) e.getSource()).getText();
notify.setText(folder);
}
public static void main(String[]args){
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
Filecabinet frame = new Filecabinet();
final int WIDTH = 600;
final int HEIGHT = 600;
//frame.setSize(WIDTH, HEIGHT);
frame.pack();
frame.setVisible(true);
}
});
}
}
You can subclass JButton and write a constructor for the subclass that takes in as a parameter a reference to the action listener, your JFrame.
For instance, your subclass could simply be:
public class YourButton extends JButton {
public YourButton(String title, ActionListener listener) {
super(title);
this.addActionListener(listener);
}
}
So instead of:
private JButton a = new JButton("A");
You would call:
private YourButton a = new YourButton("A", this);
Additionally, as others have suggested, your actionPerformed(ActionEvent e) method requires you to update your notify JLabel instance to reflect the selected button's title.
Hope that helps!

Categories

Resources