JList Selection and Rectangles In New Frame
Hello,
I'v had a problem for the past day or so I cant figure out. What I want to do is select an item from a JList and depending on the item selection I want to draw 10 rectangles on a new JFrame. I have a class that draws the rectangles perfectly but what I want to do is only draw the rectangles when a list item is selected and I cannot get it to draw. Here is my code...
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import javax.swing.event.*;
publicclass LocationSelectionextends JFrameimplements ListSelectionListener
{
//Delcares the GUI objects
//StockControl stockControl;
JFrame frame;
ArrayList<String> allNames =new ArrayList<String>(50);
ArrayList<String> allLocations =new ArrayList<String>(50);
String section;
JList list;
private String nl="\n";
DefaultListModel model =new DefaultListModel();
JPanel panel;
privatefinalstaticlong serialVersionUID = 42;
/** Creates a new instance of EditUsers */
public LocationSelection()
{
//stockControl = sc;
init();
}
publicvoid init()
{
try
{
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
}
catch (Exception e)
{
}
//Creates the Frame
frame =new JFrame();
frame.setTitle("Edit current stock..." );
frame.setSize(300,192);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//Creates the JPanels and Scrollpane to house the items
panel =new JPanel();
panel.setLayout(new BorderLayout());
JPanel buttons =new JPanel();
//Adds componants to the JFrame
frame.add(panel);
//Sets the aesthetics of the JFrame
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension size = tk.getScreenSize();
frame.setLocation( size.width/2 - 120, size.height/2 - 120 );
frame.setVisible(true);
frame.setBackground( Color.gray );
getUserDetails();
}
//Puts a call to the database and returns all user details into array lists
publicvoid getUserDetails()
{
try
{
DatabaseConnection db =new DatabaseConnection();
db.start();
Statement stmt = db.connection.createStatement();
String sql ="Select * from t_stock";
Statement state = db.connection.createStatement();
ResultSet rs = state.executeQuery( sql );
int i = 0;
while (rs.next())
{
allNames.add(i,rs.getObject("STOCK_NAME").toString());
allLocations.add(i,rs.getObject("STOCK_LOCATION").toString());
model.addElement(allNames.get(i));
i++;
}
list =new JList(model);
list.setToolTipText("List Of All Stock Currently On The System");
list.addListSelectionListener(this);
JScrollPane jsp =new JScrollPane(list);
panel.add(jsp, BorderLayout.WEST);
panel.validate();
db.connection.close();
pack();
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
}
}
publicvoid valueChanged(ListSelectionEvent e)
{
if (list.isSelectionEmpty() ==false)
{
int stock = list.getSelectedIndex();
String temp = allLocations.get(stock).toString();
repaint();
}
}
publicvoid paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.blue);
if(section.equals("1A"))
{
//Section 1A
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(5, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 1A", 5, 10);
}
else
{
//Section 1A
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(5, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 1A", 5, 10);
}
if(section.equals("1B"))
{
//Section 1B
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(5, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 1B", 5, 222);
}
else
{
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(5, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 1B", 5, 222);
}
if(section.equals("2A"))
{
//Section 2A
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(206, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 2A", 206, 10);
}
else
{
//Section 2A
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(206, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 2A", 206, 10);
}
if(section.equals("2B"))
{
//Section 2B
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(206, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 2B", 206, 222);
}
else
{
//Section 2B
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(206, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 2B", 206, 222);
}
if(section.equals("3A"))
{
//Section 3A
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(407, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 3A", 407, 10);
}
else
{
//Section 3A
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(407, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 3A", 407, 10);
}
if(section.equals("3B"))
{
//Section 3B
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(407, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 3B", 407, 222);
}
else
{
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(407, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 3B", 407, 222);
}
if(section.equals("4A"))
{
//Section 4A
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(608, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 4A", 608, 10);
}
else
{
//Section 4A
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(608, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 4A", 608, 10);
}
if(section.equals("4B"))
{
//Section 4B
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(608, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 4B", 608, 222);
}
else
{
//Section 4B
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(608, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 4B", 608, 222);
}
if(section.equals("5A"))
{
//Section 5A
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(809, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 5A", 809, 10);
}
else
{
//Section 5A
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(809, 12, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 5A", 809, 10);
}
if(section.equals("5B"))
{
//Section 5B
g.setColor(Color.red);
g2.fill(new Rectangle2D.Double(809, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 5B", 809, 222);
}
else
{
//Section 5B
g.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(809, 223, 200, 200));
g.setColor(Color.black);
g2.drawString("Section 5B", 809, 222);
}
}
publicstaticvoid main (String args[])
{
LocationSelection ls =new LocationSelection();
}
}
I wish to add the newly drawn rectangles to a new frame and place it in beside the JList of items and repaint every time a new ListItem is selected, but for some reason it wont work. Any ideas?
P.S The code above uses a call to a MySql database so it wont run for you, but Im hoping its a simple work around so it wont have to be ran
Thanks in advance

