Slightlyclueless
Slightlyclueless

Reputation: 11

jTable not visable in panel i have created

I have created my own version of a panel so I can create some dragable tables but at the moment nothing is being added on to the panel I have created

panel class:

import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Point;

public class Workspace extends JPanel implements MouseListener,MouseMotionListener{
    private JTable t;
    private DatabaseHandler d;
    public Workspace(DatabaseHandler d ){
        super();
        this.d = d;
        setPreferredSize(new Dimension(1000, 1000));
        this.setLayout(null);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void load(String table){
        t = new JTable(d.getTable(table));
        //JScrollPane js=new JScrollPane(t);
        this.add(t);
    }
}

the code which calls it:

public class Display{
   private JPanel leftPanel = new JPanel(new BorderLayout());
   public JList list;
   public JFrame frame;
   private DatabaseHandler d = new DatabaseHandler("imdb");
   private Workspace w = new Workspace(d);

   public Display(){

       //create the window
       frame = new JFrame("FYP - Database Refactoring");
       frame.getContentPane().add(w, BorderLayout.CENTER);
       frame.setSize(1000,1000);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       list = new JList(d.getTableNames());
       list.addMouseListener(new MouseAdapter(){
           public void mousePressed(MouseEvent e){
               JList l = (JList)e.getSource();
               w.load((String)l.getSelectedValue());
               frame.setVisible(true);
           }
       });
       leftPanel.add(list);
       JLabel l = new JLabel("workbench");
      w.add(l);
       frame.getContentPane().add(leftPanel, BorderLayout.LINE_START);
       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {
    new Display();
   }
}

Any help would be appricated

Upvotes: 0

Views: 30

Answers (2)

camickr
camickr

Reputation: 324108

so I can create some dragable tables

If you need the ability to drag a JTable I would suggest that you should be using a JDesktopPane with JInternalFrames. You can easily drag an internal frame around the desktop. Then you just add the JTable\JScrollPane to the internal frame like you would to a normal JFrame.

Read the section from the Swing tutorial on How to Use Internal Frames for more information and working examples.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

  1. Avoid null layouts, and in fact this one is messing you up, since with null layouts, you must fully specify call component sizes and locations. Just don't do it.
  2. Don't re-add JTables in the MouseListener. Rather give your JPanel a JTable, and change its model from the MouseListener. Otherwise you're adding multiple JTables which doesn't make sense.
  3. Put your JTable into a JScrollPane
  4. And best to have your JPanel use BorderLayout and put the JScrollPane into the BorderLayout.CENTER position.

Upvotes: 3

Related Questions