0

I am doing a JFrame in which the label inside I am dynamically add in from my database. In this case, sometimes it will be 3 data to be displayed and sometimes it will have 5 data to be displayed.

The program do show the contents, but it shows only the data that fits the screen. That means when I scroll down, nothing can be seen. Anyone know where I get wrong?

public class Trip extends JFrame implements ActionListener {
    ConnectionDB database = new ConnectionDB();

    public Trip() throws SQLException
    {
        super("Trip");
        Container c = getContentPane();
        c.setLayout(new BorderLayout());

        Definition a =new Definition();
        int NoOfAS = a.travelTime(User.UserID);

        Panel p = new Panel(new GridLayout(1,2));
        Panel p1 = new Panel(new GridLayout(NoOfAS,1));

        Label title = new Label("Itinerary");
        title.setFont(new Font("Serif", Font.BOLD, 48));
        c.add(title,"North");

        JScrollPane scroll = new JScrollPane(p);
        c.add(scroll,"Center");


        for(int i=0;i<NoOfAS;i++)
        {

            String ASNAME = database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);

            Label no = new Label("No:"+ (i+1) +" "+ASNAME);
            no.setFont(new Font("Times New Noman",Font.BOLD,20));
            Label descp = new Label(database.retrieveAS_Des(ASNAME,"AS_Description"));
            Label SugTime = new Label("Suggested Time:"+database.retrieveAS_Des(ASNAME,"AS_Time"));
            Label bus = new Label("Bus:"+database.retrieveAS_Des(ASNAME,"AS_Transport"));
            Label fee = new Label("Fees:"+database.retrieveAS_Des(ASNAME,"AS_Price"));
            Label line = new Label("---------------------------------------------------");
            line.setForeground (Color.red);
            Panel p2 = new Panel(new GridLayout(6,1));

            p2.add(no);
            p2.add(descp);
            p2.add(SugTime);
            p2.add(bus);
            p2.add(fee);
            p2.add(line);

            p1.add(p2);
            p.add(p1);
        }     
        // setSize(900,1700);
         pack();
         show();
    }

      public static void main(String args[]) throws SQLException
      {
    StdPlan app = new StdPlan( );
        app.addWindowListener(new WindowAdapter( ){});

      }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
yt729
  • 11
  • 5
  • 2
    Why not just display the data in a JTable that's held by a JScrollPane? Why go through all the gyrations that you're doing? – Hovercraft Full Of Eels Dec 26 '12 at 01:55
  • 1
    Create the GUI on the EDT. Do long running tasks like accessing the DB off the EDT. -- Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead implement `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Dec 26 '12 at 01:56
  • 1
    1) `app.addWindowListener(new WindowAdapter( ){});` That achieves nothing. 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) As @HovercraftFullOfEels mentioned, use a `JTable` for tabular data. 4) Don't extend frame, just use one. 5) `show()` was deprecated a long time ago. Look into compilation warnings and resolve them. 6) Use descriptive variable names and learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. – Andrew Thompson Dec 26 '12 at 02:01
  • This is the requirement of our assignment, so I need to use all these label to display my data. Thanks for all your comment, I will try to improve it. – yt729 Dec 27 '12 at 04:41

2 Answers2

3

The immediate issue that jumps out at me is you're mixing heavy weight and light weight components (Label & Panel onto a JScrollPane) - this is very rarely a good idea.

Use JLabel and JPanel instead.

Inside you for-next-loop you are adding p1 to p repeatedly, this actually achieves nothing, in fact, p probably should be using a BorderLayout seen as it only contains a single component.

I'd also recommend not extending directly from a JFrame, it serverly limits the reusability of you UI components. Better to use a JPanel as the base and add it to a instance of a JFrame (or any other container you might like to use).

Direcltly implementing listener interfaces is generally discouraged, as it tends to expose public methods that you don't normally want people calling. Better to use a inner or anonymous class instead - IMHO

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    *"I'd also recommend not extending directly from a JFrame, it serverly limits the reusability of you UI components. Better to use a JPanel as the base.."* That does not rule out extending `JPanel` (which seems unnecessary here - just use an instance). +1 for the rest. – Andrew Thompson Dec 26 '12 at 06:25
  • @AndrewThompson that would depend on what reuse the OP what's to get out of it, but its still a valid point – MadProgrammer Dec 26 '12 at 07:25
1

The problem is you are mixing awt (heavyweight) and swing (lightweight) components. Try to use only JLabel, JPanel etc.

Just to try you can check below code and it shows properly while your code shows what is in current view only:

public class Test extends JFrame implements ActionListener {
//ConnectionDB database = new ConnectionDB();

public Test() throws SQLException
{
    super("Trip");
    Container c = getContentPane();
    c.setLayout(new BorderLayout());

   // Definition a =new Definition();
    int NoOfAS = 15;//a.travelTime(User.UserID);

    JPanel p = new JPanel(/*new GridLayout(1,2)*/);
    JPanel p1 = new JPanel(new GridLayout(NoOfAS,1));

    JLabel title = new JLabel("Itinerary");
    title.setFont(new Font("Serif", Font.BOLD, 48));
    c.add(title,BorderLayout.NORTH);

    JScrollPane scroll = new JScrollPane(p);
    c.add(scroll,BorderLayout.CENTER);


    for(int i=0;i<NoOfAS;i++)
    {

        String ASNAME = i + "";//database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);

        JLabel no = new JLabel("No:"+ (i+1) +" "+ASNAME);
        no.setFont(new Font("Times New Noman",Font.BOLD,20));
        JLabel descp = new JLabel(ASNAME);
        JLabel SugTime = new JLabel(ASNAME);
        JLabel bus = new JLabel(ASNAME);
        JLabel fee = new JLabel(ASNAME);
        JLabel line = new JLabel("---------------------------------------------------");
        line.setForeground (Color.red);
        JPanel p2 = new JPanel(new GridLayout(6,1));

        p2.add(no);
        p2.add(descp);
        p2.add(SugTime);
        p2.add(bus);
        p2.add(fee);
        p2.add(line);

        p1.add(p2);
        p.add(p1);
    }     
    // setSize(900,1700);
     pack();
     show();
}

  public static void main(String args[]) throws SQLException
  {
Test app = new Test( );
    app.addWindowListener(new WindowAdapter( ){});

  }

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}
vishal_aim
  • 7,636
  • 1
  • 20
  • 23