java/swing gui

This is a discussion on java/swing gui within the Tech Board forums, part of the Community Boards category; below is my code I'm using gridbaglayout. the layout has created extra space from the outside borders of the gui. ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    java/swing gui

    below is my code
    I'm using gridbaglayout.
    the layout has created extra space from the outside borders of the gui.
    Does anyone know in what direction i should study to eliminate this?
    I'm also having problems on my file processing.
    if there are any pointers, i would be very grateful.

    Code:
     import java.awt.*;
     import java.awt.event.*;
     import javax.swing.*;
     import java.util.*;
     import java.io.*;
     
     public class homeworkJava extends JFrame 
     { 
        Container container= getContentPane();
        GridBagLayout layout= new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints(); 
     
        Box box1 = Box.createVerticalBox();
        Box box2 = Box.createVerticalBox();
        Box box3 = Box.createVerticalBox();
     		   
        JTextField commands = new JTextField(18);
     
        JLabel GeneraLabel = new JLabel("Genera", JLabel.RIGHT);
        JLabel RatingLabel = new JLabel("Rating", JLabel.RIGHT);
        JLabel outputArea1Label = new JLabel("Details",JLabel.RIGHT);  
     
        JMenuBar bar = new JMenuBar();
     
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
     
        Vector DetailsV = new Vector();
        Vector GeneraV = new Vector();
        Vector RatingV = new Vector();
        Vector TableLabelsV = new Vector();
     
        JComboBox GeneraCB = new JComboBox(GeneraV);
        JComboBox RatingCB = new JComboBox(RatingV);
     
        
        JTable jt = new JTable(DetailsV,TableLabelsV);
        //JScrollPane jsp = new JScrollPane(jt);
     
        File GeneraInputFile = new File("/home/buddha/code/homeworkJava","GeneraFile.txt");
     
        public homeworkJava()
        {
     	  super("DVD Database");
     
     	  container.setLayout(layout);	
     
     	  readData(GeneraInputFile,GeneraV);	
     
     	  commands.setText("COMMANDS");		
     
     	  GeneraLabel.setLabelFor(GeneraCB);
      
     	  RatingLabel.setLabelFor(RatingCB);	 
       
     	  outputArea1Label.setLabelFor(jt);
     
     	  box1.add(GeneraLabel);
     	  box1.add(GeneraCB);
     
     	  box2.add(RatingLabel);
     	  box2.add(RatingCB);
     
     	  box3.add(outputArea1Label);
     	  box3.add(jt);   
     
     	  fileMenu.add(new JMenuItem("Open"));
     	  fileMenu.add(new JMenuItem("Close"));
     	  fileMenu.add(new JMenuItem("Search"));
     
     	  editMenu.add(new JMenuItem("Cut"));
     	  editMenu.add(new JMenuItem("Copy"));
     	  editMenu.add(new JMenuItem("Paste"));
     
     	  bar.add(fileMenu);
     	  bar.add(editMenu);
     	  
     	  TableLabelsV.add("Genera");
     	  TableLabelsV.add("Rating");
     	  TableLabelsV.add("Title");
     	  TableLabelsV.add("Discription");
     
     	  container.add(bar);
     
     	  box1.setBackground(Color.white);
     	  box2.setBackground(Color.white);
     	  box3.setBackground(Color.white);
     	  
     	  container.setBackground(Color.white);  
     	  
     	  c.fill = GridBagConstraints.BOTH;
     	  c.gridx = 0;
     	  c.gridy = 1;
     	  c.gridwidth=4;
     	  container.add(commands,c);	
     
     	  c.fill = GridBagConstraints.BOTH;
     	  c.gridx=0;
     	  c.gridy=2;
     	  c.gridwidth=2;
     	  container.add(box1,c);
     	  
     	  c.fill = GridBagConstraints.BOTH;
     	  c.gridx=1;
     	  c.gridy=2;
     	  c.gridwidth=0;
     	  container.add(box2,c);	   
     	  
     	  c.fill = GridBagConstraints.BOTH;
     	  c.gridx=0;
     	  c.gridy=4;
     	  c.gridheight=9;
     	  c.gridwidth=3;
     	  container.add(box3,c);
     
     	  setSize(250,250);
     	  setVisible(true); 
        } 
     
        public static void main(String args[])
        {
     	  homeworkJava application = new homeworkJava();
     	  application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
     
        public void readData(File inputFile,Vector DataStorage)
        {/////////////////work on this option 
     	 /* try
     	  {
     		FileReader fr = new FileReader(inputFile);
     		BufferedReader in = new BufferedReader(fr);
     		while(in.readLine()!=null)
     		{
     		  DataStorage.add(in.readLine());
     		}
     	  }
     	  catch(FileNotFoundException e)
     	  {
     		System.out.println("File Disappeared");
     	  }*/
        }
     }
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    for your file processing your wasting a line everytime you evaluate the loop condition, try something like this.

    Code:
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line;
    while( (line = in.readLine()) != null ) {
       // add 'line' to your data set
    }
    as for the layout problem, im not really familiar with GridBagLayout. try adding your bar to the frames content pane, then add your other components to a panel and the panel to the frame.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    thanks, i didn't see that waist of code.
    i fixed the menu issue with
    Code:
         setJMenuBar(bar);
    I'm still looking for ideas on how to eliminate the extra space encapsulating my gui.
    Can you compile and run this to see if you see what i'm seeing?
    i'm not sure, but i would think that this isn't os dependent.
    i'm running linux redhat9
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Moved to Tech

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    see this page for an explanation of the available ways to configure a GridBagLayout.

    http://java.sun.com/j2se/1.4.2/docs/...BagLayout.html

    There's about 6.8725 billion different ways to arrange those widgets on the frame. heres one solution.
    Add "c.weighty = 1.0" before you add your components to the frame. This will weight the y portion of the components equally eliminating the extra space that you dont want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Programming...
    By ShadeS_07 in forum C++ Programming
    Replies: 12
    Last Post: 12-28-2008, 04:58 PM
  2. Convert Windows GUI to Linux GUI
    By BobS0327 in forum Linux Programming
    Replies: 21
    Last Post: 11-27-2005, 04:39 AM
  3. .NET And GUI Programming :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2002, 04:22 PM
  4. GUI Programming :: C++ Exclusive
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2002, 03:22 PM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21