Results 1 to 16 of 16
  1. #1
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Post how to split these code

    public class RangeSample {
    public static void main(String args[]) {
    JFrame frame = new JFrame("Range Example");
    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(3, 2));

    content.add(new JLabel("Range: 0-255"));
    Document rangeOne = new IntegerRangeDocument(0, 255);
    JTextField textFieldOne = new JTextField();
    textFieldOne.setDocument(rangeOne);
    content.add(textFieldOne);

    content.add(new JLabel("Range: -100-100"));
    Document rangeTwo = new IntegerRangeDocument(-100, 100);
    JTextField textFieldTwo = new JTextField();
    textFieldTwo.setDocument(rangeTwo);
    content.add(textFieldTwo);

    content.add(new JLabel("Range: 1000-2000"));
    Document rangeThree = new IntegerRangeDocument(1000, 2000);
    JTextField textFieldThree = new JTextField();
    textFieldThree.setDocument(rangeThree);
    content.add(textFieldThree);

    frame.setSize(250, 150);
    frame.setVisible(true);
    }
    }

    class IntegerRangeDocument extends PlainDocument {

    int minimum, maximum;

    int currentValue = 0;

    public IntegerRangeDocument(int minimum, int maximum) {
    this.minimum = minimum;
    this.maximum = maximum;
    }

    public int getValue() {
    return currentValue;
    }

    public void insertString(int offset, String string, AttributeSet attributes)
    throws BadLocationException {

    if (string == null) {
    return;
    } else {
    String newValue;
    int length = getLength();
    if (length == 0) {
    newValue = string;
    } else {
    String currentContent = getText(0, length);
    StringBuffer currentBuffer = new StringBuffer(currentContent);
    currentBuffer.insert(offset, string);
    newValue = currentBuffer.toString();
    }
    try {
    currentValue = checkInput(newValue);
    super.insertString(offset, string, attributes);
    } catch (Exception exception) {
    Toolkit.getDefaultToolkit().beep();
    }
    }
    }

    public void remove(int offset, int length) throws BadLocationException {
    int currentLength = getLength();
    String currentContent = getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + after;
    try {
    currentValue = checkInput(newValue);
    super.remove(offset, length);
    } catch (Exception exception) {
    Toolkit.getDefaultToolkit().beep();
    }
    }

    public int checkInput(String proposedValue) throws NumberFormatException {
    int newValue = 0;
    if (proposedValue.length() > 0) {
    newValue = Integer.parseInt(proposedValue);
    }
    if ((minimum <= newValue) && (newValue <= maximum)) {
    return newValue;
    } else {
    throw new NumberFormatException();
    }
    }
    }

    how can i use this code in my gui window..
    i have gui.java and main.java .
    Jtextfield name inputtext


    testgui.java (GUI)


    package Org;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class TestGUI extends javax.swing.JFrame {

    public TestGUI() {
    initComponents();
    TestGUI.this.setVisible(true);
    }

    }

    main.java

    import Org.TestGUI;

    public class Main {

    public static void main(String[] args) {
    TestGUI ob = new TestGUI();

    }

    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    sorry what is the question??

    how to split what - be specific?

  3. #3
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    i want to implement these code in my gui windows..

    the above coding run only in command prompt now i want to use this code in my
    GUI..

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you have a GUI program already and you want to add these to it? so put these in the same package as the GUI and you can use the classes/methods in your GUI program

  5. #5
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    ya u r correct..


    but now i have 2 packages. one package for gui (package name Org, filename - TestGui.java) and another package for main class (package name is - fortext ,filename - main.java)

    The testGui contain one textfield -- named by input text..

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if you want to put the Org package files into the other package, it needs to be one package so rename the Org package to the same name as the other package so it becomes one package with all the files

  7. #7
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    i know how to write these code in one package..

    now i want to use two packages.. so please convert the code to 2 packages..

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    ohhh i see. then do the opposite - just name two packages

    package org1;

    public class TestGui {

    }

    package org2;

    public class Main {

    }

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you will need to put them in different folders as well like this:

    src/org1/TestGui.java
    src/org2/Main.java

    and when you need a method from TestGui to be used in Main.java, you will need to do this:
    org1.TestGui.someMethod()

    also when compiling a package, you must go to the 'src' folder above the package folder, compile like this:

    javac org1/TestGui.java
    javac org2/Main.java

    and to run:

    java org1.TestGui
    java org2.Main

  10. #10
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    thats not possible my friend..

    because the main.java file only have the psvm.. run the gui..

    so we have to split the code from class integerRange.... { to }

    then write the psvm code in main.java.. if i write the code in main.java the inputtext cant be extend here

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    what do you mean psvm?

  12. #12
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    public static void main(psvm)

  13. #13
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Default

    TextField with Constaints : TextField**Swing JFC**Java


    please go through this code..

    the concept is it will create one gui automatically when i run from command prompt like java RangeSample.java


    now i want to use this code in my gui.. that's all friend...

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i'm really not sure how to help you sorry...

  15. #15
    helptoall's Avatar
    helptoall is offline Member
    Join Date
    Mar 2011
    Location
    Chennai
    Posts
    9
    Rep Power
    0

    Post

    am using netbeans IDE friend

  16. #16
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    please note that the class RangeSample already make a call to frame.setVisible(true); other point: where is this method initComponents(); implemented?

Similar Threads

  1. How to use split?
    By jeff_limsy in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 09:23 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 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
  •