0

I'm pretty new to Javas swings, so sorry if this is something trivial. This is the constructor, I excluded unnecessary form items. (I tried running the code as short as this, but the problem still appears)

//This just opens a connection to MySQL server, this doesn't create any problems.
bp = BazaPodataka.getBaza();

//Forming the main frame..
JFrame frame = new JFrame();
{
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds(d.width/2 - sirina/2, d.height/2 - visina/2, sirina, visina);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));

//Adding a layered pane so I can place items inside the form more 'freely'
JLayeredPane layeredPane = new JLayeredPane();
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);

    //Adding a table
JTable table = new JTable();
String[] rowData = {"Name:", "Price:", "Cathegory:", "Sum:"};
DefaultTableModel model = new DefaultTableModel(rowData, 0);
JScrollPane skrol = new JScrollPane(table);
table.setModel(model);
//The 2 lines below work as intended
ResultSet rs = (ResultSet) bp.query("SELECT * FROM table"); //This calls a query        
popuniTabelu(rs, model); //This populates the table.
table.setBounds(10, 110, 500, 350);
table.setEnabled(false);
table.setShowHorizontalLines(false);
layeredPane.add(table);

Populating the table and displaying it isn't the problem, there's enough information inside the table 'table' that the user even needs to scroll down.

But that's where the problem begins, the scroll doesn't show up. How do I implement it from here. I tried following solutions I found on google, but they pretty much sum up on:

JScrollPane scroll = new JScrollPane(table);

Which simply doesn't work in my case.

The problem may be trivial, if it is, I'm sorry, I'm still learning swing. Also, sorry for my bad english, it's not my native language. :)

Also, if there's something I forgot to include, please let me know.

Thank you!

Toza
  • 1,348
  • 2
  • 14
  • 35
  • do you have the full program? – tim May 15 '15 at 09:10
  • 2
    @tim *"do you have the full program?"* The 'full program' might be thousands of lines of code. Typically we prefer an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Hard code some data to replace the DB. Oh, and.. – Andrew Thompson May 15 '15 at 09:15
  • 1
    .. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 15 '15 at 09:15
  • @tim as Andrew said, I have around 10 classes now, with about 150 lines each. I don't think anyone would have the nerve to go through them for a simple problem, which could have been answered with just the code above. By the way, Andrew, thanks for that source, it helped a lot! – Toza May 15 '15 at 09:43

1 Answers1

3

You add your table to 2 components :

JScrollPane skrol = new JScrollPane(table);

and

layeredPane.add(table);

Because of Swing component can have just one parent component second statment override first, so your JScrollPane is empty. Seems you need to remove layeredPane.add(table);

As mentioned here

Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

alex2410
  • 10,904
  • 3
  • 25
  • 41