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!
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.
Related
I have a tablePanel which is a JScrollPane,and initialized with a JTable, the JTable initialized with a defaultTableModel.When I trying to add some rows to the table, but didn't see the scroll bar, appriciated for any reply.
JFrame jFrame = new JFrame();
//rows will be added dynamically.
DefaultTableModel defautTableModel = new DefaultTableModel(null,columnNames){
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable jTable = new JTable(defautTableModel);
jTable.setLocation(20,60);
jTable.setSize(950,450);
jTable.setRowHeight(25);
jTable.getColumn("No.").setMaxWidth(45);
jTable.getColumn("position").setMaxWidth(45);
...
JTableHeader jTableHeader = jTable.getTableHeader();
jTableHeader.setLocation(20,30);
jTableHeader.setSize(950,30);
jTableHeader.setFont(new Font(null, Font.BOLD, 16));
jTableHeader.setResizingAllowed(true);
jTableHeader.setReorderingAllowed(true);
JScrollPane tablePanel = new JScrollPane(jTable);
tablePanel.setLayout(null);
tablePanel.add(jTableHeader);
tablePanel.add(jTable);
jFrame.setContentPane(tablePanel);
tablePanel.setLayout(null); is the primary cause of your problem. A JScrollPane has its own layout manager which is used to manage the scrollbars, view port and headers.
tablePanel.add is your next problem, as you shouldn't be adding components to the JScrollPane. Instead, you should be setting the JScrollPane's JViewPort.
But, since you're using JScrollPane tablePanel = new JScrollPane(jTable);, there's actually no need for the three lines which follow it.
I would highly recommend that you take a closer look at:
How to us tables
How to use scroll panes
Laying Out Components Within a Container
Now, before you tell me how nothing I've suggested actually works, go and re-read Laying Out Components Within a Container - this is the corner stone concept you will need to understand and master before Swing really begins to work for you
JScrollPane tablePanel = new JScrollPane(jTable);
// No need for the below code
/*tablePanel.setLayout(null);
tablePanel.add(jTableHeader);
tablePanel.add(jTable);*/
jFrame.setContentPane(tablePanel);
I created a JFrame with Java Swing using a BorderLayout. Both east and west layout are fixed with some components, while BorderLayout at the center can change depending on what the user does. At the beginning there is a picture,
panelDinamycCenter = new JPanel ();
JScrollPane = new JScrollPane (panelDinamycCenter);
panelDinamycCenter.add (new JLabel ( "", new Imagelcon ( "......."), JLabel.CENTER));
jFrame.getContentPane (). add (JScrollPane, BorderLayout.CENTER);
Then a data entry screen, for which I used a GridBagLayout. In order to remove the picture and to insert the new screen I used this:
panelDinamycCenter.removeAll (),
then I add the various components. After
setVisible (true);
I have now created a JTable, to be included in panelDinamycCenter,
panelDinamycCenter.removeAll ();
String [] [] matrixValori = new String [arrayOggCreaJTableDeroghe.length] [arrayJTableNomeColumnDeroghe.length];
for (int i = 0; i <arrayOggCreaJTableDeroghe.length; i ++) {
matrixValori [i] = arrayOggCreaJTableDeroghe [i] .creaArrayString ();
}
DefaultTableModel model = new DefaultTableModel (matrixValori, arrayJTableNomeColumnDeroghe);
JTable JTable = new JTable (model);
jTable.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
panelDinamycCenter.add (JTable);
jFrame.setVisible (true);
All right, but I can not see the names of the columns. Why?
As shown in How to Use Tables: Adding a Table to a Container, "The scroll pane automatically places the table header at the top of the viewport." At a minimum, you need to replace the JScrollPane removed by removeAll():
JTable jTable = new JTable(model);
panelDinamycCenter.add(new JScrollPane(jTable));
Among alternatives, consider these:
Add the table header to PAGE_START, as suggested here.
Instead of removing and restoring, use CardLayout to switch between views.
I have the following code:
JList<Song> list = new JList<Song>(this.lib);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(3);
list.setSize(1, 1);
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
I am not sure what is wrong here. When I look at the JFrame, I see the list in a single box with all 9 items that I have in my list. I tried messing with the size to see if I could get that to work but it isn't. The size doesn't seem to change no matter what I set it to.
My goal is to have a vertical and horizontal scroll-bar when necessary and a JList that is of some fixed size (fixed compared to the frame size would be best if possible).
You're adding the list to the container, not the listScroller.
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
You should try using something like...
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
//add(list);
add(listScroller);
I think you will also find list.setSize(10, 10); is pointless, as the JScrollPane (actually the viewport, but lets keep it simple) has it's own layout manager.
You should take a look at Jlist#setVisibleRowCount if you want to affect the number of visible rows a JList will show before it needs to be scrolled.
The actual width and height of each row is determined by the ListCellRenderer, but you can use JList#setPrototypeCellValue to affect how these might be calculated.
I wrote a small function that will display a table inside a dialog and am looking for advice on what to clean up and what is better programming practice when it comes to dealing with swing.
What improvements can be made to my code
//constraints for panel to fill out the frame
GridBagConstraints grid = new java.awt.GridBagConstraints();
grid.fill = java.awt.GridBagConstraints.BOTH;
grid.weightx = 1.0;
grid.weighty = 1.0;
//create jtable based on a table model created from an array
JTable table = new JTable(testModel); //a method creates my test model
table.add(table.getTableHeader(), BorderLayout.PAGE_START);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(testModel);
table.setRowSorter(sorter);
//add scrollpane for visibility
JScrollPane jscrollpane = new JScrollPane(table);
table.setFillsViewportHeight(true);
//add the scrollpane to a panel
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(jscrollpane, grid);
//create for use with the dialog
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame, "My Test Dialog", true);
dialog.add(panel);
dialog.pack();
dialog.setLocationRelativeTo(null); //added as advice of Stripies
dialog.setVisible(true);
I'm open to all constructive criticism as my goal is to learn the proper techniques for programming with swing.
To clarify, I'm looking whether I can take anything out or improve any of it.
What are the benefits of using setLocationByPlatform(true)?
Using setLocationRelativeTo(null) is a convenient choice for examples, demos and utilities. Quality applications preserve the user's preferred position, possibly recording the most recent setting in an instance of java.util.Preferences. Because a user's experience varies by platform, setLocationByPlatform(true) represents the implementer's best effort to meet that expectation. It's a better choice for the default location when no preference yet exists.
INTRO:
I created a java application using JFrame. I have a JMenuBar at the top and under that I'd like to display rows of text.
PURPOSE:
When I have 50 rows and only 20 are displayable at once, I'd like to be able to scroll down and back up again.
PROBLEM:
Of course, my theory doesn't wanna work as it should. My problem is that I don't know how to add a vertical scroll properly.
QUESTION:
How should I change this code to reach my goal?
public void display(){
Container content = this.window.getContentPane();
content.setLayout(new BorderLayout());
Border border = LineBorder.createGrayLineBorder();
//this is just a sample
for(int i = 0;i<50;i++){
JLabel lab = new JLabel("lonyaladek");
lab.setSize(570, 20);
lab.setBorder(border);
lab.setLocation(10, 20+(i*25));
content.add(lab);
}
//scroll
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
content.add(sb);
}
First you need to start with a layout manager that allows you to add multiple components to the container. Maybe a GridLayout is the best place to start.
Then you add this container to the scrollPane and then you add the scrollpane to the window.
So the basic code would be:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(...);
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
window.add(scrollPane, BorderLayout.CENTER);
I suggest you read the section from the Swing tutorial on How to Use Scroll Panes for more info.