Results 1 to 7 of 7
  1. #1
    Jarob22 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Making borders on labels/table-like format

    Hey there,

    I had another thread about this GUI im working on but I figured this is a totally different issue so I made a new thread.

    I want to make a table-like panel that pops up to show some information that will be input and am not sure how to go about it. Something like this:

    Java Code:
             ----------------------------
             | A   |  B  |   C  |   D   |
    -------------------------------------
    Title A  |     |     |      |       |
    -------------------------------------
    Title B  |     |     |      |       |
    -------------------------------------
    Anyone got any ideas?

    Thanks.

    Edit: Yay the forums messed up my spacebar editing and doesn't allow tabs :(

    Basically 3 rows and 5 columns, the first row being the titles of the columns, and the first column being the titles of the rows.

    Edit2: the inputs for the table are not to be input by the user, they are to be automatically changed by the program, so no need for input fields here for the user to type into.

    Edit3: Failing a tabular thing, just making the labels which hold the table values a certain colour would be good, but I can't seem to do that atm.. myContainer.setBackground(Color.XXX) doesn't seem to change the colour of the label, and setForeground just changed the text colour :(
    Last edited by Fubarable; 03-06-2011 at 07:46 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I added code tags and edited your diagram so that it works for me, and hopefully for you in your browser too. For presenting tabular data, nothing really beats using a JTable.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    And for providing the leftmost column in your illustration, you could adapt camickr's Row Number Table.
    Row Number Table « Java Tips Weblog

    db
    Last edited by DarrylBurke; 03-07-2011 at 05:57 PM.

  4. #4
    Jarob22 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Hey.

    Thanks for the tip on JTables fubar, I've had a go at it and it all mostly works, but when I set a certain cell's value, it doesn't seem to go through. This example will run by itself:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
    		
    	public void portInfo()
    	{
    		JFrame ports = new JFrame();
    		ports.setLayout(new BorderLayout());
    		ports.setPreferredSize(new Dimension(350,200));
    		TableModel dataModel = new AbstractTableModel() {
    			public int getColumnCount()
    			{
    				return 6;
    			}
    			public int getRowCount()
    			{
    				return 3;
    			}
    			public Object getValueAt(int row, int col)
    			{
    				return null;
    			}
    		};
    		JTable table = new JTable(dataModel);
    		table.setShowGrid(true);
    		table.setRowSelectionAllowed(false);
    		table.setPreferredSize(new Dimension(300,150));
    		table.setValueAt("PortX", 0, 1); //None of these
    		table.setValueAt("DDRx", 0, 2); //three lines seem 
    		table.setValueAt("A", 1, 0);     //to set the cell values
    
    		ports.add(table, BorderLayout.CENTER);
    		ports.pack();
    		ports.setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		Test t = new Test();
    		t.portInfo();
    	}
    }
    Anyone have any idea why the 3 table.setValueAt() methods don't seem to be setting the values at those cell co-ordinates. I did assume that cell numbering started at 0, but tried it starting at 1, which didn't change anything.

    Thanks in advance!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Well, you do have a getValueAt(...) method that unconditionally returns null.

    Why are you using a concretized AbstractTableModel? What's wrong with DefaultTableModel? Recommended reading: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Don't rush through it. Understanding JTable takes a little more time than understanding less complex components like labels and buttons.

    db

  6. #6
    Jarob22 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Not quite sure why having a getValueAt() method returning null would affect the setValueAt() method :S

    The JTable api laid out this example as a way of setting up a JTable. I'll have a look at that reading.

  7. #7
    Jarob22 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Found out what the problem was - needed to create an overriding setValueAt() method in the datamodel. Thanks for the reading and tips fubar and db.

Similar Threads

  1. how to display data in a table format using jstl tag
    By anand12356 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 02-09-2011, 01:26 PM
  2. need help making a multiplication table
    By KAM0002 in forum New To Java
    Replies: 17
    Last Post: 12-07-2010, 02:44 AM
  3. Borders
    By sessomw5098 in forum New To Java
    Replies: 2
    Last Post: 04-21-2010, 05:00 AM
  4. Help making table
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 03:53 PM
  5. Replies: 2
    Last Post: 07-30-2008, 12:33 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
  •