Results 1 to 1 of 1
- 03-17-2011, 11:36 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Need some help with panels inside panels
Hey, im currently taking a starter course in java and we have been given the task of making a small program with GUI that demonstrates various sorting methods. As a "pre-task" to this project we've been asked to create a program that can make and draw a graph based on a random array with given number of rows, and max/min-values.
Here is what the finished program should look like:

I'm currently having some problems with the drawing part, because once my gui has been created it seems hard to update a panel within the GUI when i hit the draw button.
The empty panel which im having problems getting to update when i click the "draw graph" button. I've called it graphWindow in the picture, but in the code below i think its called grafVindu

I've currently got three classes:
a class called Filler, this one handles creation and filling of the random array.
a class called Graf (=graph)Java Code:import javax.swing.JFrame; import java.util.Random; public class Filler { int rader=0; int[] tabell=null; Graf grafz=null; public Filler(int a, int b, int c){ rader=a; Random rnd = new Random(); tabell= new int[a]; for(int i=0;i<tabell.length -1;i++){ tabell[i]=rnd.nextInt((c - b))+b; } grafz=new Graf(tabell); } public Graf getGraf(){ return grafz; } }
a class called GUItest, which is mostly designed and generated by Swing with the exception of the actionlistener.Java Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame; import java.util.Random; public class Graf extends JPanel { int rader=0; int[] tabell=null; public Graf(int[] b){ tabell=b; } public void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth(); int height = getHeight(); int width2 = width / (tabell.length-1); for (int i = 0; i < tabell.length-1; i++) { g.drawRect(i * width2, height - tabell[i], width2, tabell[i]); } } }
If i modify my code to open a new window rather than try to update the empty panel the graph draws just fine. So, any ideas on how I can make the graph inside the GUI? Thanks in advanceJava Code:import javax.swing.JOptionPane; public class GUItest extends javax.swing.JFrame { /** Creates new form GUItest */ public GUItest() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); a = new javax.swing.JTextField(); b = new javax.swing.JTextField(); c = new javax.swing.JTextField(); grafVindu = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Draw graph"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setText("Number of rows:"); jLabel2.setText("Min value:"); jLabel3.setText("max value:"); grafVindu.setBackground(new java.awt.Color(255, 255, 255)); grafVindu.setLayout(new javax.swing.BoxLayout(grafVindu, javax.swing.BoxLayout.LINE_AXIS)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(a, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(55, 55, 55) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(70, 70, 70) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(75, 75, 75) .addComponent(jButton1)) .addComponent(grafVindu, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGap(38, 38, 38) .addComponent(jButton1))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(grafVindu, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int rader, minVerdi, maxVerdi; try { rader = Integer.parseInt( this.a.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "uglyldig rad-verdi", "Error", JOptionPane.ERROR_MESSAGE); return; } try { minVerdi = Integer.parseInt( this.b.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "uglyldig min-verdi", "Error", JOptionPane.ERROR_MESSAGE); return; } try { maxVerdi = Integer.parseInt( this.c.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "uglyldig max-verdi", "Error", JOptionPane.ERROR_MESSAGE); return; } Filler filler = new Filler(rader,minVerdi,maxVerdi); grafVindu=filler.getGraf(); /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GUItest().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField a; private javax.swing.JTextField b; private javax.swing.JTextField c; public javax.swing.JPanel grafVindu; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; // End of variables declaration }
Similar Threads
-
Java Panels
By nikkka in forum New To JavaReplies: 11Last Post: 03-12-2011, 02:28 PM -
Displaying panels inside a main panel
By Sneaky Fox in forum AWT / SwingReplies: 4Last Post: 01-21-2011, 04:12 PM -
Deleting panels
By dilpreet28 in forum New To JavaReplies: 13Last Post: 08-11-2010, 09:23 AM -
how to add 2 panels in a panel inside a frame
By clydedoris in forum New To JavaReplies: 4Last Post: 07-31-2010, 09:11 AM -
resizeable panels
By simo_mon in forum AWT / SwingReplies: 4Last Post: 07-29-2009, 01:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks