I have a JPanel with a CardLayout and a JComboBox inside it. They are dinamically filled with data taken from a JTable. If the JComboBox has a single item it shows just fine, but if I fill it with more than one it doesn't show.
JPanel intervalPanel = new JPanel;
CardLayout intervalLayout = new CardLayout();
intervalPanel.setLayout(intervalLayout);
JComboBox intervalComboBox = new JComboBox();
for (int i = 0; i < table.getRowCount(); i++) {
String name = (String) table.getValueAt(i, 0);
intervalComboBox.addItem(name);
JPanel p = new JPanel();
p.setName(name);
p.add(intervalComboBox);
p.add(new JLabel(name));
intervalPanel.add(p, name);
}
A Swing component can only have a single parent.
p.add(intervalComboBox);
The above statement keeps removing the combo box from the previous panel and adds it to the current panel.
but if I fill it with more than one it doesn't show.
So the reason it doesn't show is because it is only visible on the last card, but you current see the first card.
The better solution is to NOT add the combo box to the panel in the CardLayout. Instead your main panel should use a BorderLayout. Then the basic logic would be something like:
JPanel main = new JPanel( new BorderLayout() );
JComboBox comboBox = new JComboBox(...);
main.add(comboBox, BorderLayout.PAGE_START);
JPanel card = new JPanel( intervalLayout );
main.add(card, BorderLayout.CENTER);
frame.add( main );
Then you just add child panels to the "card" panel as required.
Read the section from the Swing tutorial on How to Use CardLayout for a complete working demo that uses the above design concept.
Related
im trying to make a program to add an admin to a ms access database
I researched many times, figured out all the components need to be in a panel, and only the same type of J stuff can be in a panel, so i made many panels and combined them in a big panel
//frame details
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
JFrame aFrame = new JFrame("Add admin");
aFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel declaration
JPanel BigPanel = new JPanel();
JPanel adminnameenter = new JPanel();
JPanel typeadminname = new JPanel();
JPanel adminlastnameenter = new JPanel();
JPanel typeadminlastname = new JPanel();
JPanel buttonaddadmin = new JPanel();
//labels, textfields, and buttons
JLabel newAdminName = new JLabel("Enter admin name");
JTextField adminName = new JTextField(7);
JLabel newadminlastname = new JLabel("Enter admin last name");
JTextField adminlastname = new JTextField(7);
JButton addadmin = new JButton("Add Admin");
//add things to panel
adminnameenter.add(newAdminName);
typeadminname.add(adminName);
adminlastnameenter.add(newadminlastname);
typeadminlastname.add(adminlastname);
buttonaddadmin.add(addadmin);
//add things to big jPanel
BigPanel.add(adminnameenter);
BigPanel.add(typeadminname);
BigPanel.add(adminlastnameenter);
BigPanel.add(typeadminlastname);
BigPanel.add(buttonaddadmin);
//add things to frame
aFrame.add(BigPanel);
the only thing that popped up was a frame that said add admin
Add this code to the end of your function:
aFrame.setVisible(false);
aFrame.setVisible(true);
Or alternatively put
aFrame.setVisible(true);
at the end of your function instead of the beginning.
And all the components will appear. This is because whenever you change anything to your JFrame, it will only change on the user side once it is told to resize or refresh the frame. Also you don't need to put every single component in it's own JPanel, you can simply insert them directly in your BigPanel (small nitpick, but the b in bigPanel, shouldn't be capitalized, seeing as variables start with non-capitalized letters).
Also look into LayoutManagers, they will probably be useful for your application.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
How I can display the same JCombox in different JPanel.
In my code it displays just in the last JPanel.
for (int i=1; i<=nb_client; i++) {
JPanel panel=new JPanel();
String titre="client"+i;
tabbedPane.add(titre, panel);
combox.setPreferredSize(new Dimension(100, 20));
panel.add(combox);
tabbedPane.validate();
}
how i can display the same Jcombox in diffrent Jpanel in my code it displays just in the last Jpanel
You can't.
A component can only have a single parent, so it will only ever display in the last panel you add the combo box to.
However you can share the model of the combo box:
JComboBox comboBox1 = new JComboBox(...);
JComboBox comboBox2 = new JComboBox( comboBox1.getModel() );
Now when you select an item in one it will also be selected in the other.
If you don't want this behaviour then you need to copy the data from one combo box to the other. So in this case you will need to write a loop. I'll let you write the loop to copy the data.
I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!
Trying to add 3 panels i created to a frame in Java with the JSplitPane. Have tried this with 2 panels, and that worked great, but with 3 it still does not do what i want.
Have read something about making 2 JSplitPanes and put the one in the other, but that does not actually work what i would like it to do.
My code shows that there are 3 panels, but the size are all wrong.. it should be filled out.
My code:
frame = new JFrame(); // Create a new frame
frame.setVisible(true); // Makes it visible
frame.setSize(900, 500); // Sets size
frame.setTitle(""); // Sets title
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Sets the window on the center of the screen
temp_panel = new JPanel(); // Creates new JPanel
water_panel = new JPanel(); // Creates new JPanel
power_panel = new JPanel(); // Creates new JPanel
temp_panel.setBackground(Color.decode("#2ecc71")); // Sets color
water_panel.setBackground(Color.decode("#3498db")); // Sets color
power_panel.setBackground(Color.decode("#f1c40f")); // Sets color
temp_label = new JLabel("This is Temperature");
water_label = new JLabel("This is Water consumption");
power_label = new JLabel("This is Power consumption");
// Add labels on panel
temp_panel.add(temp_label);
water_panel.add(water_label);
power_panel.add(power_label);
JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JSplitPane splitPaneRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneLeft.setLeftComponent( temp_panel );
splitPaneLeft.setRightComponent( water_panel );
splitPaneRight.setLeftComponent( splitPaneLeft );
splitPaneRight.setRightComponent( power_panel );
splitPaneLeft.setEnabled(false);
splitPaneLeft.setDividerSize(0);
splitPaneRight.setEnabled(false);
splitPaneRight.setDividerSize(0);
// put splitPaneRight onto a single panel
JPanel panelSplit = new JPanel();
panelSplit.add( splitPaneRight );
frame.add(panelSplit, BorderLayout.CENTER);
It should look like this, but just with 3 panels with 3 different colors instead of 2!
Hope someone can help
If you don't need to change the relative sizes of the components during runtime, don't use a JSplitPane. Instead create a container JPanel that uses GridLayout, say new GridLayout(1, 0) for 1 row and variable number of columns, add your three colored JPanels to the GridLayout-using JPanel, and add this then to the JFrame.
you could make one of the panels another JSplitPane, unfortunately there is no other solution for this.
In a part of my software I have a layout on bottom that holds couple of JButtons and a JLabel. I want to keep buttons one the right side of the panel, and label on the left side. I could manage to put buttons on the right, but do not know how to keep the JLabel on the left side.
Here is the code:
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
ftpBack = new JButton("Back");
ftpNext = new JButton("Next");
label = new JLabel("Text);
bottomPanel.add(label);
bottomPanel.add(ftpBack);
bottomPanel.add(ftpNext);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
This is what I am trying to achieve:
Any idea how to make it ?
You can't do this with a FlowLayout.
You can use a horizontal BoxLayout:
Box box = Box.createHorizontalBox();
box.add(label);
box.add(Box.createHorizontalGlue());
box.add(backButton);
box.add(Box.createHorizontalStrut(5));
box.add(nextButton);
Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples.
Or another approach is to nest layout managers:
JPanel main = new JPanel( new BorderLayout() );
main.add(label, BorderLayout.WEST);
JPanel buttonPanel= new JPanel();
buttonPanel.add(back);
buttonPanel.add(next);
main.add(buttonPanel, BorderLayout.EAST);