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

[14965 byte] By [yelnahs@gmail.coma] at [2007-10-2 18:58:21]
# 1
Maybe you meant for this lineString temp = allLocations.get(stock).toString();in "valueChanged" to look like thissection = allLocations.get(stock).toString();
74philipa at 2007-7-13 20:36:49 > top of Java-index,Security,Cryptography...
# 2

Ah good spot, sorry I have so many versions of the file I dont know where to look. What I did was hard code 'section' to equal "1A" so the valueChanged method looks like this

public void valueChanged(ListSelectionEvent e)

{

if (list.isSelectionEmpty() == false)

{

section = "1A";

repaint();

}

}

but when a new list item is selected nothing is drawn. Any more ideas?

yelnahs@gmail.coma at 2007-7-13 20:36:49 > top of Java-index,Security,Cryptography...
# 3

Your app extends JFrame and then turns around and instantiates and shows a new instance of

JFrame. You only need to do it one way. JFrame, the enclosing class, does not have a

"paintComponent" method. JComponent does but the signature is different. So no one was

calling your "paintComponent" method since it didn't show up in the component hierarchy.

I added an inner class that could host the "paintComponent" method and it renders now. Doing

flexible paint/rendering which resizes with the parent container is much more fun than

hard-coding everything. Get the width and height of the component, ie,"panel", figure out

how wide each cell can be and draw them. They will always be the correct size. As-is, the

painted cells overlap the JList on my screen.

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

public class LS implements ListSelectionListener

{

String section = "hello world";

JList list;

private String nl = "\n";

DefaultListModel model = new DefaultListModel();

private final static long serialVersionUID = 42;

public LS()

{

init();

}

public void init()

{

//Creates the Frame

JFrame frame = new JFrame();

frame.setTitle( "Edit current stock..." );

frame.setSize(300,192);

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//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.setBackground( Color.gray );

getUserDetails(frame);

frame.setVisible(true); // call this last

}

public void getUserDetails(JFrame frame)

{

for(int j = 1; j <= 5; j++)

{

model.addElement(String.valueOf(j) + "A");

model.addElement(String.valueOf(j) + "B");

}

list = new JList(model);

list.setToolTipText("List Of All Stock Currently On The System");

list.addListSelectionListener(this);

JScrollPane jsp = new JScrollPane(list);

frame.add(jsp, BorderLayout.WEST);

}

public void valueChanged(ListSelectionEvent e)

{

if (!list.isSelectionEmpty())

{

section = (String)list.getSelectedValue();

panel.repaint();

}

}

private JPanel panel = new JPanel()

{

protected void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

g.setColor(Color.blue);

if(section.equals("1A"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

else

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"))

g.setColor(Color.red);

else

g.setColor(Color.blue);

g2.fill(new Rectangle2D.Double(809, 223, 200, 200));

g.setColor(Color.black);

g2.drawString("Section 5B", 809, 222);

}

};

public static void main (String args[])

{

new LS();

}

}

74philipa at 2007-7-13 20:36:49 > top of Java-index,Security,Cryptography...
# 4

I realise there is a much better way to code this but this is just a tiny section of a big project so I didnt really want to put much work into it!

It now works perfectly, thanks so much. I had them hard coded as an example, but they are done alot more efficiently in the "real" code. Thanks a million for your help and enjoy your dukes.

P.s Do you by any chance know of a way to change my username? I just cant find where to change it, I dont like having my e-mail address as my user name!

yelnahs@gmail.coma at 2007-7-13 20:36:49 > top of Java-index,Security,Cryptography...