LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2004, 01:48 PM   #1
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Rep: Reputation: 30
Question questions on my 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");
      }*/
   }
}
 
Old 07-11-2004, 07:45 PM   #2
p-static
Member
 
Registered: Jul 2004
Distribution: Gentoo
Posts: 101

Rep: Reputation: 15
Well, if you don't know about it already, http://java.sun.com/j2se/1.4.2/docs/api/index.html is the entire java API, and it's an incredibly useful resource. To answer your question, i'd look at c.insets. (A full description is somewhere in the above link.) As for the file processing, it would help if you'd say what sort of problems you're having.
 
Old 07-12-2004, 12:50 PM   #3
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
i have been unable to impliment the insets.

i have tried the following

Code:
container.getInsets(new Insets(0,0,0,0));

//and

container.Insets(0,0,0,0);

//and
Insets I = new Insets(0,0,0,0);
container.getInsets();

//and
Insets I = new Insets(0,0,0,0);
container.add(I);
Unfortunately there is no example code in any of my books or at that website you provided.
Good website by the way.
Do you have an example of it's implimentation?

Last edited by xviddivxoggmp3; 07-12-2004 at 12:54 PM.
 
Old 07-12-2004, 05:16 PM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Yes, what are the problems with the file IO?
By the way,
Code:
FileReader fr = new FileReader(inputFile);
BufferedReader in = new BufferedReader(fr);
can be more neatly written as:
Code:
BufferedReader in = new BufferedReader(new FileReader(inputFile));
if you're not planning on needing to access the FileReader explicitly.
 
Old 07-13-2004, 07:42 AM   #5
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Use GridBagConstraints.insets (c.insets = new Insets(...)).
Use JFrame.setJMenuBar so the bar will always be at the top.
Set the size of your window to get rid of the extra space. GridBagLayout will not stretch itself to fit larger windows.
 
Old 07-18-2004, 09:29 PM   #6
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
I'm writing a program that is to read a file into a vector after being tokenized.
I'm receiving an error that I'm not sure how to correct.

unreported exception java.ioIOException; must be caught or declared to be thrown
while((tokens = new StringTokenizer(in.readLine()))!=null))

I'm assuming that the while condition is invalid.
Why is that?
The code for my read is in the function on the very bottom of the program.
Code is below.

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);     
  
      box1.add(GeneraLabel);
      box1.add(GeneraCB);

      box2.add(RatingLabel);
      box2.add(RatingCB);

      box3.add(outputArea1Label);
      box3.add(jsp);   

      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");

      bar.setBackground(Color.white);

      box1.setBackground(Color.white);
      box2.setBackground(Color.white);
      box3.setBackground(Color.white);
      
      container.setBackground(Color.white);  
      
      c.fill = GridBagConstraints.HORIZONTAL;
      c.weightx = 1;
      c.weighty = 1;
      c.gridx = 0;
      c.gridy = 0;
      c.gridwidth=3;
      container.add(commands,c);    

      c.gridx=0;
      c.gridy=1;
      c.gridwidth=1;
      container.add(box1,c);
 
      c.gridx=2;
      c.gridy=1;
      c.gridwidth=0;
      container.add(box2,c);       
      
      c.fill = GridBagConstraints.BOTH;
      c.gridx=0;
      c.gridy=2;
      c.gridheight=6;
      c.gridwidth=3;
      container.add(box3,c);


      setJMenuBar(bar);
      setSize(300,300);
      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
      {
        BufferedReader in = new BufferedReader(new FileReader(inputFile));
        StringTokenizer tokens;

        while((tokens = new StringTokenizer(in.readLine()))!=null)
        {
          while(tokens.hasMoreTokens())
            DataStorage.add(tokens.nextToken());
        }
      }
      catch(FileNotFoundException e)
      {
        System.out.println("File is not accessable");
      }
   }
}
 
Old 07-19-2004, 10:44 AM   #7
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Right after:
catch(FileNotFoundException e)
{
System.out.println("File is not accessable");
}

Add 'catch(IOException e){ ....'
FileNotFoundException isn't the only exception thrown.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a java-swing problem... shosh Programming 2 06-26-2009 05:38 AM
Java Swing Card Game kermitthefrog91 Programming 11 08-18-2005 11:46 AM
java swing JTextArea gauravbagga Programming 1 05-23-2005 04:34 AM
XLib error while executing JAVA Swing GUI applicatoins rahul_kulkarni Programming 0 03-11-2005 12:07 AM
Java help! Using Swing and Layouts Mega Man X Programming 7 02-05-2004 05:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration