Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By Tolls

Thread: Formatting contents within Tabbed Panes

  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Formatting contents within Tabbed Panes

    Hi all.

    I'm building a GUI interface using JTabbedPane and need some advice on formatting the contents of one of my tabs. Specifically, I've added a JTextField and JTable to the tab. I'd like the Text Field to appear on top of the JTable. Currently, it's displaying as a few pixels on the left side of the window.

    Java Code:
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    
    public class gui2 {
    public static void main(String[] args) throws IOException{
    	
    	gui2 app = new gui2();
    	app.build();		
    	
    }
    
    
    public void   build() throws IOException{
    	
    	SearchTest st = new SearchTest();
    	
    	JFrame frame = new JFrame("PCQ 0.1 Beta"); 
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	Container contentPane = frame.getContentPane(); 
    	
    
    	
    	//get JTabbedPane as object & create tabs
    	JTabbedPane tb = new JTabbedPane(); 
    	JPanel tbInv = new JPanel();
    	JPanel tbRep = new JPanel();
    	
    	//create INV View tab	
    	tb.add(tbInv, "Inventory View");
    	JPanel inv = new JPanel();
    	inv.add(st.textField);
    	tbInv.add(inv);
    	JScrollPane tableContainer = new JScrollPane(st.table); 
    
    
    	tbInv.add(tableContainer);
    	
    //create Report Tab
    	tb.add(tbRep, "Reports");
    	JPanel rep = new JPanel();
    	tbRep.add(rep);
    	
    //create frame size, add tabs, and make visible
    	contentPane.add(tb,BorderLayout.CENTER);
    	
    //frame.getContentPane().add(BorderLayout.EAST, tbConf);
    	frame.setSize(800, 500); 
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
    	
    	
    		}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Formatting contents within Tabbed Panes

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Formatting contents within Tabbed Panes

    You neglected to import the kitchen sink.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Formatting contents within Tabbed Panes

    More seriously, how do you expect anyone to help you by posting code with 17 unused imports, but omitting the class that has an unencapsulated field textField?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Formatting contents within Tabbed Panes

    Fair enough. class SearchTest main method contains set up for a test gui-- here, using a container. This is how I would like to implement the text field and table, except within a tab within gui2. The problem is, I can't use the same methods when using a tab. The question is how to manage layout of JPanel as tabs vice by using the content pane. Below are the two classes, unused imports are removed.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    
    
    
    public class gui2 {
    public static void main(String[] args) throws IOException{
    	
    	gui2 app = new gui2();
    	app.build();		
    	
    }
    
    
    public void   build() throws IOException{
    	
    	SearchTest st = new SearchTest();
    	
    	JFrame frame = new JFrame("PCQ 0.1 Beta"); 
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	Container contentPane = frame.getContentPane(); 
    
    	//get JTabbedPane as object & create tabs
    	JTabbedPane tb = new JTabbedPane(); 
    	JPanel tbInv = new JPanel();
    	JPanel tbRep = new JPanel();
    	
    	//create INV View tab
    	tb.add(tbInv, "Inventory View");
    	JPanel inv = new JPanel();
    	
    	tbInv.add(st.textField);
    	JScrollPane tableContainer = new JScrollPane(st.table); 
    	tbInv.add(inv);
    	
    	tbInv.add(tableContainer);
    	
    	/*  
    	SearchTest test = new SearchTest();
        JFrame f = new JFrame("-JQuery V_0.1 Beta-");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    	Container cP = f.getContentPane(); 
    
    	f.getContentPane().add(new JTabbedPane());
        f.getContentPane().add(new JScrollPane(test.table));
        f.getContentPane().add(test.textField, "North");
        f.setSize(600,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        test.textField.requestFocusInWindow();
    	 */
    	
    //create Report Tab
    	tb.add(tbRep, "Reports");
    	JPanel rep = new JPanel();
    	tbRep.add(rep);
    	
    //create frame size, add tabs, and make visible
    	contentPane.add(tb,BorderLayout.CENTER);
    	
    //frame.getContentPane().add(BorderLayout.EAST, tbConf);
    	frame.setSize(800, 500); 
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
    	
    	
    		}
    }

    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import javax.swing.*;
    import javax.swing.table.*;
     
    public class SearchTest
    {
        JTable table;
        JTextField textField;
     
        public SearchTest() throws IOException
        {
            table = getTable();
            textField = new JTextField();
            textField.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    search();
                }
            });
        }
     
        JTable getTable() throws IOException    { 
        	Scanner s = new Scanner(new File("C:\\INV\\TEST5.txt")); 
        	String[] colNames = { "ECN", "CUSTOMER", "MODEL", "SERIAL", "ICN", "MFR_CD" };  
        	 int cols = colNames.length;    
    
        	//TRY 1
        	/*
        	for(int row = 0; row < rows; row++)     
        		for(int col = 0; col < cols; col++){    
        			data[row][col] = "test " + (row*cols + col + 1);   
        		}
        	*/
    
        	//TRY 2
        	  
    		List<String[]> list = new ArrayList<String[]>();      
    		while (s.hasNextLine()) {         
    			String[] line = s.nextLine().split(" ");   
    			list.add(new String[] { (line[0]),   
    									(line[1]),
    									(line[2]),
    									(line[3]),
    									(line[4]),
    									(line[5])
    			
    									});      
    			}         
    		
    		int rows = list.size();
    		int numberOfColumns = 6; 
    		
        	Object[][] data = new Object[rows][cols];    
    		for (int i = 0; i < rows; i++) {        
    			data[i] = list.get(i);     
    		}     
    		
        	DefaultTableModel model = new DefaultTableModel(data, colNames) 
        	{            public Class getColumnClass(int col)     
        	{                Object o = getValueAt(0, col);     
        	if(o == null)                 
        		return Object.class;         
        	else              
        		return o.getClass();   
        	}     
        	};    
        	JTable table = new JTable(model);     
        	table.setDefaultRenderer(String.class, new CustomRenderer());
        	table.setRowSelectionAllowed(true);       
        	table.setColumnSelectionAllowed(true);     
        	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    
        	return table;   
        	
        	} 
    
     void search()
        {
            String target = textField.getText();
            for(int row = 0; row < table.getRowCount(); row++)
                for(int col = 0; col < table.getColumnCount(); col++)
                {
                    String next = (String)table.getValueAt(row, col);
                    if(next.equals(target))
                    {
                        showSearchResults(row, col);
                        return;
                    }
                }
     
            // reset table display after failed search
            //        > target null/not found <
            CustomRenderer renderer =
                    (CustomRenderer)table.getDefaultRenderer(String.class);
            renderer.setTargetCell(-1, -1);
            table.repaint();
        }
     
    private void showSearchResults(int row, int col)
        {
            CustomRenderer renderer = (CustomRenderer)table.getCellRenderer(row, col);
            renderer.setTargetCell(row, col);
            Rectangle r = table.getCellRect(row, col, false);
            table.scrollRectToVisible(r);
            table.repaint();
        }
     
    public static void main(String[] args) throws IOException
        {
            SearchTest test = new SearchTest();
            JFrame f = new JFrame("-JQuery V_0.1 Beta-");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    		Container cP = f.getContentPane(); 
    
    		f.getContentPane().add(new JTabbedPane());
            f.getContentPane().add(new JScrollPane(test.table));
            f.getContentPane().add(test.textField, "North");
            f.setSize(600,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            test.textField.requestFocusInWindow();
    
        }
    }
     
    class CustomRenderer implements TableCellRenderer
    {
        JLabel label;
        int targetRow, targetCol;
     
        public CustomRenderer()
        {
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setOpaque(true);
            targetRow = -1;
            targetCol = -1;
        }
     
    public Component getTableCellRendererComponent(JTable table,
                                                       Object value,
                                                       boolean isSelected,
                                                       boolean hasFocus,
                                                       int row, int column)
        {
            if(isSelected)
            {
                label.setBackground(table.getSelectionBackground());
                label.setForeground(table.getSelectionForeground());
            }
            else
            {
                label.setBackground(table.getBackground());
                label.setForeground(table.getForeground());
            }
            if(row == targetRow && column == targetCol)
            {
                label.setBorder(BorderFactory.createLineBorder(Color.red));
                label.setFont(table.getFont().deriveFont(Font.BOLD));
            }
            else
            {
                label.setBorder(null);
                label.setFont(table.getFont());
            }
            label.setText((String)value);
            return label;
        }
     
        public void setTargetCell(int row, int col)
        {
            targetRow = row;
            targetCol = col;
        }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Formatting contents within Tabbed Panes

    Java Code:
    	JTabbedPane tb = new JTabbedPane(); 
    	JPanel tbInv = new JPanel();
    	JPanel tbRep = new JPanel();
    	
    	//create INV View tab	
    	tb.add(tbInv, "Inventory View");
    	JPanel inv = new JPanel();
    	inv.add(st.textField);
    	tbInv.add(inv);
    	JScrollPane tableContainer = new JScrollPane(st.table);
    (This goes for your entire GUI to be honest).
    You have no Layout Manager associated with any of these containers.
    So the default is used, which I believe is FlowLayout (ie left to right).
    I'd recommend the Swing layout tutorial as a good thing to go through.
    You'll probably want BorderLayout for this immediate problem, but I'd go through the whole thing since you'll need to know this stuff.
    Redefine12 and DarrylBurke like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Formatting contents within Tabbed Panes

    Thank you Tolls. I'll post back with more dumb questions if I get stuck on Layout Manager concepts.

Similar Threads

  1. JTab Panes
    By Nidi in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 05:27 AM
  2. Replies: 1
    Last Post: 02-23-2010, 10:48 AM
  3. Layered Panes
    By teckno101 in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2009, 12:16 AM
  4. panes, frames and ddm's :|
    By skatefreak in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 11:21 AM
  5. Two content panes in one GUI?
    By Leprechaun in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 05:02 AM

Posting Permissions

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