Results 1 to 2 of 2
  1. #1
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Having problem displaying JTable

    Hi, I'm still getting used to Swing components, and I'm having trouble creating a JTable that receives values during the execution of my program and displays it. Apparently it works fine when I try to create the table in the Main function, but when I try to do it in the middle of another function it displays a frame with nothing inside.
    this is my code for creating and displaying a Table (it's a modified sun example).
    Java Code:
    public class Tabela extends JPanel {
    
        public Tabela(String[] columnNames, Object[][] data) {
            super(new GridLayout(1,0));
    
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
    
    
            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
    
            //Add the scroll pane to this panel.
            add(scrollPane);
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        protected static void createAndShowGUI(String[] columnNames, Object[][] data) {
            //Create and set up the window.
            JFrame frame = new JFrame("Tabela");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Tabela newContentPane = new Tabela(columnNames, data);
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    }
    This is the main class of the program, sorry for posting so much code but I have no idea why this problem is happening.

    Java Code:
    /* Defines the system's interface */
    public class Repart implements ActionListener{
    
    	private JButton go;
        private JTextField nuConsumers;
    	private JTextField percCons;
    	private JTextField percBold;
    	private JTextField percMiser;
        private JTextField nuEnterprises;
        private JTextField percInvestor;
        private JTextField percDecadent;
        private JTextField percCheater;
        private JTextField percGreedy;
        private JTextField percStrategist;
        private JTextField percWholesaler;
        private JTextField percReactive;
    	private JTextField timewhen;
    	private JRadioButton enabled;
    	private JRadioButton disabled;
    	private JRadioButton trueRumour;
        private JRadioButton falseRumour;
        private JRadioButton positive;
        private JRadioButton negative;
    	private ButtonGroup group;
        private ButtonGroup group2;
        private ButtonGroup group3;
    	private JSpinner timesteps;
    	private final SpinnerModel sm;
        private JSpinner percConsumers;
        private final SpinnerModel sm2;
    
        /* Simulation Options*/
    
        private int cycles;
    
        private int numConsumers;
        private int pConservative, pBold, pMiser;
    
    
        private int numEnterprises;
        private int pInvestor, pDecadent, pCheater, pGreedy, pStrategist, pWholesaler, pReactive;
    
        private boolean enableRumour, isTrue, isPositive;
        private int when;
        private int pConsumers;
    
    
    	public Repart(){
    		sm = new SpinnerNumberModel(new Integer(0), new Integer(0), new Integer(1000), new Integer(10));
            sm2 = new SpinnerNumberModel(new Integer(0), new Integer(0), new Integer(100), new Integer(5));
    		timesteps = new JSpinner(sm);
            percConsumers = new JSpinner(sm2);
            nuConsumers = new JTextField(5);
    		percCons = new JTextField(3);
    		percBold = new JTextField(3);
            percMiser = new JTextField(3);
            nuEnterprises = new JTextField(5);
            percInvestor = new JTextField(3);
            percDecadent = new JTextField(3);
            percCheater = new JTextField(3);
            percGreedy = new JTextField(3);
            percStrategist = new JTextField(3);
            percWholesaler = new JTextField(3);
            percReactive = new JTextField(3);
    
            timewhen = new JTextField(6);
    		group = new ButtonGroup();
            group2 = new ButtonGroup();
            group3 = new ButtonGroup();
    		go = new JButton("Go");
    	}
    
    	public void launchFrame(){
    		JFrame frame = new JFrame("RePart");
    		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frame.setLocation(300, 50);
    
    		frame.setLayout(new GridLayout(17,1));
    		
    		FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
    
    		Panel p = new Panel();
    		JLabel time = new JLabel("Cycles:");
    		p.setLayout(fl);
    		p.add(time);
    		timesteps.addChangeListener(new ChangeListener(){
    			public void stateChanged(ChangeEvent e){
    				String text = timesteps.getValue().toString();
    				cycles = Integer.parseInt(text);
    			}
    		});
    		p.add(timesteps);
    		frame.add(p);
    
            JSeparator sep = new JSeparator();
            frame.add(sep);
    
            Panel p0 = new Panel();
            p0.setLayout(fl);
            JLabel labelNumConsumers = new JLabel("Number of Consumers:");
            p0.add(labelNumConsumers);
            p0.add(nuConsumers);
            frame.add(p0);
    
    		Panel p1 = new Panel();
    		p1.setLayout(fl);
    		JLabel cons = new JLabel("Conservative :");
    		JLabel bold = new JLabel("  Bold :");
    		JLabel miser = new JLabel("  Miser :");
    		JLabel percentage1 = new JLabel("%");
    		JLabel percentage2 = new JLabel("%");
    		JLabel percentage3 = new JLabel("%");
    	
    		p1.add(cons);
    		p1.add(percCons);
    		p1.add(percentage1);
    		
    		p1.add(bold);
    		p1.add(percBold);
    		p1.add(percentage2);
    		
    		p1.add(miser);
    		p1.add(percMiser);
    		p1.add(percentage3);
    
    		frame.add(p1);
    
            JSeparator sep2 = new JSeparator();
            frame.add(sep2);
    
    
            Panel p2 = new Panel();
            p2.setLayout(fl);
            JLabel labelNumEnterprises = new JLabel("Number of Enterprises:");
            p2.add(labelNumEnterprises);
            p2.add(nuEnterprises);
            frame.add(p2);
    
    
    
    		Panel p3 = new Panel();
    		p3.setLayout(fl);
    		JLabel inv = new JLabel("   Investor :");
    		JLabel ins = new JLabel("  Decadent :");
    		JLabel chea = new JLabel("    Cheater :   ");
    
    		JLabel percentage4 = new JLabel("%");
    		JLabel percentage5 = new JLabel("%");
    		JLabel percentage6 = new JLabel("%");
    
    		p3.add(inv);
    		p3.add(percInvestor);
    		p3.add(percentage4);
    		
    		p3.add(ins);
    		p3.add(percDecadent);
    		p3.add(percentage5);
    		
    		p3.add(chea);
    		p3.add(percCheater);	
    		p3.add(percentage6);
            frame.add(p3);
    
    
            Panel p4 = new Panel();
            p4.setLayout(fl);
    
            JLabel gree = new JLabel("   Greedy :  ");
            JLabel str = new JLabel("  Strategist :");
            JLabel ws = new JLabel("  Wholesaler :");
    
    	    JLabel percentage7 = new JLabel("%");
            JLabel percentage8 = new JLabel("%");
            JLabel percentage9 = new JLabel("%");
    
    	    p4.add(gree);
            p4.add(percGreedy);
            p4.add(percentage7);
    
            p4.add(str);
            p4.add(percStrategist);
            p4.add(percentage8);
    
            p4.add(ws);
            p4.add(percWholesaler);
            p4.add(percentage9);
            frame.add(p4);
    
            Panel p5 = new Panel();
            p5.setLayout(fl);
    
    		JLabel reac = new JLabel("   Reactive :");
    
            JLabel percentage10 = new JLabel("%");
    
            p5.add(reac);
    		p5.add(percReactive);
    		p5.add(percentage10);
            frame.add(p5);
    
            JSeparator sep3 = new JSeparator();
            frame.add(sep3);
    
    
    		Panel p6 = new Panel();
    		p6.setLayout(fl);
    		JLabel publicity = new JLabel("Rumour:");
        	p6.add(publicity);
    
            enabled = new JRadioButton("Enable   ", true);
            disabled = new JRadioButton("Disable", false);
            group.add(enabled);
            group.add(disabled);
    
    		p6.add(enabled);
    		p6.add(disabled);
    
            frame.add(p6);
    
            Panel p7 = new Panel();
            p7.setLayout(fl);
    
            JLabel space = new JLabel("             ");
            trueRumour = new JRadioButton("True      ", true);
            falseRumour = new JRadioButton("False", false);
            group2.add(trueRumour);
            group2.add(falseRumour);
    
            p7.add(space);
            p7.add(trueRumour);
            p7.add(falseRumour);
    
            frame.add(p7);
        
            Panel p8 = new Panel();
            p8.setLayout(fl);
    
            JLabel space2 = new JLabel("             ");
            positive = new JRadioButton("Positive ", true);
            negative = new JRadioButton("Negative", false);
            group3.add(positive);
            group3.add(negative);
    
            p8.add(space2);
            p8.add(positive);
            p8.add(negative);
    
            frame.add(p8);
    
            Panel p9 = new Panel();
            p9.setLayout(fl);
    
    		JLabel w = new JLabel("When?");
    		p9.add(w);
    		p9.add(timewhen);
    	 
            
            JLabel pC = new JLabel("      Percentage of Consumers: ");
            p9.add(pC);
    		percConsumers.addChangeListener(new ChangeListener(){
    			public void stateChanged(ChangeEvent e){
    				String text = percConsumers.getValue().toString();
    				pConsumers = Integer.parseInt(text);
    			}
    		});
            p9.add(percConsumers);
    
    
            frame.add(p9);
    
            JSeparator sep4 = new JSeparator();
            frame.add(sep4);
    
    		Panel p10 = new Panel();
            go.addActionListener(this);
            p10.add(go);
    		frame.add(p10);
    
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent e){
    		boolean isOk = true;
    		SimulatorCore sm = new SimulatorCore();
    
            String text = nuConsumers.getText();
    		if(text.length() == 0)
    			numConsumers = 0;
    		else
            	numConsumers = Integer.parseInt(text);
    
            text = percCons.getText();
    		if(text.length() == 0)
    			pConservative = 0;
    		else
            	pConservative = Integer.parseInt(text);
    
            text = percBold.getText();
    		if(text.length() == 0)
    			pBold = 0;
    		else
            	pBold = Integer.parseInt(text);
    
            text = percMiser.getText();
    		if(text.length() == 0)
    			pMiser = 0;
    		else
            	pMiser = Integer.parseInt(text);
    
    		//validates the fileds
    		if(pConservative + pBold + pMiser != 100){
    			String message = String.format("Percentage of consumers profiles must total 100");
    			JOptionPane.showMessageDialog(null, message);
    			isOk = false;
    		}
    
            text = nuEnterprises.getText();
    		if(text.length() == 0)
    			numEnterprises = 0;
    		else
            	numEnterprises = Integer.parseInt(text);
    
            text = percInvestor.getText();
    		if(text.length() == 0)
    			pInvestor = 0;
    		else
            	pInvestor = Integer.parseInt(text);
    
            text = percDecadent.getText();
    		if(text.length() == 0)
    			pDecadent = 0;
    		else
            	pDecadent = Integer.parseInt(text);
    
    	    text = percCheater.getText();
    		if(text.length() == 0)
    			pCheater = 0;
    		else
    			pCheater = Integer.parseInt(text);
    
            text = percGreedy.getText();
    		if(text.length() == 0)
    			pGreedy = 0;
    		else
            	pGreedy = Integer.parseInt(text);
    
            text = percStrategist.getText();
    		if(text.length() == 0)
    			pStrategist = 0;
    		else
            	pStrategist = Integer.parseInt(text);
    
            text = percWholesaler.getText();
    		if(text.length() == 0)
    			pWholesaler = 0;
    		else
            	pWholesaler = Integer.parseInt(text);
    
            text = percReactive.getText();
    		if(text.length() == 0)
    			pReactive = 0;
    		else
            	pReactive = Integer.parseInt(text);
    
    		//validates the fileds
    		if(pInvestor + pDecadent + pCheater + pGreedy + pStrategist + pWholesaler + pReactive != 100){
    			String message = String.format("Percentage of enterprises profiles must total 100");
    			JOptionPane.showMessageDialog(null, message);
    			isOk = false;
    		}
    
            text = timewhen.getText();
    		if(text.length() == 0)
    			when = 0;
    		else
            	when = Integer.parseInt(text);
    
            if(enabled.isSelected())
                enableRumour = true;
            else
                enableRumour = false;
    
            if (trueRumour.isSelected())
                isTrue = true;
            else
                isTrue = false;
    
            if(positive.isSelected())
                isPositive = true;
            else
                isPositive = false;
           
    		//if all the parametrs are ok, then executes the simulation
    		if(isOk == true){
    			final String[] columnNames = {"First Name",
    	                "Last Name",
    	                "Sport",
    	                "# of Years",
    	                "Vegetarian"
    	        };
    			 final Object[][] data = {
    					 {"Mary", "Campione",
    						 "Snowboarding", new Integer(5), new Boolean(false)},
    						 {"Alison", "Huml",
    							 "Rowing", new Integer(3), new Boolean(true)},
    							 {"Kathy", "Walrath",
    								 "Knitting", new Integer(2), new Boolean(false)},
    								 {"Sharon", "Zakhour",
    									 "Speed reading", new Integer(20), new Boolean(true)},
    									 {"Philip", "Milne",
    										 "Pool", new Integer(10), new Boolean(false)}
    			 };	 
    		     Tabela.createAndShowGUI(columnNames,data);
    			sm.setParameters(cycles, numConsumers, pConservative, pBold, pMiser, numEnterprises, pInvestor, pDecadent, pCheater, pGreedy, pStrategist, pWholesaler, pReactive, enableRumour, isTrue, isPositive, when, pConsumers);
    			try{
    				sm.simulate();
    			}catch(IOException io){
    				System.err.println("Error writing file");
    				System.exit(1);
    			}
    			System.exit(0);
    		}
        }
    
    	public void initialStep(){
    		JFrame initial = new JFrame();
    		initial.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		initial.setUndecorated(true);
    		initial.setLocation(370,230);
    		Icon picture = new ImageIcon( getClass().getResource("TelaInicial.jpeg"));
    		JLabel label = new JLabel();
    		label.setIcon(picture);
    		initial.getContentPane().add(label);
    		initial.setSize(362,267);
    		initial.setVisible(true);
    		try{
    			Thread.sleep(4000);
    		}
    		catch(Exception e){
    		}
    		initial.dispose();
    
    	}
    
    	public static void main(String[] args){
    		Repart repart = new Repart();
    	    repart.initialStep();
    	    
    		repart.launchFrame();
    	}
    }
    
    
    }

  2. #2
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Could you post an SSCCE for your concern.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. problem with displaying data from hql query
    By omesanni in forum Web Frameworks
    Replies: 1
    Last Post: 01-05-2009, 10:03 AM
  2. Problem displaying the view content.
    By poonam2008 in forum Eclipse
    Replies: 0
    Last Post: 11-30-2008, 02:30 AM
  3. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  4. problem with displaying result..
    By SCS17 in forum New To Java
    Replies: 4
    Last Post: 04-23-2008, 11:19 AM
  5. problem displaying GUI from jbase
    By diveshsingh in forum AWT / Swing
    Replies: 0
    Last Post: 01-30-2008, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •