Random and Jtable

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 873 Views - Last Post: 04 October 2012 - 07:53 PM Rate Topic: -----

#16 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 09:10 PM

View PostTomas1122, on 03 October 2012 - 09:02 PM, said:

View Postpbl, on 03 October 2012 - 08:55 PM, said:

You have a good start
Yopu will have to add one elemnt to the header array
to use [3] rather thean [2] as second dimention for the names array


great i got that working. but still having problem generating random majors.


Never mind i got that working :). Would i be able to put this table on a .html?
Was This Post Helpful? 0
  • +
  • -

#17 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 03 October 2012 - 09:15 PM

Also how would i make the rows alternate colors?
Was This Post Helpful? 0
  • +
  • -

#18 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 04 October 2012 - 03:38 AM

To make the row alternate color you need a CellRenderer
http://www.dreaminco...snippet5600.htm
make each cell return a JLabel setOpaque(true)

for the .HTML you need a JApplet. Converting from a JFrame is easy
http://www.dreaminco...snippet4467.htm
Was This Post Helpful? 0
  • +
  • -

#19 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 04 October 2012 - 11:11 AM

View Postpbl, on 04 October 2012 - 03:38 AM, said:

To make the row alternate color you need a CellRenderer
http://www.dreaminco...snippet5600.htm
make each cell return a JLabel setOpaque(true)

for the .HTML you need a JApplet. Converting from a JFrame is easy
http://www.dreaminco...snippet4467.htm


Not sure what im doing wrong but i can't get the row to alternate colors. :-/
Was This Post Helpful? 0
  • +
  • -

#20 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 04 October 2012 - 11:49 AM

Post your CellRendrer code and the way you attach it to your JTable
Was This Post Helpful? 0
  • +
  • -

#21 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 04 October 2012 - 12:24 PM

View Postpbl, on 04 October 2012 - 11:49 AM, said:

Post your CellRendrer code and the way you attach it to your JTable



JTable t = new JTable(names, header);
		add(new JScrollPane(t));
                class MyTable extends JTable implements TableCellRenderer {
		MyTable() {
			
			super(new MyModel());
		}

	
		public TableCellRenderer getCellRenderer(int row, int col) {
			
			return this;
		}


		public Component getTableCellRendererComponent(JTable arg0,
				Object component, boolean focus, boolean arg3, int arg4, int arg5) {
			
			return (Component) component;
		}
	}




i don't think im doing this right but this is what i did. please help.
Was This Post Helpful? 0
  • +
  • -

#22 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 04 October 2012 - 12:34 PM

return (Component) component;

the component that you received as parameter is a String object you cannot cast it to a Component

You will have to return a JLabel who's setText() is your actual String passed as parameter component
Make the JLabel opaque(true)
based on the row number selected a background color depending if row % 2 == 0 or not
Was This Post Helpful? 0
  • +
  • -

#23 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 04 October 2012 - 12:45 PM

View Postpbl, on 04 October 2012 - 12:34 PM, said:

return (Component) component;

the component that you received as parameter is a String object you cannot cast it to a Component

You will have to return a JLabel who's setText() is your actual String passed as parameter component
Make the JLabel opaque(true)
based on the row number selected a background color depending if row % 2 == 0 or not



JTable t = new JTable(names, header);
		add(new JScrollPane(t));
                class MyTable extends JTable implements TableCellRenderer {
		MyTable() {
			
			super(new MyModel());
		}

	
		public TableCellRenderer getCellRenderer(int row, int col) {
			
			return this;
		}


		public Component getTableCellRendererComponent(JTable arg0,
				Object component, boolean focus, boolean arg3, int arg4, int arg5) {
			
			return JLable;
                         JLabel opaque(true)
		}
	}



Sorry i have no clue what im doing wrong. :-/. This is my first java code i ever done. lol
Was This Post Helpful? 0
  • +
  • -

#24 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 04 October 2012 - 01:22 PM


import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Shashi Suthar
 */

public class Names extends JApplet implements ActionListener {
 
        JButton[] button;
        JLabel[] label;
        int[] nbClick;
       
        // Applet init method that will init the JComponent
        // will be called automatically when running an Applet
        // will be "manually" called by main() when running as an application
    @Override
        public void init() {
                // create the buttons and the labels that display how many
                // time the buttons were clicked
                button = new JButton[2];
                label = new JLabel[button.length];
                nbClick = new int[button.length];
                // as a gridLayout of number of buttons X 2 for their corresponding labels
                setLayout(new GridLayout(button.length,2));
                for(int i = 0; i < button.length; i++) {
                        // create button
                        button[i] = new JButton("Button #" + (i+1));
                        button[i].addActionListener(this);
                        add(button[i]);
                        // and its corresponding label
                        label[i] = new JLabel("     Nb click: 0");
                        add(label[i]);
                }
        }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    


public class Names extends JFrame {




What am i doing wrong to make it work as applet? . I get errors on this line "public class Names extends JFrame {"
Was This Post Helpful? 0
  • +
  • -

#25 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Random and Jtable

Posted 04 October 2012 - 05:53 PM

help?
Was This Post Helpful? 0
  • +
  • -

#26 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random and Jtable

Posted 04 October 2012 - 07:53 PM

View PostTomas1122, on 04 October 2012 - 03:45 PM, said:

Sorry i have no clue what im doing wrong. :-/. This is my first java code i ever done. lol

And you already start with a GUI ???
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2