I'm trying to develop a Find and Replace like in Notepad. Here is my code so far for find. I am wondering how I can keep the dialog box open after pressing the button, so that I can use the dialog box for the next find.
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.*;
class TextAreaEx extends JFrame implements ActionListener, KeyListener {
JButton button1;
JTextArea tx = new JTextArea();
int startFrom = 0;
int offset = 0;
String find = "";
String text = "";
TextAreaEx() {
super("My Frame");
FileInputStream fis = null;
StringBuffer sb = new StringBuffer();
try {
Scanner scan = new Scanner(new FileReader("C:\\Users\\Sam\\Desktop\\networktools.txt"));
while (scan.hasNext()) // while there's still something to read
{
tx.append(scan.nextLine() + "\n"); // append
}
} catch (Exception e) {
e.printStackTrace();
}
// text = sb.toString();
text = tx.getText();
text = text.toLowerCase();
button1 = new JButton("Find");
button1.addActionListener(this);
getContentPane().add(button1, BorderLayout.PAGE_START);
button1.setFocusable(false);
JScrollPane p1 = new JScrollPane(tx);
getContentPane().add(p1);
JFrame.setDefaultLookAndFeelDecorated(true);
tx.addKeyListener(this);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400, 300);
setVisible(true);
}
public static void main(String s[]) {
new TextAreaEx();
}
public void actionPerformed(ActionEvent e) {
startFrom = 0;
offset = 0;
if (e.getSource() == button1) {
find = (String) JOptionPane.showInputDialog(this, "FIND:\n", "Find", JOptionPane.INFORMATION_MESSAGE, null, null, null);
find = find.toLowerCase();
findWord();
this.setVisible(true);
}
}
public void findWord() {
offset = text.indexOf(find, startFrom);
if (offset > -1) {
tx.setFocusable(true);
tx.select(offset, find.length() + offset);
startFrom = find.length() + offset + 1;
} else {
JOptionPane.showMessageDialog(this, "No (more) matches");
}
}
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_F3) {
findWord();
}
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
}
Thanks for the help.
JOptionPane is meant to be used just to aks a simple question for the user to answer with yes/no, ok/cancel or ask the for simple input and than be closed.
"Real" find/replace dialog boxes need to do a lot more than this, don't try to use JOptionPane for this, it was not designed for that purpose.
You will need complete new dialog, (JFrame in you case) that can accomplish what you need, and you will be able to extend it to specify other options like "Match Case", use regular expressions and so on.
Notice: Aside from this your code needs A LOT of other improvements. Start with not using magic numbers and strings hard coded, name your variables appropriately ( and not button1 ) , stick up to the coding and formatting conventions in the language that you are using. Try to use smaller classes and methods that do just one thing - "A class should have one, and only one, reason to change". Try to extract to search functionality itself in its own class and write unit tests for it.
After time you could try to look at the some open source Java projects (JEdit is good example for editor) just to see how professional code looks like.
Good luck.
Related
Okay, so here's the deal. Currently, I am using this:
String[] choices = {"Rock", "Paper", "Scissors"};
String input = (String) JOptionPane.showInputDialog(null, "Please, make your choice", "Rock Paper Scissors!", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
Which is what I need. It creates a drop down menu that allows the user to select Rock, Paper, or Scissors and then outputs it into a String. The problem is, the window that it pops in is REALLY small, and is in the center of the screen. I want to re-size it to be 970 pixels by 300 pixels, and to appear at the location of 950 pixels and 0 pixels.
Now, before you say it, I HAVE tried to use JFrames for this, because I know how to get it the size and at the location I want it. However, I can't get the ActionListener to behave in the way that I want it to.
public static void main(String args[]) throws IOException
{
JFrame hi = new JFrame("Hi");
hi.setSize(970, 300);
hi.setLocation(950, 0);
System.out.println("Hi");
Picture Hi = new Picture("c:/The Game/Cool.png");
Hi.display();
JButton B = new JButton("Hey There!");
hi.add(B);
int c = Output(hi);
}
public int Output(JFrame b)
{
int j = 0;
j = //CODE NEEDED HERE
return j;
}
#Override
public void actionPerformed(ActionEvent arg0) {
}
So, the problem with this is that I need the JFrame to pop up in then "CODE NEEDED HERE" section, and then, upon clicking the button, to return a certain value, and then to close out of the JFrame. However, the JFrame doesn't wait for the Output() function, and it immediately returns j, which is equal to 0. Instead, it just does whatever is in the actionPerformed function.
So, I am asking for a solution to either one of these problems. How to either re-size the JOptionPane.showInputDialog() or to get the JFrame to return an int value upon clicking a button.
Sorry if this is really poorly explained, I'm really new to JOptionPane and JFrames.
JOptionPane is quite configurable, it's also nicely self contained, taking a lot of the repetitive, boil plate code and hiding it away in an easy to use package.
But that doesn't mean you have to use it that way, you can simply create an instance of JOptionPane, which is just an ancestor of JComponent and add it to what ever you want.
The difficulty is plumbing all the functionality back together, so you can respond to the buttons, for example.
Just beware, your example places the dialog under my task bar (yes, mine is at the top of the screen), so I can tell you, as a user, that will annoy me!
So, this example basically wraps up all the boiler plate code into a simple class/method, which makes it easy to repeatedly prompt the user the same question, over and over again...
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.OK_CANCEL_OPTION;
import static javax.swing.JOptionPane.UNINITIALIZED_VALUE;
import static javax.swing.JOptionPane.VALUE_PROPERTY;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
String pick = Picker.pick();
System.out.println("You picked " + pick);
System.exit(0);
}
public static class Picker {
public static String pick() {
String[] choices = {"Rock", "Paper", "Scissors"};
JOptionPane pane = new JOptionPane("Please, make your choice", JOptionPane.QUESTION_MESSAGE,
OK_CANCEL_OPTION, null, null, null);
pane.setWantsInput(true);
pane.setSelectionValues(choices);
pane.setInitialSelectionValue(choices[0]);
JDialog dialog = new JDialog();
dialog.setModal(true);
PropertyChangeListener listener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
// Let the defaultCloseOperation handle the closing
// if the user closed the window without selecting a button
// (newValue = null in that case). Otherwise, close the dialog.
if (dialog.isVisible()
&& (event.getPropertyName().equals(VALUE_PROPERTY))
&& event.getNewValue() != null
&& event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
dialog.setVisible(false);
}
}
};
WindowAdapter adapter = new WindowAdapter() {
private boolean gotFocus = false;
public void windowClosing(WindowEvent we) {
pane.setValue(null);
}
public void windowClosed(WindowEvent e) {
dialog.removePropertyChangeListener(listener);
dialog.getContentPane().removeAll();
}
public void windowGainedFocus(WindowEvent we) {
// Once window gets focus, set initial focus
if (!gotFocus) {
pane.selectInitialValue();
gotFocus = true;
}
}
};
dialog.addWindowListener(adapter);
dialog.addWindowFocusListener(adapter);
dialog.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ce) {
// reset value to ensure closing works properly
pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
}
});
pane.addPropertyChangeListener(listener);
dialog.add(pane);
//dialog.pack();
//dialog.setLocationRelativeTo(null);
dialog.setSize(970, 300); // This is bad idea, use an EmptyBorder instead
dialog.setLocation(950, 0);
dialog.setVisible(true);
String pick = null;
Object value = pane.getInputValue();
if (value != UNINITIALIZED_VALUE) {
pick = value.toString();
}
return pick;
}
}
}
The reason you're having problems with JFrame is because it's not designed to block the code execution when displayed, a JDialog can, see How to Make Dialogs for more details
package javaisnotbannana;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Javaisnotbannana{
public static void main(String[] args) {
window();
}
////////////////////////////////////////////////////
public static void window()
{
JFrame window= new JFrame();
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jt = new JTextField(30);
JButton jb = new JButton("Enter");
window.setTitle("ThisisTitleofWindow");
window.setVisible(true);
window.setSize(500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//textfield
jp.add(jt);
jt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String inoutt = jt.getText();
jl.setText(inoutt);
}
});
Why dose this lower section have the problem "Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range:(how ever many characters i entered)".str is receiving what is typed in the Jtextfield and im trying to filter the input to give a different output. With out a filter it works fine like above just press enter,but when i try to press the button i and filter i get an error.
//button
jp.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = jt.getText();
String text="";
int A=0;
int B=1;
int C=2;
for(int num=0;num<=str.length()/3;num++)
{
if (str.charAt(A) == 'T'&&str.charAt(B) == 'A'&&str.charAt(C)=='S')
{
text+="smell tasty";
}
else if(str.charAt(A) == 'B'&&str.charAt(B) == 'A'&&str.charAt(C)=='N')
{
text+="bannanas";
}
A+=3;
B+=3;
C+=3;
}
jl.setText(text);
}
});
jp.add(jl);
window.add(jp);
}
}
your index is overshooting use this instead
for(int num=0;num<str.length()/3;num++)
just < not <=
The problem seems to the fact that you are overthinking the it. Remember, Java Strings are zero indexed so num<=str.length()/3 should be something more like num < str.length()/3
But, having said that, the whole thing seems woefully inefficient (not to mention confusing)
You could use something like...
for (int num = 0; num + 3 < str.length(); num++) {
String test = str.substring(num, 3);
if ("tas".equalsIgnoreCase(test)) {
text = "smell tasty";
break;
} else if ("ban".equalsIgnoreCase(test)) {
text = "bannanas";
break;
}
}
But even that's a long way around an otherwise simple problem.
Instead, you could simply use something like...
String str = "tas";
String text = "";
if (str.toLowerCase().contains("tas")) {
text = "smell tasty";
} else if (str.toLowerCase().contains("ban")) {
text = "bannanas";
}
I'm new to Java and have set myself the task of trying to create a simple calculator (and GUI) to advance my understanding and skills of the language.
Take this code:
import java.awt.FlowLayout;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class calc extends JFrame {
public JTextField input;
public JTextField output;
public JPanel Window;
public JButton math_button[] = new JButton[5];
public JButton number_button[] = new JButton[10];
public String[] number_button_name = {"1","2","3","4","5","6","7","8","9","0"};
public String[] name = {"Add", "Mulitply", "Divide", "Subtract", "Equals"};
public JFrame frame = new JFrame();
public JPanel math_panel = new JPanel();
public JPanel number_panel = new JPanel();
public JTextField math_input = new JTextField();
boolean trrgger = false;
thehandler handler = new thehandler();
public void go()
{
for(int b=0; b<number_button.length; b++)
{
number_button[b] = new JButton(number_button_name[b]);
number_button[b].addActionListener(handler);
number_panel.add(number_button[b]);
}
for(int i=0; i<math_button.length;i++)
{
math_button[i] = new JButton(name[i]);
math_button[i].addActionListener(handler);
math_panel.add(math_button[i]);
}
frame.getContentPane().add(BorderLayout.NORTH, math_input);
frame.getContentPane().add(BorderLayout.SOUTH, math_panel);
frame.getContentPane().add(BorderLayout.CENTER, number_panel);
frame.setSize(400,400);
frame.setVisible(true);
}
//Method to handle the math and return the results of whichever 'button' was pressed
static int Math(String button_num, int first_num, int second_num)
{
int total = 0;
if(button_num == "Add")
{
total = first_num + second_num;
}
else if (button_num == "Mulitply") //multiply
{
total = first_num * second_num;
}
else if (button_num == "Divide") //divide
{
total = first_num / second_num;
}
else if (button_num == "Substract") //subtract
{
total = first_num - second_num;
}
else if (button_num == "Equals") //subtract
{
total = total;
}
return total;
}
//Action Events - Code that is triggered once the associated button is clicked
public class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
for (int h = 0; h <math_button.length; h++)
{
if(event.getSource()==math_button[h])
{
int firstn = Integer.parseInt(math_input.getText());
math_input.setText("");
int secondn = Integer.parseInt(math_input.getText());
System.out.println(calc.Math(math_button[h].getText(), firstn, secondn));
}
}
for(int n=0; n<number_button.length; n++)
{
if(event.getSource()==number_button[n])
{
String number_clicked = (number_button[n].getText());
String number = math_input.getText();
math_input.setText(number + number_clicked);
}
}
}
}
}
The idea behind this code is to create a simple GUI and allows the user to input the desired amount of numbers and then press the 'equals' button to display the result. However, as stated I'm having a problem with the logic. I can get the first entered number from the JTextField, clear the text once the first variable has been initialized but this is where the program falls over. The variable 'second_num' is passed to the 'Math' method as blank (which throws up errors) because that's what I tell the ActionEvent to do in order to allow for more fluid use of the program, no user wants to have to keep clearing the input box when using a calculator.
Anybody got any ideas?
Thanks
int firstn = Integer.parseInt(math_input.getText());
math_input.setText("");
int secondn = Integer.parseInt(math_input.getText());
What exactly do you expect the above lines to do? You are getting the text from math_input. Then you set it to an empty string. And by immediately getting the string back you are expecting to get something other than empty string?
The correct approach would be:
First time the handler is called (i.e. a "math" button is clicked) collect the input. Store it somewhere.
Next time this handler will be called you will have your next input. And so on until the user clicks on "=" to evaluate the whole expression.
Advice: If you are new to java, you might find it easier to create a calculator on the command line first. The functionality of a calculator does not require a GUI. In command line collecting the input is more simple. If you get that working then you can proceed to more fancy stuff like Swing
I looked at your code but its sound complicated for me. I recommend you to use IDE like Netbeans. Create swing application. To add two number all you need to do is as follow
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int input_1 = Integer.parseInt(jTextField1.getText());
int input_2 = Integer.parseInt(jTextField2.getText());
jTextField3.setText(String.valueOf((input_1+input_2)));
}
http://i.stack.imgur.com/1G4v7.jpg
I have a GUI application that uses an InputVerifier to check the content of text fields before yielding the focus. This is all very normal. Yesterday, however, discovered a problem - it seems to be a bug, but I cannot find any mention of it anywhere. Before I report this as a bug, I thought I would ask: am I missing something obvious here?
Situation:
A set of text fields with InputVerifiers.
Listeners for FocusLost and FocusGained on all controls, so I can see what is happening.
A separate thread uses a DefaultKeyboardFocusManager to report (every 2 seconds) which control has the focus.
I place invalid data in a JTextField in the middle of the form, and try to leave the control.
If I try to move away from this control using the mouse, or using the tab-key, I cannot. The FocusLost event does not fire and the control properly retains the focus.
However, if I try to move away from the control in reverse tab order, using Shift-Tab, sometimes the FocusLost event fires. If this happens, the separate thread reports that no control has the focus, i.e., getFocusOwner() returns null.
Edit: below is a small sample program that shows the problem. The problem has nothing to do with the extra thread - the thread is just there to make the problem more obvious. If there is a race-condition, it is somewhere in Swing.
To see the problem, go to the second text box and empty it. The control should retain the focus, and does so unless you leave it by pressing shift-tab. Unlike the full application, the error seems to occur here 100% of the time. This is true both under OpenJDK 6 and under Oracle Java 7.
This is almost too obvious to be a bug, plus it happens in multiple Java environments. Hence, my suspicion that I am missing something obvious. Anyone?
public class FocusBugDemo extends JFrame {
static JTextField txtOne = new JTextField("Ignore this control");
static JTextField txtTwo = new JTextField("Delete this text, then press shift-tab");
static JLabel lblFocus = new JLabel("");
static KeyboardFocusManager kfm = new DefaultKeyboardFocusManager();
public static void main(String[] args) {
new FocusBugDemo();
Thread t = new Thread() {
#Override
public void run() {
while(true) {
Component c = kfm.getFocusOwner();
String focusInfo = "elsewhere";
if (c == null) { focusInfo = "null";
} else if (c == txtOne) { focusInfo = "txtOne";
} else if (c == txtTwo) { focusInfo = "txtTwo";
}
lblFocus.setText(System.currentTimeMillis() + " - Focus owner " + focusInfo);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
};
t.start();
}
private FocusBugDemo() {
super("Focus bug demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300,100));
setLayout(new GridLayout(3,1));
NotEmpty validator = new NotEmpty();
txtOne.setInputVerifier(validator);
txtTwo.setInputVerifier(validator);
add(txtOne);
add(txtTwo);
add(lblFocus);
pack();
setVisible(true);
}
private class NotEmpty extends InputVerifier {
#Override
public boolean verify(JComponent input) {
JTextField txtField = (JTextField) input;
return (txtField.getText().length() > 0);
}
}
}
Now reported to Oracle as bug 7167871.
Using your sscce, I am unable to reproduce the effect you describe on Mac OS X, Java 6, which supports #CatalinaIsland's observation. In particular, focus never leaves an empty text field using either tab or shift-tab; focus becomes null only when the frame is deactivated.
I see two threads accessing multiple fields with no synchronization at all. At a minimum, you should use EventQueue.invokeLater() in t to update the GUI, as described in Concurrency in Swing and show below.
The broader question is this: What focus problem are you trying to solve using t?
import java.awt.Component;
import java.awt.DefaultKeyboardFocusManager;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FocusDemo {
private static final JTextField txtOne =
new JTextField("Ignore this control");
private static final JTextField txtTwo =
new JTextField("Delete this text, then press shift-tab");
private static final JLabel lblFocus = new JLabel("");
public static void main(String[] args) {
new FocusDemo();
Thread t = new Thread() {
#Override
public void run() {
while (true) {
EventQueue.invokeLater(new Runnable() {
KeyboardFocusManager kfm =
new DefaultKeyboardFocusManager();
#Override
public void run() {
Component c = kfm.getFocusOwner();
String focusInfo = "elsewhere";
if (c == null) {
focusInfo = "null";
} else if (c == txtOne) {
focusInfo = "txtOne";
} else if (c == txtTwo) {
focusInfo = "txtTwo";
}
lblFocus.setText(System.currentTimeMillis()
+ " - Focus owner " + focusInfo);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}
};
t.start();
}
private FocusDemo() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Focus bug demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(300, 100));
f.setLayout(new GridLayout(3, 1));
NotEmpty validator = new NotEmpty();
txtOne.setInputVerifier(validator);
txtTwo.setInputVerifier(validator);
f.add(txtOne);
f.add(txtTwo);
f.add(lblFocus);
f.pack();
f.setVisible(true);
}
});
}
private class NotEmpty extends InputVerifier {
#Override
public boolean verify(JComponent input) {
JTextField txtField = (JTextField) input;
return (txtField.getText().length() > 0);
}
}
}
I'm making this grade keeping program for my computer science class and the one thing I haven't gotten to work is saving/loading sessions. As I have it set up now it automatically saves when you exit and loads when you start if there is a file to load. However, I feel I'm loading or saving it incorrectly; there's one main JFrame that holds all the data, and that's the one object that is saved. When it's loaded, well, it'd be easier to show you.
If it looks like this when I close it::
Then it'll look like this when I start it up again:
Also, the "Enter Student" button, as well as the ActionListener on the JTextField used for input, cease to work when the program loads the JFrame. The program is split into 3 class files:
Gradebook:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class GradeBook implements java.io.Serializable {
private static JFrame frame;
public static void main(String[] args) throws Exception{
try{
FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
frame = (JFrame) ois.readObject();
ois.close();
} catch(Throwable t) {
frame = new JFrame("Course Grades");
}
frame.addWindowListener(new closing());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CourseGrade());
frame.pack();
frame.setVisible(true);
}
closing:
private static class closing extends WindowAdapter {
public void windowClosing(WindowEvent e) {
try {
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(frame);
oos.close();
} catch(Throwable t){System.out.println(t.getMessage());}
}
}
}
CourseGrade:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.util.Random;
import javax.swing.table.*;
import java.io.*;
public class CourseGrade extends JPanel implements java.io.Serializable {
private JLabel entername;
private JTextField in;
private JTextArea list;
private JScrollPane scroll;
private String[] students = new String[1000];
private JButton enter;
private JButton[] studentbuttons = new JButton[1000];
private JButton[] delete=new JButton[1000];
private int numstudents;
private JFrame[] studentframes=new JFrame[1000];
private static JTable[] tables=new JTable[1000];
private static DefaultTableModel[] model=new DefaultTableModel[1000];
private static int[] numass=new int[1000];
public CourseGrade() {
String[][] data = {{"", "", "", ""}};
String[] headers = {"Assignment", "Date Assigned", "Score", "Percentage"};
for(int i=0; i<tables.length; i++){
model[i] = new DefaultTableModel(data, headers);
tables[i] = new JTable(model[i]){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
}
numstudents=0;
in=new JTextField(13);
in.addActionListener(new enterListener());
list=new JTextArea();
scroll=new JScrollPane(list);
list.setEditable(false);
entername=new JLabel("Enter a student's name");
enter=new JButton("Enter Student");
enter.addActionListener(new enterListener());
setPreferredSize(new Dimension(260, 300));
setBackground(Color.green);
add(entername);
add(in);
add(enter);
add(scroll);
scroll.setPreferredSize(new Dimension(240, 200));
}
private class enterListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String x=in.getText();
String y="";
String z="";
in.setText("");
int a=numstudents+1;
if(x.length()>0) x=a+". " + x + "\n";
students[numstudents] = x;
if(x.length()>0)numstudents++;
for(int i=0; i<numstudents; i++){
x=students[i];
if(x.length()>13)x=x.substring(0,11)+"...\n";
y+=x;
}
studentbuttons[numstudents]=new JButton("Edit");
studentbuttons[numstudents].addActionListener(new grades());
delete[numstudents]=new JButton("Delete");
delete[numstudents].addActionListener(new del());
list.setText(y);
list.add(studentbuttons[numstudents]);
studentbuttons[numstudents].setBounds(100,(numstudents-1)*16,55,15);
list.add(delete[numstudents]);
delete[numstudents].setBounds(160,(numstudents-1)*16,70,15);
}
}
private class grades implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JButton){
JButton clicked = (JButton)obj;
int x=clicked.getY()/16;
String y=students[x];
for(int i=0; i<y.length(); i++){
String q=y.substring(i,i+1);
if(q.equals(" ")) {
y=y.substring(i+1);
i=y.length()+1;
}
}
studentframes[x]=new JFrame(y+"'s Grades");
studentframes[x].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{studentframes[x].getContentPane().add(new GradePage(x, tables[x], numass[x]));}
catch(Exception e){}
studentframes[x].pack();
studentframes[x].setVisible(true);
}
}
}
private class del implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JButton){
JButton clicked = (JButton)obj;
int x=clicked.getY()/16;
String y="", z="";
studentbuttons[numstudents].setVisible(false);
delete[numstudents].setVisible(false);
numstudents--;
int q=3;
int w=0;
for(int i=x; i<=numstudents; i++){
if(i<10)q=1;
else if(i<100) q=2;
tables[i]=tables[i+1];
numass[i]=numass[i+1];
model[i]=model[i+1];
w=i+1;
try{if(!students[i+1].equals(null)){students[i]=w+students[i+1].substring(q);} else{students[i]=null;}}catch(Throwable t){}
}
for(int i=0; i<numstudents; i++){
y=students[i];
if(y.length()>13)y=y.substring(0,11)+"...\n";
z+=y;
}
list.setText(z);
}
}
}
public static void setTable(int numtable, JTable table){
tables[numtable]=table;
}
public static void newRow(int numtable){
model[numtable].addRow(new Object[]{"", "", "", ""});
}
public static void setNumEntries(int numtable, int num){
numass[numtable]=num;
}
}
The third class shouldn't have anything to do with this so I wont post it for now.
I realize the code is probably poorly written but I'm only in my second year of High School computer science and we haven't actually covered any of this. This program wasn't even supposed to be a GUI, and this is the first time I've even heard of Input or Output streams so I really don't know anything about them. I realize now that having the classes implement java.io.Serializable was probably unnecessary but when I was trying to research this I came across someone taking about how some objects can't be saved because they don't naturally implement it. So sorry if it's some stupid mistake, and thanks for your time.
You can store the data in a notepad file like this
try
{
FileWriter file = new FileWriter( "insert file name here" );
BufferedWriter buffer = new BufferedWriter( file ) ;
buffer.write( "insert file content here" ) ;
buffer.newLine();
buffer.close();
}
catch ( IOException e )
{
//Insert error message here
}
And then read it like this
try
{
FileReader file = new FileReader( "insert file name here" ) ;
BufferedReader buffer = new BufferedReader( file );
String line = "" ;
while( ( line = buffer.readLine() ) != null )
{
System.out.println( line ) ;
}
buffer.close() ;
}
catch( IOException e )
{
//Insert error message here
}
Hope that helps
i dont recomend that you save JFrame object. here is a better solution you can save the students name and there grades on a text file and each time your program loads it will load the information from the text file.
Hint : you can use Input & Output streams dirctly but it's maybe a little bit sonfusing so go and read about these two classes :
Scanner (and how to use it to read from file)
PrintWriter (and how to use it to write to file)
P.S.: i could'v given you the code but it is much better learning experience to suffer a little bit to find information, because while you are trying to figure it out you will learn a lot of other things.
The frame is not an integral part of your application's state, it's just a way of displaying the important information (names, grades, etc). If you want to save your session, save the information the is currently on the screen and then re-create the JFrame with the deserialized information. An ObjectOutputStream is a reasonable way of writing things to disk, and and ObjectInputStream is good for reading the data back in. The documentation contains good examples. Also, your classes would need to implement Serializable.