W3R3 – Add a Save File Option
This commit is contained in:
1 parent
2f87c1b848
commit
46bbc562bd
4 files changed
+75
-73
No files matched your search
@@ -5,7 +5,9 @@
|
||||
package userprofilebuilder.model;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.TitledBorder;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
@@ -22,64 +24,97 @@ public class UserGroup {
|
||||
String line = csvReader.readLine();
|
||||
values = line.split(",");
|
||||
|
||||
for (int i =0; i < values.length; i=i+4)
|
||||
{
|
||||
if ( row == Integer.parseInt(String.valueOf(values[i].charAt(4))) )
|
||||
{
|
||||
if (Pname.equals("User Title"))
|
||||
{
|
||||
text = values[i+1];
|
||||
for (int i = 0; i < values.length; i = i + 4) {
|
||||
if (row == Integer.parseInt(String.valueOf(values[i].charAt(4)))) {
|
||||
if (Pname.equals("User Title")) {
|
||||
text = values[i + 1];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
else if (Pname.equals("User Name")) {
|
||||
text = values[i+2];
|
||||
} else if (Pname.equals("User Name")) {
|
||||
text = values[i + 2];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
else if (Pname.equals("User Email")) {
|
||||
text = values[i+3];
|
||||
} else if (Pname.equals("User Email")) {
|
||||
text = values[i + 3];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
System.out.println("error with reader");
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public static void SaveFor(String file, JFrame viewer, int TotalRows)
|
||||
{
|
||||
String[] values = null;
|
||||
try (BufferedReader csvReader = new BufferedReader(new FileReader(file))) {
|
||||
public static void SaveFor(String file, JFrame viewer, int TotalRows) {
|
||||
|
||||
ArrayList<String> Tab1 = new ArrayList<>();
|
||||
ArrayList<String> Tab2 = new ArrayList<>();
|
||||
ArrayList<String> Tab3 = new ArrayList<>();
|
||||
|
||||
try {
|
||||
FileWriter wr = new FileWriter(file);
|
||||
|
||||
JPanel mainPane = (JPanel) viewer.getContentPane();
|
||||
JTabbedPane jt = (JTabbedPane) mainPane.getComponent(0);
|
||||
|
||||
for (int i=0; i<3; i++)
|
||||
// for tab 1:
|
||||
|
||||
JPanel jp1 = (JPanel) jt.getComponentAt(0); // tab
|
||||
for (int row = 0; row < TotalRows; row++) {
|
||||
JPanel jp2 = (JPanel) jp1.getComponent(row); // row
|
||||
JRadioButton jr1 = (JRadioButton) jp2.getComponent(0); // always stays 0 --> selecting radio button
|
||||
Tab1.add(jr1.getText());
|
||||
}
|
||||
|
||||
//for tab 2:
|
||||
JPanel jp1new = (JPanel) jt.getComponentAt(1); // tab
|
||||
for (int row = 0; row < TotalRows; row++) {
|
||||
JPanel jp2 = (JPanel) jp1new.getComponent(row); // row
|
||||
JRadioButton jr1 = (JRadioButton) jp2.getComponent(0); // always stays 0 --> selecting radio button
|
||||
Tab2.add(jr1.getText());
|
||||
}
|
||||
|
||||
//for tab 3:
|
||||
JPanel jp1new1 = (JPanel) jt.getComponentAt(2); // tab
|
||||
for (int row = 0; row < TotalRows; row++) {
|
||||
JPanel jp2 = (JPanel) jp1new1.getComponent(row); // row
|
||||
JRadioButton jr1 = (JRadioButton) jp2.getComponent(0); // always stays 0 --> selecting radio button
|
||||
Tab3.add(jr1.getText());
|
||||
}
|
||||
|
||||
for (int i = 0; i < Tab1.size(); i++) {
|
||||
JPanel jp2 = (JPanel) jp1.getComponent(i);
|
||||
TitledBorder title = (TitledBorder) jp2.getBorder();
|
||||
String UserProfile = title.getTitle();
|
||||
String line = UserProfile + "," + Tab1.get(i) + "," + Tab2.get(i) + "," + Tab3.get(i);
|
||||
wr.write(line);
|
||||
wr.write("\n");
|
||||
}
|
||||
|
||||
wr.flush();
|
||||
wr.close();
|
||||
System.out.println("Successfully wrote to the file.");
|
||||
} catch (IOException e) {
|
||||
System.out.println("An error occurred while saving.");
|
||||
}
|
||||
}
|
||||
|
||||
public static int getLine(String file)
|
||||
{
|
||||
String line = csvReader.readLine();
|
||||
values = line.split(",");
|
||||
int count = 0;
|
||||
|
||||
|
||||
JPanel jp1 = (JPanel) jt.getComponentAt(i); // tab
|
||||
for (int rows = 0; rows < TotalRows; rows++){
|
||||
JPanel jp2 = (JPanel) jp1.getComponent(rows);
|
||||
JRadioButton jr1 = (JRadioButton) jp2.getComponent(0);
|
||||
//System.out.println(jr1.getText());
|
||||
values[i] = jr1.getText();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
while (reader.readLine() != null){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
System.out.println("error with reader");
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,4 +32,8 @@ public class UserProfile {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -26,45 +26,8 @@ public class MainViewer {
|
||||
public ArrayList<JRadioButton> radioButtons = new ArrayList<>();
|
||||
int TotalRows;
|
||||
|
||||
public int a = 3; // this represents a value that is to declare how many rows are to be made -- to be changed by the amount of lines in csv. ROW COUNT
|
||||
public int a = UserGroup.getLine(file); // this represents a value that is to declare how many rows are to be made -- to be changed by the amount of lines in csv. ROW COUNT
|
||||
|
||||
public String Reader(int row, String Pname) {
|
||||
String text = "Default Text";
|
||||
String[] values = null;
|
||||
try (BufferedReader csvReader = new BufferedReader(new FileReader(file))) {
|
||||
while (csvReader.ready()) {
|
||||
String line = csvReader.readLine();
|
||||
values = line.split(",");
|
||||
|
||||
for (int i =0; i < values.length; i=i+4)
|
||||
{
|
||||
if ( row == Integer.parseInt(String.valueOf(values[i].charAt(4))) )
|
||||
{
|
||||
if (Pname.equals("User Title"))
|
||||
{
|
||||
text = values[i+1];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
else if (Pname.equals("User Name")) {
|
||||
text = values[i+2];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
else if (Pname.equals("User Email")) {
|
||||
text = values[i+3];
|
||||
System.out.println("Set radio button text to :" + text);
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("error with reader");
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
private void CreateRow(int count, JPanel jp, String Pname, ButtonGroup group)
|
||||
{
|
||||
@@ -78,7 +41,7 @@ public class MainViewer {
|
||||
|
||||
// setting the border title to the correct title
|
||||
TitledBorder RowTitle;
|
||||
RowTitle = BorderFactory.createTitledBorder("User "+ row);
|
||||
RowTitle = BorderFactory.createTitledBorder("User" + row);
|
||||
RowPanel.setBorder(RowTitle);
|
||||
// end of border title code
|
||||
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
User1,Dr,David Livingstone,d.livingstone@kingston.ac.uk
|
||||
User2,Mr,Dave Livingstone,dave@googlemail.com
|
||||
User2,Mr,bobby,dave@googlemail.com
|
||||
User3,HRH,DextroseDave,dex@twitter.com
|
||||
|
Reference in new issue
Block a user