Help with JScrollPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help with JScrollPane

  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Help with JScrollPane

    Hi guys,

    Im new at this forum. Im really a newb regarding java but I think my question is advanced enough to be posten in this topic rather than the new to java topic.

    Try my code below. Whenever you click an item in the rightmost list 1 or more grafs are drawn (based on random values in this example). Now since the class GrafPanel (where the grafs are drawn) changes size to that the grafs will fit, the ScrollPane also has to be revalidated. However, no matter how I try I always get the old "preferredSize" from the GrafPanel so that the ScrollPane is always adapted for the previous graf. How would I change my code so that it is sized for the graf show at the moment?
    _______________________
    Karl XII King Of Sweden

  2. #2
    Join Date
    Oct 2003
    Posts
    3
    here is the code(perfectly runnable):

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.Insets.*;
    import javax.swing.event.*;
    import java.util.regex.*;
    import java.awt.geom.*;
    import java.lang.Math.*;

    public class GUI extends JFrame implements EventListener {

    JList id;
    JList testData;
    DefaultListModel idModel;
    DefaultListModel testModel;
    Color grey = new Color(210,210,200);
    GrafPanel display;
    JScrollPane dataScrollPane;
    JScrollPane grafScrollPane;
    JScrollPane idScrollPane;
    boolean headlinesLoaded= false;
    int[]lastSelectedIndices;
    boolean firstTime = true;


    public static void main( String[] params) {
    new GUI();
    }

    public GUI() {
    super("List IdNummer");
    setLocation(50,50);
    setSize(950, 590);
    validate();
    getContentPane().setLayout(new BorderLayout());



    JPanel leftPanel = new JPanel();
    leftPanel.setLayout( new GridLayout());
    getContentPane().add(leftPanel, BorderLayout.WEST);




    display = new GrafPanel();

    display.setForeground(Color.white);
    //display.setPreferredSize(new Dimension(800, 590));
    getContentPane().add(display, BorderLayout.CENTER);

    grafScrollPane = new JScrollPane(display,
    JScrollPane.VERTICAL_SCROLLBAR_NEVER,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    grafScrollPane.setPreferredSize(new Dimension(500, 590));
    grafScrollPane.getHorizontalScrollBar().setUnitIncrement(10);
    getContentPane().add(grafScrollPane, BorderLayout.CENTER);




    GridBagLayout gridbag = new GridBagLayout();
    leftPanel.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();
    Insets anInsets = new Insets(1,1,1,1);
    c.fill = GridBagConstraints.BOTH;
    c.insets = anInsets;
    c.gridx=0;
    c.gridy=0;

    JLabel idLabel = new JLabel("ID Nummer:");
    idLabel.setPreferredSize(new Dimension(85, 25));
    gridbag.setConstraints(idLabel, c);
    leftPanel.add(idLabel);
    c.gridx=1;
    c.gridy=0;

    JLabel testLabel = new JLabel("Resultat:");
    testLabel.setPreferredSize(new Dimension(85, 25));
    gridbag.setConstraints(testLabel, c);
    leftPanel.add(testLabel);



    testModel = new DefaultListModel();
    testData = new JList(testModel);
    testData.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    testData.setLayoutOrientation(JList.VERTICAL);
    testData.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent event) {
    if(!id.getValueIsAdjusting()) {
    if( testData.getSelectedIndices().length > 1)
    if(testData.getSelectedIndices().length > 3) {
    lastSelectedIndices = new int[testData.getSelectedIndices().length];
    lastSelectedIndices = testData.getSelectedIndices();
    int[] newSelection = new int[lastSelectedIndices.length-1];
    for( int i=0; i < newSelection.length; i++){
    newSelection[i] = lastSelectedIndices[i];
    }
    testData.setSelectedIndices(newSelection);
    }


    if(id.getSelectedIndices().length > 1)
    if(testData.getSelectedIndices().length > 1 || testData.getSelectedIndices().length > 3) {
    lastSelectedIndices = new int[testData.getSelectedIndices().length];
    lastSelectedIndices = testData.getSelectedIndices();
    int[] newSelection = new int[lastSelectedIndices.length-1];
    for( int i=0; i < newSelection.length; i++){
    newSelection[i] = lastSelectedIndices[i];
    }
    testData.setSelectedIndices(newSelection);
    }
    if ( testData.getSelectedIndex() > -1 || testData.getSelectedIndices().length <= 3) {
    if(!testData.getValueIsAdjusting() && headlinesLoaded)
    setSelectedData();
    headlinesLoaded = true;
    }

    System.out.println("PrefSize i listlistenern: " +display.getGrafPanelSize());
    display.setPreferredSize(display.getGrafPanelSize());
    grafScrollPane.revalidate();

    }
    }
    });


    String[] idNummer = getID();


    idModel = new DefaultListModel();
    for (int i = 0; i < idNummer.length; i ++) {
    idModel.addElement(idNummer[i]);
    }
    _______________________
    Karl XII King Of Sweden

  3. #3
    Join Date
    Oct 2003
    Posts
    3
    id = new JList(idModel);
    id.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    id.setLayoutOrientation(JList.VERTICAL);
    id.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent event) {
    if( id.getSelectedValue() != null) {
    if(!id.getValueIsAdjusting()) {
    if (firstTime) {
    testModel.removeAllElements();
    readHeadlines((String)id.getSelectedValue());
    firstTime = false;
    }
    }
    }

    if( id.getSelectedIndices().length > 1)
    if(id.getSelectedIndices().length > 3) {
    lastSelectedIndices = new int[id.getSelectedIndices().length];
    lastSelectedIndices = id.getSelectedIndices();
    int[] newSelection = new int[lastSelectedIndices.length-1];
    for( int i=0; i < newSelection.length; i++){
    newSelection[i] = lastSelectedIndices[i];
    }
    id.setSelectedIndices(newSelection);
    }

    if(testData.getSelectedIndices().length > 1)
    if(id.getSelectedIndices().length > 1) {
    lastSelectedIndices = new int[id.getSelectedIndices().length];
    lastSelectedIndices = id.getSelectedIndices();
    int[] newSelection = new int[lastSelectedIndices.length-1];
    for( int i=0; i < newSelection.length; i++){
    newSelection[i] = lastSelectedIndices[i];
    }
    id.setSelectedIndices(newSelection);
    }
    }
    });



    c.fill = GridBagConstraints.BOTH;
    c.gridx=0;
    c.gridy=1;
    c.weightx = 0.4;
    c.weighty = 1.0;

    idScrollPane = new JScrollPane(id,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    idScrollPane.setPreferredSize(new Dimension(100, 590));


    gridbag.setConstraints(id, c);
    //leftPanel.add(id);
    gridbag.setConstraints(idScrollPane, c);
    leftPanel.add(idScrollPane);


    c.gridx=1;
    c.gridy=1;
    c.weightx = 1.0;
    c.weighty = 1.0;


    gridbag.setConstraints(testData, c);

    dataScrollPane = new JScrollPane(testData,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    dataScrollPane.setPreferredSize(new Dimension(200, 590));

    c.gridx=1;
    c.gridy=1;
    c.gridwidth=1;
    c.gridheight= 1;
    gridbag.setConstraints(dataScrollPane, c);
    leftPanel.add(dataScrollPane);


    setVisible(true);
    }



    void readHeadlines(String selectedValue){

    for(int i =0; i<10; i++)
    testModel.addElement("Item" + i);

    testData.setModel(testModel);
    }

    void setSelectedData() {
    String arrArr[][] = new String[3][];

    for (int l = 0; l < 3; l++) {
    double b = 20*Math.random();
    int a = (int)b;
    arrArr[l] = new String[a];
    }
    double a = 0;


    for( int i=0; i<3; i++)
    for(int p=0; p < arrArr[i].length; p++) {
    a = p*Math.random();
    arrArr[i][p] = ( " " + a);

    }


    display.renderSomething(arrArr);
    }




    String[] getID() {

    String[] array = new String[10];
    for(int i =0; i< 10; i++)
    array[i] = "Item " + i;

    return array;
    }
    }

    class MyWinlis extends WindowAdapter {
    public void windowClosing( WindowEvent we){
    System.exit(0);
    }
    }

    class GrafPanel extends JPanel /*implements Scrollable */{

    float x;
    String[] gNames = new String[3];
    float[][] y = new float[2][10];
    boolean called = false;
    int limit = 265;
    int maxTests;
    Dimension prefSize = new Dimension();
    int callNumber = 0;

    public GrafPanel(){
    //setSize(1000, 590);
    }

    public void paint(Graphics g) {
    System.out.println("Call nr:" + callNumber++);
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(Color.red);
    float dash1[] = {10.0f};
    BasicStroke dashed = new BasicStroke(1.0f,
    BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_MITER,
    10.0f, dash1, 0.0f);

    Color[] colors = new Color[]{ Color.blue, Color.red, Color.yellow};




    Color dullGrey = new Color(140,140,140);
    g2.setPaint(dullGrey);
    g2.setStroke(dashed);
    int nextText=12;
    int starty=65;
    for(int m=0; m <maxTests+2; m++) {
    g2.draw(new Line2D.Double(starty , 40, starty, 500));
    starty +=50;
    g2.setPaint(Color.black);
    g2.drawString(String.valueOf(m+1), nextText+50, 30);
    nextText +=50;
    g2.setPaint(dullGrey);

    }

    if( maxTests > 0)
    prefSize.setSize(50*maxTests +200,590);
    else
    prefSize.setSize(400, 590);

    this.setPreferredSize(prefSize);


    g2.setPaint(dullGrey);
    starty = 65;
    for(int m=0; m <9; m++) {
    g2.draw(new Line2D.Double(40 , starty, 50*maxTests +150, starty));
    starty +=50;
    }
    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(2.0f));



    if (called) {
    x = 15;
    float oldy = 215;
    for(int l= 0; l < y.length; l++) {
    g2.setPaint(colors[l]);
    x = 65;



    if(y[l].length > 0)
    oldy = (float)y[l][0]+215;

    for(int i =1; i<y[l].length; i++) {
    y[l][i] = y[l][i]*20;
    g2.draw(new Line2D.Double(x , oldy, x + 50, y[l][i] +215 ));
    oldy = y[l][i] +215;
    x += 50;
    }
    }

    }
    }

    public Dimension getGrafPanelSize() {
    return prefSize;
    }






    public void renderSomething(String[][] values) {
    y = new float[3][];
    for (int i = 0; i < values.length; i++)
    y[i] = new float[values[i].length];
    maxTests=0;
    for(int i=0; i < values.length; i++){
    if(values[i].length > maxTests)
    maxTests = values[i].length;
    }
    if (maxTests < 8)
    maxTests= 8;
    for(int k=0; k < 3; k++) {
    for(int i =0; i<values[k].length; i++) {
    y[k][i] = Float.parseFloat(values[k][i].toString());
    }
    }
    called = true;
    repaint();

    }


    }
    _______________________
    Karl XII King Of Sweden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center