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;
|
package userprofilebuilder.model;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.TitledBorder;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,64 +24,97 @@ public class UserGroup {
|
|||||||
String line = csvReader.readLine();
|
String line = csvReader.readLine();
|
||||||
values = line.split(",");
|
values = line.split(",");
|
||||||
|
|
||||||
for (int i =0; i < values.length; i=i+4)
|
for (int i = 0; i < values.length; i = i + 4) {
|
||||||
{
|
if (row == Integer.parseInt(String.valueOf(values[i].charAt(4)))) {
|
||||||
if ( row == Integer.parseInt(String.valueOf(values[i].charAt(4))) )
|
if (Pname.equals("User Title")) {
|
||||||
{
|
text = values[i + 1];
|
||||||
if (Pname.equals("User Title"))
|
|
||||||
{
|
|
||||||
text = values[i+1];
|
|
||||||
System.out.println("Set radio button text to :" + text);
|
System.out.println("Set radio button text to :" + text);
|
||||||
return text;
|
return text;
|
||||||
}
|
} else if (Pname.equals("User Name")) {
|
||||||
else if (Pname.equals("User Name")) {
|
text = values[i + 2];
|
||||||
text = values[i+2];
|
|
||||||
System.out.println("Set radio button text to :" + text);
|
System.out.println("Set radio button text to :" + text);
|
||||||
return text;
|
return text;
|
||||||
}
|
} else if (Pname.equals("User Email")) {
|
||||||
else if (Pname.equals("User Email")) {
|
text = values[i + 3];
|
||||||
text = values[i+3];
|
|
||||||
System.out.println("Set radio button text to :" + text);
|
System.out.println("Set radio button text to :" + text);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
System.out.println("error with reader");
|
System.out.println("error with reader");
|
||||||
}
|
}
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SaveFor(String file, JFrame viewer, int TotalRows)
|
public static void SaveFor(String file, JFrame viewer, int TotalRows) {
|
||||||
{
|
|
||||||
String[] values = null;
|
|
||||||
try (BufferedReader csvReader = new BufferedReader(new FileReader(file))) {
|
|
||||||
|
|
||||||
|
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();
|
JPanel mainPane = (JPanel) viewer.getContentPane();
|
||||||
JTabbedPane jt = (JTabbedPane) mainPane.getComponent(0);
|
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();
|
int count = 0;
|
||||||
values = line.split(",");
|
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
JPanel jp1 = (JPanel) jt.getComponentAt(i); // tab
|
while (reader.readLine() != null){
|
||||||
for (int rows = 0; rows < TotalRows; rows++){
|
count++;
|
||||||
JPanel jp2 = (JPanel) jp1.getComponent(rows);
|
|
||||||
JRadioButton jr1 = (JRadioButton) jp2.getComponent(0);
|
|
||||||
//System.out.println(jr1.getText());
|
|
||||||
values[i] = jr1.getText();
|
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
System.out.println("error with reader");
|
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<>();
|
public ArrayList<JRadioButton> radioButtons = new ArrayList<>();
|
||||||
int TotalRows;
|
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)
|
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
|
// setting the border title to the correct title
|
||||||
TitledBorder RowTitle;
|
TitledBorder RowTitle;
|
||||||
RowTitle = BorderFactory.createTitledBorder("User "+ row);
|
RowTitle = BorderFactory.createTitledBorder("User" + row);
|
||||||
RowPanel.setBorder(RowTitle);
|
RowPanel.setBorder(RowTitle);
|
||||||
// end of border title code
|
// end of border title code
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
User1,Dr,David Livingstone,d.livingstone@kingston.ac.uk
|
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
|
User3,HRH,DextroseDave,dex@twitter.com
|
||||||
|
Reference in new issue
Block a user