This is my final project for my second Java class! I would like to get some feedback on it to see where and what I can improve.
There are five class files:
GuiMusic
This contains all all of the the Swing and AWT components and the key listeners.
ScrollPaneSync
Synchronizes the scroll bars on each
JPanel
.Stringbillder
Manipulates result set data into a different form so that it can be set onto
JTextField
s.SqlCon
Contains the connection and the methods for sending queries.
UpdateQuery
This is another data manipulation class used for insert and delete queries.
GuiMusic
I'm open to any critisim such that it's constructive but I have some specific questions
How would you split the class to make it smaller?
A lot of the
ActionListeners
have very similiar functionality, is it better to reuse them with different sources and expand the class or should everything have its own listener?I avoided some code repetition but I'm not sure that that's a good thing when you it might slow your program down.
Could generics improve the flexibility of my code?
package guiMusic;
public class GuiMusic extends JFrame
{
private static final long serialVersionUID = 1L;
/**
* @conn
* object represents the connection to the database
*/
SqlCon conn;
/**
* @stmt
* used by conn to process sql query results
*/
Statement stmt;
ResultSet rs;
/**
* @sb
* StringBuilder used to manipulate query data
*/
StringBillder sb;
/**
* @formatedData
* used to store information from textAreas and pass to conn
*/
StringBuilder[] formatedData;
/**
* @addInfo
* after a query from the add tab things are the information is stored in addinfo
*/
String[] addInfo;
/**
* @sync
* class that synchronizes scrollbars together
*/
ScrollPaneSync sync;
/**
* @query
* pretty self explanatory
*/
String query;
/**
* @search
* @searchBar
* @searchLabel
* @searchText
* majority of the objects needed for
* the creation of the search panel
*/
JPanel search;
JTextField searchBar;
JTextField[] searchLabel = new JTextField[4];
JTextArea[] searchText = new JTextArea[4];
Font tmu = new Font("Tibetan Machine Uni",1, 20);
Font tmus = new Font("Tibetan Machine Uni",1,15);
String[] labelQueries= {
"select * from info where %s like '%%%s%%'",
"select * from info where %s like '%%%s%%' && %s like '%%%s%%'",
"select * from info where %s like '%%%s%%' && %s like '%%%s%%' && %s like '%%%s%%'",
"select * from info where %s like '%%%s%%' && %s like '%%%s%%' && %s like '%%%s%%' && %s like '%%%s%%'"};
/**
* @add
* @addSubmit
* @addClear
* @addSearch
* @addLabel
* @addText
* majority of objects needed for
* the creation of add panel
*/
JPanel add;
JButton addSubmit;
JButton addClear;
JTextField addSearch;
JTextField[] addLabel = new JTextField[3];
JTextArea[] addText = new JTextArea[3];
/**
* @remove
* @remSubmit
* @remClear
* @remSearch
* @remLabel
* @remText
* components needed for creation of
* the remove tab
*/
JPanel remove;
JButton remSubmit;
JButton remClear;
JTextField remSearch;
JTextField[] remLabel = new JTextField[4];
JTextArea[] remText = new JTextArea[4];
/**
* @labelValues
* used throughout program to make associations
* and assign values
*
*/
public final String[] LABELVALUES = {"artist" ,"album" ,"song", "genre"};
GuiMusic() throws ClassNotFoundException, SQLException
{
formatedData = new StringBuilder[4];
for(int i = 0; i<4; i++)
{
formatedData[i] = new StringBuilder();
}
addInfo = new String[4];
conn = new SqlCon();
sb = new StringBillder();
setBounds(200, 100, 1500, 900);
JTabbedPane main = new JTabbedPane();
add(main);
main.setFont(tmu);
main.addTab("<html><body marginwidth=200 marginheight=10>Search</body></html>", createSearch());
main.addTab("<html><body marginwidth=200 marginheight=10>Add</body></html>", createAdd());
main.addTab("<html><body marginwidth=200 marginheight=10>Remove</body></html>", createRemove());
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e)
{
try
{
conn.clothes();
} catch (SQLException e1) {e1.printStackTrace();}
}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowOpened(WindowEvent e) {}
});
}
private JPanel createSearch()
{
search = new JPanel();
searchBar = new JTextField("search");
searchBar.setHorizontalAlignment(SwingConstants.CENTER);
searchBar.setPreferredSize(new Dimension(1300,60));
searchBar.setFont(tmu);
searchBar.setName("searchBar");
searchBar.addKeyListener(new SearchBarListener());
search.add(searchBar);
JScrollPane[] scroll = new JScrollPane[4];
for(int i = 0; i<4; i++)
{
searchLabel[i] = new JTextField(LABELVALUES[i], SwingConstants.CENTER);
searchLabel[i].setFont(tmu);
searchLabel[i].setName(LABELVALUES[i]);
searchLabel[i].addKeyListener( new textFieldListener());
searchLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
searchLabel[i].setPreferredSize(new Dimension(325,60));
searchText[i] = new JTextArea();
searchText[i].setFont(tmus);
}
for(JTextField el : searchLabel)
{
search.add(el);
}
for(int i = 0; i<4; i++)
{
scroll[i] = new JScrollPane(searchText[i]);
scroll[i].setVerticalScrollBar(new JScrollBar());
scroll[i].setPreferredSize(new Dimension(325,600));
search.add(scroll[i]);
}
sync = new ScrollPaneSync(scroll);
for(int i = 0; i<scroll.length; i++)
{
scroll[i].getVerticalScrollBar().addAdjustmentListener(sync);
}
return search;
}
private JPanel createAdd()
{
add = new JPanel();
add.setLayout(new FlowLayout());
addClear = new JButton(("Clear"));
addClear.setFont(tmu);
addClear.setPreferredSize(new Dimension(220,60));
addClear.setName("addClear");
addClear.addActionListener(new buttonListener());
addSubmit = new JButton(("Submit"));
addSubmit.setFont(tmu);
addSubmit.setPreferredSize(new Dimension(220,60));
addSubmit.setName("addSubmit");
addSubmit.addActionListener(new buttonListener());
addSearch = new JTextField("Artist");
addSearch.setFont(tmu);
addSearch.setPreferredSize(new Dimension(1000,60));
addSearch.setHorizontalAlignment(SwingConstants.CENTER);
addSearch.setName("addSearch");
addSearch.addKeyListener(new addSearchListener());
add.add(addSubmit);
add.add(addSearch);
add.add(addClear);
JScrollPane[] addScroll = new JScrollPane[3];
for(int i = 0; i<3; i++)
{
addLabel[i] = new JTextField(LABELVALUES[i+1]);
addLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
addLabel[i].setPreferredSize(new Dimension(450,100));
addLabel[i].setFont(tmu);
addLabel[i].setName(LABELVALUES[i+1]);
addLabel[i].addKeyListener(new addFieldListener());
add.add(addLabel[i]);
addText[i] = new JTextArea();
addText[i].setFont(tmus);
}
for(int i = 0; i<3; i++)
{
addScroll[i] = new JScrollPane(addText[i]);
addScroll[i].setVerticalScrollBar(new JScrollBar());
addScroll[i].setPreferredSize(new Dimension(450,500));
add.add(addScroll[i]);
}
sync = new ScrollPaneSync(addScroll);
for(int i = 0; i<addScroll.length; i++)
{
addScroll[i].getVerticalScrollBar().addAdjustmentListener(sync);
}
return add;
}
private JPanel createRemove()
{
remove = new JPanel();
remSubmit = new JButton("Remove");
remSubmit.setFont(tmu);
remSubmit.setPreferredSize(new Dimension(220,60));
remSubmit.addActionListener(new buttonListener());
remSearch = new JTextField("Search Term",SwingConstants.CENTER);
remSearch.setHorizontalAlignment(SwingConstants.CENTER);
remSearch.setPreferredSize(new Dimension(1000,60));
remSearch.setFont(tmu);
remSearch.setName("remSearch");
remSearch.addKeyListener(new remBarListener());
remClear = new JButton("Clear");
remClear.setFont(tmu);
remClear.setPreferredSize(new Dimension(220,60));
remClear.addActionListener(new buttonListener());
remove.add(remSubmit);
remove.add(remSearch);
remove.add(remClear);
JScrollPane[] remScroll = new JScrollPane[4];
for(int i =0; i<4; i++)
{
remLabel[i] = new JTextField(LABELVALUES[i]);
remLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
remLabel[i].setPreferredSize(new Dimension(325,100));
remLabel[i].setName(LABELVALUES[i]);
remLabel[i].setFont(tmu);
remLabel[i].addKeyListener(new textFieldListener());
remove.add(remLabel[i]);
remText[i]= new JTextArea();
remText[i].setFont(tmus);
remScroll[i] = new JScrollPane(remText[i]);
remScroll[i].setPreferredSize(new Dimension(325,600));
}
for(int i = 0; i<4; i++)
{
remove.add(remScroll[i]);
}
sync = new ScrollPaneSync(remScroll);
for(int i = 0; i<remScroll.length; i++)
{
remScroll[i].getVerticalScrollBar().addAdjustmentListener(sync);
}
return remove;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
for(int i = 0; i<10; i++)
{
System.out.println("indie");
}
System.out.println();
for(int i = 0; i<10; i++)
{
System.out.println("XO");
}
new GuiMusic().setVisible(true);
}
public class SearchBarListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
query = searchBar.getText();
query = String.format("select * from info where album like '%%%s%%' || artist like '%%%s%%' || song like '%%%s%%'|| genre like '%%%s%%'",query,query,query,query);
try
{
rs = conn.sqlQuery(query);
for(int i = 0; i<4; i++)
{
formatedData[i].setLength(0);
}
formatedData = sb.formatData(rs);
} catch (SQLException ef) {ef.printStackTrace();}
searchText[0].setText(formatedData[0].toString());
searchText[1].setText(formatedData[1].toString());
searchText[2].setText(formatedData[2].toString());
searchText[3].setText(formatedData[3].toString());
}
@Override
public void keyTyped(KeyEvent e) {}
}
public class remBarListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
query = remSearch.getText();
query = String.format("select * from info where album like '%%%s%%' || artist like '%%%s%%' || song like '%%%s%%'|| genre like '%%%s%%'",query,query,query,query);
try
{
rs = conn.sqlQuery(query);
for(int i = 0; i<4; i++)
{
formatedData[i].setLength(0);
}
formatedData = sb.formatData(rs);
} catch (SQLException ef) {ef.printStackTrace();}
remText[0].setText(formatedData[0].toString());
remText[1].setText(formatedData[1].toString());
remText[2].setText(formatedData[2].toString());
remText[3].setText(formatedData[3].toString());
}
@Override
public void keyTyped(KeyEvent e) {}
}
public class textFieldListener implements KeyListener
{
String queryLocation;
boolean[] validTextonLabel = new boolean[4];
String getText;
int numberOfTrue = 0;
ArrayList<Integer> trueIndex = new ArrayList<>();
boolean search = false;
boolean remove = false;
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
for(int i = 0; i<4; i++)
{
if(e.getSource().equals(remLabel[i]) || e.getSource().equals(remSearch))
{
remove = !remove;
System.out.println("work");
}
else if(e.getSource().equals(searchLabel[i]))
search = !search;
}
for(int q = 0; q <4; q++)
{
if(search)
{
getText = searchLabel[q].getText();
}
else if(remove)
{
getText = remLabel[q].getText();
}
if(!(getText.equals(null)) && !(getText.equals(LABELVALUES[q])) && !(getText.equals("")) && !(getText.equals(" ")))
{
validTextonLabel[q] = true;
addInfo[q] = getText;
trueIndex.add(q);
numberOfTrue++;
}
}
if(numberOfTrue == 4)
{
query = String.format(labelQueries[3],LABELVALUES[0],addInfo[0],LABELVALUES[1],addInfo[1],LABELVALUES[2],addInfo[2],LABELVALUES[3],addInfo[3]);
}
else if(numberOfTrue == 3 )
{
query = String.format(labelQueries[2],LABELVALUES[trueIndex.get(0)],addInfo[trueIndex.get(0)],LABELVALUES[trueIndex.get(1)],addInfo[trueIndex.get(1)],LABELVALUES[trueIndex.get(2)],addInfo[trueIndex.get(2)]);
}
else if(numberOfTrue == 2)
{
query = String.format(labelQueries[1], LABELVALUES[trueIndex.get(0)],addInfo[trueIndex.get(0)],LABELVALUES[trueIndex.get(1)],addInfo[trueIndex.get(1)]);
}
else if(numberOfTrue == 1)
{
query = String.format(labelQueries[0], LABELVALUES[trueIndex.get(0)],addInfo[trueIndex.get(0)]);
}
else if(e.getSource().equals(remSearch))
{
System.out.println("true");
}
else
System.out.println("false");
System.out.println(query);
try
{
rs = conn.sqlQuery(query);
for(int i = 0; i<4; i++)
{
formatedData[i].setLength(0);
}
formatedData = sb.formatData(rs);
} catch (SQLException ef) {ef.printStackTrace();}
if(search)
{
searchText[0].setText(formatedData[0].toString());
searchText[1].setText(formatedData[1].toString());
searchText[2].setText(formatedData[2].toString());
searchText[3].setText(formatedData[3].toString());
}
else if(remove);
{
remText[0].setText(formatedData[0].toString());
remText[1].setText(formatedData[1].toString());
remText[2].setText(formatedData[2].toString());
remText[3].setText(formatedData[3].toString());
}
trueIndex.clear();
numberOfTrue = 0;
for(int i = 0; i<4; i++)
{
validTextonLabel[i] = false;
}
search = false;
remove = false;
}
@Override
public void keyTyped(KeyEvent e) {}
}
public class addFieldListener implements KeyListener
{
String queryLocation;
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
queryLocation = e.getComponent().getName();
for(int i = 0; i<3; i++)
{
if(queryLocation.equals(LABELVALUES[i+1]))
{
query = String.format("select * from info where %s like '%%%s%%'",LABELVALUES[i+1],addLabel[i].getText());
}
}
try
{
rs = conn.sqlQuery(query);
for(int i = 0; i<4; i++)
{
formatedData[i].setLength(0);
}
formatedData = sb.formatData(rs);
} catch (SQLException ef) {ef.printStackTrace();}
addText[0].setText(formatedData[1].toString());
addText[1].setText(formatedData[2].toString());
addText[2].setText(formatedData[3].toString());
}
@Override
public void keyTyped(KeyEvent e) {}
}
public class addSearchListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e)
{
query = addSearch.getText();
query = String.format("select * from info where album like '%%%s%%' || artist like '%%%s%%' || song like '%%%s%%'|| genre like '%%%s%%'",query,query,query,query);
try {
rs = conn.sqlQuery(query);
for(int i = 0; i<4; i++)
{
formatedData[i].setLength(0);
}
formatedData = sb.formatData(rs);
} catch (SQLException ef) {ef.printStackTrace();}
addText[0].setText(formatedData[1].toString());
addText[1].setText(formatedData[2].toString());
addText[2].setText(formatedData[3].toString());
}
@Override
public void keyTyped(KeyEvent e) {}
}
public class buttonListener implements ActionListener
{
String[][] addReturn;
UpdateQuery uq;
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(addClear))
{
addSearch.setText("");
for(int i = 0; i<3; i++)
{
addText[i].setText("");
}
}
else if(ae.getSource().equals(remClear))
{
remSearch.setText("");
for(int i = 0; i<4; i++)
{
remText[i].setText("");
}
}
else if(ae.getSource().equals(addSubmit))
{
addInfo[0] = addSearch.getText();
for(int i = 1; i<4; i++)
{
addInfo[i] = addText[i-1].getText();
}
uq = new UpdateQuery();
addReturn = uq.sqlUpdate(addInfo);
for(int i = 0; i< addReturn[1].length; i++)
{
query = String.format("insert into info values('%s','%s','%s','%s')", addReturn[0][0],addReturn[1][i],addReturn[2][i],addReturn[3][i]);
try
{
conn.executeUpdate(query);
} catch (SQLException ef) {ef.printStackTrace();}
}
}
else if(ae.getSource().equals(remSubmit))
{
for(int i = 0; i<4; i++)
{
addInfo[i] = remText[i].getText();
}
uq = new UpdateQuery();
addReturn = uq.sqlUpdate(addInfo);
for(int i = 0; i< addReturn[1].length; i++)
{
query = String.format("delete from info where song = '%s'",addReturn[2][i]);
try
{
conn.executeUpdate(query);
} catch (SQLException ef) {ef.printStackTrace();}
}
}
}
}
}
ScrollPaneSync
- Readability?
package guiMusic;
public class ScrollPaneSync implements AdjustmentListener
{
JScrollBar[] bars;
ScrollPaneSync(JScrollPane...scrolls)
{
bars = new JScrollBar[scrolls.length];
int count = 0;
for(int i = 0; i<scrolls.length; i++)
{
bars[count] = scrolls[i].getVerticalScrollBar();
count++;
}
}
@Override
public void adjustmentValueChanged(AdjustmentEvent e)
{
JScrollBar scrollBar = (JScrollBar)e.getSource();
for(int i = 0; i< bars.length; i++)
{
bars[i].setValue(scrollBar.getValue());
}
}
}
StringBillder
class StringBillder {
private StringBuilder[] theReturn;
private ArrayList<String> artVals;
private ArrayList<String> albVals;
private ArrayList<String> traVals;
private ArrayList<String> genVals;
StringBillder()
{
theReturn = new StringBuilder[4];
for(int i = 0; i< theReturn.length;i++)
{
theReturn[i] = new StringBuilder();
}
artVals = new ArrayList<>();
albVals = new ArrayList<>();
traVals = new ArrayList<>();
genVals = new ArrayList<>();
}
public StringBuilder[] formatData(ResultSet rs)
{
artVals.clear();
albVals.clear();
traVals.clear();
genVals.clear();
try
{
while(rs.next())
{
artVals.add(rs.getString(1));
albVals.add(rs.getString(2));
traVals.add(rs.getString(3));
genVals.add(rs.getString(4));
}
} catch (SQLException e) {e.printStackTrace();}
for(String el : artVals)
{
theReturn[0].append(el).append("\n");
}
for(String el: albVals)
{
theReturn[1].append(el).append("\n");
}
for(String el : traVals)
{
theReturn[2].append(el).append("\n");
}
for(String el : genVals)
{
theReturn[3].append(el).append("\n");
}
return theReturn;
}
}
SqlCon
public class SqlCon {
private static final String dbClassName = "com.mysql.jdbc.Driver";
private static final String CONNECTION = "jdbc:mysql://this.is.a.ip/music";
Connection con;
Statement stmt = null;
ResultSet rs;
SqlCon() throws ClassNotFoundException, SQLException
{
Class.forName(dbClassName);
Properties p = new Properties();
p.put("user", "foo");
p.put("password", "bar");
con = DriverManager.getConnection(CONNECTION,p);
}
public void executeUpdate(String query) throws SQLException
{
stmt = con.createStatement();
stmt.executeUpdate(query);
}
public ResultSet sqlQuery(final String query) throws SQLException
{
stmt = con.createStatement();
rs = stmt.executeQuery(query);
return rs;
}
public void clothes() throws SQLException
{
if(con != null )
con.close();
if(stmt != null)
stmt.close();
if(rs != null)
rs.close();
}
}
UpdateQuery
public class UpdateQuery
{
String[][] formattedReturn;
String[] queriedInfo;
UpdateQuery()
{
queriedInfo = new String[4];
formattedReturn = new String[4][];
}
public String[][] sqlUpdate(String[] arg)
{
queriedInfo = arg;
for(int column = 0; column<4;column++)
{
formattedReturn[column] = queriedInfo[column].split("\\n");
}
return formattedReturn;
}
}
addLabel
, i think i,j,k would be a better start. Finally i decide not to answer because it would be Sisyphean labor. \$\endgroup\$StringBillder
intentional so that it doesn't conflict with that ofStringBuilder
? I think you need a much better class name here... \$\endgroup\$