Two JTables over each other - java

As a part for my first GUI App I want to show 2 Tables the following way:
http://i.stack.imgur.com/6mq0m.jpg
I´m unable to print the 2 tables that way!
Here is my code so far:
// Center
JPanel panel_center = new JPanel();
panel_overview.add(panel_center, BorderLayout.CENTER);
panel_center.setLayout(new BorderLayout());
JPanel panel_center_table = new JPanel();
panel_center.add(panel_center_table, BorderLayout.NORTH);
panel_center_table.setLayout(new GridLayout(2, 1));
JPanel panel_table_north = new JPanel();
panel_center_table.add(panel_table_north);
JPanel panel_table_south = new JPanel();
panel_center_table.add(panel_table_south);
JPanel panel_center_combobox = new JPanel();
panel_center.add(panel_center_combobox, BorderLayout.NORTH);
panel_center_combobox.setLayout(new BorderLayout());
panel_center_combobox.add(combobox_table_chooser, BorderLayout.WEST);

Consider using a different layout manager. I'd recommend using a BoxLayout, since you'll be able to easily stack components on top of each other. Here's a How To Use BoxLayout tutorial.

You could also try using a split pane. That way, the user can control the height of each of the tables.

Related

Can I use multiple grids?

I am trying to figure out the layout for this(the rest of the code is in the early stages) but for this block, I am trying to figure out the best(and doable) way to format it. I want it to be an 8x8 grid that I will eventually populate with the treasure/empty buttons but I also need a title up top as well as some labels and text on the left. I am unsure if I am able to do multiple grids but what I did below is try to create a 1x2 grid and then place two other grids inside, one with the info on the left(3x2), and another with the 8x8 grid for the buttons. I know it's not close to what it needs to be but none of the grids are showing up at all(it's just putting the title and then one column with 8 rows) and I wanna know if I'm even on any sort of right track, or if I'm just making things up at this point. Any tips would be appreciated, or resources about possibly nesting the grids? I can't find anything in my book about That specifically.
private void buildPanel()
{
// Create labels to display the
treasuresLeftLabel = new JLabel("Treasures left: ");
treasuresFoundLabel = new JLabel("Treasures found: ");
triesLeftLabel = new JLabel("Tries left: ");
// Create text fields for each label
treasuresLeftTextField = new JTextField(2);
treasuresLeftTextField.setEditable(false);
treasuresLeftTextField.setText(String.valueOf(20-game.getTreasuresFound()));
treasuresFoundTextField = new JTextField(2);
treasuresFoundTextField.setEditable(false);
treasuresFoundTextField.setText(String.valueOf(game.getTreasuresFound()));
triesLeftTextField = new JTextField(2);
triesLeftTextField.setEditable(false);
triesLeftTextField.setText(String.valueOf(game.getTriesLeft()));
emptyButton = new EmptyButton();
emptyButton.addActionListener(new emptyButtonListener());
treasureButton = new TreasureButton();
treasureButton.addActionListener(new treasureButtonListener());
// new JPanel object referenced by panel
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
// Add a gridlayout to the content pane
panel.setLayout(new GridLayout(1, 2));
panel.setLayout(new GridLayout(3, 2));
panel.add(treasuresLeftLabel);
panel.add(treasuresLeftTextField);
panel.add(treasuresFoundLabel);
panel.add(treasuresFoundTextField);
panel.add(triesLeftLabel);
panel.add(triesLeftTextField);
panel.setLayout(new GridLayout(8, 8));
panel.add(treasureButton);
panel.add(emptyButton);
}
You can't use multiple grids within the same JPanel - one panel, one layout manager.
But you can nest layout managers (and thereby grids) by using nested panels.
For example you could use a BorderLayout for the first panel (containing the title at the top, the info panel on the left and the button panel in the center.
The code to construct those panel then might look like this:
// panel contains the complete UI
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
panel.setLayout(new BorderLayout());
panel.add(new JLabel("This is the Title"), BorderLayout.PAGE_START);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(3, 2));
infoPanel.add(treasuresLeftLabel);
infoPanel.add(treasuresLeftTextField);
infoPanel.add(treasuresFoundLabel);
infoPanel.add(treasuresFoundTextField);
infoPanel.add(triesLeftLabel);
infoPanel.add(triesLeftTextField);
panel.add(infoPanel, BorderLayout.LINE_START);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8, 8));
buttonPanel.add(treasureButton);
buttonPanel.add(emptyButton);
for (int i = 0; i < 62; i++) {
buttonPanel.add(new JButton(String.format("%02d", i)));
}
panel.add(buttonPanel, BorderLayout.CENTER);

When i increase my GUI window size to full the components go back to standard layout

When i increase the window size to full, my components goes back to standard layout(jtable,button1,button2,button3) and so on. so i wonder if my code is right and how i can decrease the window size.
JTabbedPane jtabbed = new JTabbedPane(JTabbedPane.TOP);
JPanel panel=new JPanel();
tabellinnhold = new DefaultTableModel(defaulttabell,kolonnenavn);
posttabell = new JTable(tabellinnhold);
rullefelt = new JScrollPane(posttabell);
koble = new JButton("koble til");
lukke = new JButton("lukke");
hente = new JButton("Hente data");
avslutt = new JButton("Avslutt");
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);
//action listener
koble.addActionListener(this);
lukke.addActionListener(this);
hente.addActionListener(this);
avslutt.addActionListener(this);
jtabbed.add("se post",panel);
add(jtabbed);
//////////////////////////////////////////////////
Grensesnitt p = new Grensesnitt();
p.setDefaultCloseOperation(EXIT_ON_CLOSE);
p.GUIcode();
p.setTitle("title");
p.setSize(500,700);
p.setVisible(true);
JPanel panel=new JPanel();
...
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);
The default layout manager for a JPanel is a FlowLayout which will simply display all the components on a single line.
You can't just use the BorderLayout constraints and expect it to work.
If you want to use a BorderLayout then the code should be:
//JPanel panel=new JPanel();
JPanel panel=new JPanel( new BorderLayout() );
Also, you can't add 4 components to the "SOUTH" of the BorderLayout. You can only add a single component. So you need to create a child panel and add your components to that first:
JPanel south = new JPanel();
south.add(koble);
south.add(lukke);
south.add(hente);
south.add(avslutt);
panel.add(south, Borderlayout.SOUTH);
Read the section from the Swing tutorial on Using Layout Manager for more information and working examples to get you started.
Keep a link to the tutorial handy for examples of all Swing basics.

JPanel on top of another JPanel

I have been using JPanels for a while and now want to place a JPanel on top of another JPanel.
I have looked at using JLayer but I was wondering If there is a solution to just set the layer of the bottom and top, I don't want to set each components layer if possible.
Example
JPanel bottomPanel = new JPanel(); # Set as bottom panel
JPanel topPanel = new JPanel(); # Set as top panel
JPanel sidePanel = new JPanel(); # Don't have to set
JPanel anotherSidePanel = new JPanel(); # Don't have to set
If this isn't possible what is the best solution for this, Thanks.
You can have the main panel use a BorderLayout.
Then you can do something like:
mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);
It sounds like what you want is a layout manager. There are a few different ones that suit different needs. There's a link at the bottom of this post.
My personal favorite is GridLayout. So for what you want to do, you can do this:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
That will do what you want.
If you wanted to read more about them, here's a link:
Oracle Docs on Layout Managers
I know this is quite late, but if anyone now has this issue, I suggest using a BoxLayout. BorderLayout can only have one cell in each of its five locations, and GridLayout's cells are all the same dimension. If you want to stack different sized JPanels, here's how BoxLayout can be implemented:
JFrame frame = new JFrame("Intro to BoxLayout");
JPanel container = new JPanel();
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(X1, Y1));
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(X2, Y2));
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(panel1);
container.add(panel2);
frame.add(container);
frame.pack();
frame.setVisible(true);
where X1, Y1, X2, Y2 are arbitrary panel dimensions.

Position of buttons in rows, in a JPanel based 'menu bar'

I'm trying Swing programming but I can't do what I want.
I would like to place a top bar button with 2 lines of button but I just have 1 line in my case.
Here is my code:
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
setMinimumSize(new Dimension(1000,500));
setMaximumSize(new Dimension(1000,500));
JPanel panelButton = new JPanel();
JPanel panelTopButton = new JPanel();
JPanel panelBottomButton = new JPanel();
panelTopButton.add(dashboard);
panelTopButton.add(journal);
panelTopButton.add(myPlans);
panelTopButton.add(myFavorites);
panelTopButton.add(shoppingCart);
panelBottomButton.add(profile);
panelBottomButton.add(notifications);
panelButton.add(panelTopButton, BorderLayout.NORTH);
panelButton.add(panelBottomButton, BorderLayout.SOUTH);
contentPane.add(panelButton,BorderLayout.NORTH);
//Display
setSize(400,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I have this
And I want this
Can somebody help me?
You need one panel for each line.
Try to do this:
JPanel panelButtonsL1 = new JPanel();
JPanel panelButtonsL2 = new JPanel();
panelButtonsL1.add(dashboard);
panelButtonsL1.add(journal);
panelButtonsL1.add(myPlans);
panelButtonsL1.add(myFavorites);
panelButtonsL1.add(shoppingCart);
panelButtonsL2.add(profile);
panelButtonsL2.add(notifications);
The default layour of JPanel is FlowLayout. Bear in mind that layout is very important to work with swing component disposition.
Define the bottom panel as GridLayout.
JPanel panelButton = new JPanel(new GridLayout(2, 1)); // 2 rows x 1 column
panelButton.add(panelButtonsL1);
panelButton.add(panelButtonsL2);
Details of GridLayout you can find on API.
You can achieve that using a GridLayout: assign a GridLayout to panelButton with two rows and one column, and then add the two panels to it.
According to what you want there is a simpler alternative by continue using the default FlowLayout from the panel. It is more appropriate than using GridLayout since you wanted the last 2 buttons to move to the next row and center itself.
If you use GridLayout, the buttons at the next row are likely going to be directly below one of the buttons above. Here are 2 ways to get what you want.
Method 1. Reduce the width of the main panel holding your buttons:
Dosing so, you will have to add the main panel using BorderLayout.CENTER.
Method 2. Add the buttons to a sub-panel of smaller width and add it to the main panel. All your buttons will be added to the smaller sub-panel:

JPanel create empty space between subPanels

I'm developing a Java application for homework. This is my code
JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
final JCheckBox queryHandlerCB = new JCheckBox();
JPanel checkBoxPanel = new JPanel(new FlowLayout());
checkBoxPanel.add(queryHandlerL);
checkBoxPanel.add(queryHandlerCB);
// Query Panel
// set image
picLabelQuery = new JLabel("",JLabel.LEFT);
picLabelQuery.setIcon(currentPicForm);
JPanel queryPanel = new JPanel();
final JButton queryButton = new JButton("Insert a query");
queryPanel.add(queryButton);
queryPanel.add(picLabelQuery);
// Panel create
final JButton createButton = new JButton("Create");
JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
createPanel.add(createButton);
JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2));
finalPanel.add(queryPanel);
finalPanel.add(checkBoxPanel);
finalPanel.add(createPanel);
finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));
setLayout(new GridBagLayout());
add(finalPanel);
I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.
This piece of code works but this is the result
How do I remove the space that is automatically created between the panels?
How do I remove the space that is automatically created between the panels?
Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.
Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.
Read the section from the Swing tutorial on Layout Managers for more information and examples.
You should pack() your surrounding panel or set the height to a desired value.

Categories

Resources