error displaying JList in JPanel - java

Below is the code in which I create the JPanel
public class ListBranchesInterface extends JPanel {
private Library theLibrary; // reference to back end
private ArrayList<LibraryBranch> branches;
private DefaultListModel dlm;
private JList list;
private JScrollPane scroll;
public ListBranchesInterface(Library theLibrary) {
this.theLibrary = theLibrary;
branches = new ArrayList<LibraryBranch>();
branches.addAll(theLibrary.getLibraryBranches());
System.out.println(branches.size());
Iterator<LibraryBranch> iter = branches.iterator();
dlm = new DefaultListModel();
while (iter.hasNext()) {
dlm.addElement(iter.next().toString());
System.out.println("Added");
}
list = new JList(dlm); // create a JList from the default list model
scroll = new JScrollPane(list); // add a scroll pane to the JList
add(scroll);
setSize(400, 400);
setVisible(true);
}
}
when I add the panel to the JFrame nothing is displayed. (The error is not in adding it to the JFrame because I am adding other panels and they work properly.) What is the error?

Related

wrong parent for CardLayout in java

I want to change cards in my CardLayout (which contains labels) for every choice in my combo box. So when I select Item2 in the combo box it should show the second card but it returns error instead.
Inside the method initComponents() I successfully showed the first card using cardLayout.show(imagePanel, "1"); but when I tried to do the same inside private void comboMenuActionPerformed(), it returns the error "IllegalArgumentException: wrong parent for CardLayout". Why is this happening?
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
}
private void initComponents() {
cardLayout = new java.awt.CardLayout();
mainPanel = new javax.swing.JPanel();
centerPanel = new javax.swing.JPanel();
imagePanel = new javax.swing.JPanel(cardLayout);
comboMenu = new javax.swing.JComboBox<>();
JLabel firstPicture = new JLabel("");
JLabel secondPicture = new JLabel("");
...
firstPicture.setIcon(...);
secondPicture.setIcon(...);
imagePanel.add(firstPicture, "1");
imagePanel.add(secondPicture, "2");
String[] menu = {"Item1", "Item2", "Item3"};
cardLayout.show(imagePanel, "1"); //this works fine
imagePanel.setLayout(new java.awt.CardLayout());
centerPanel.add(imagePanel);
comboMenu.setModel(new javax.swing.DefaultComboBoxModel<>(menu));
mainPanel.add(centerPanel);
}
private void comboMenuActionPerformed(java.awt.event.ActionEvent evt) {
if(comboMenu.getSelectedItem().toString().equals("Item2")) {
cardLayout.show(imagePanel, "2"); //WHY THIS DOESN'T WORK
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
private javax.swing.JComboBox<String> comboMenu;
private javax.swing.JPanel centerPanel;
private javax.swing.JPanel imagePanel;
private javax.swing.JPanel mainPanel;
private java.awt.CardLayout cardLayout;
}
imagePanel = new javax.swing.JPanel(cardLayout);
...
cardLayout.show(imagePanel, "1"); //this works fine
imagePanel.setLayout(new java.awt.CardLayout());
You replace the layout of the image panel with a new instance of the CardLayout. Get rid of the last statement:
//imagePanel.setLayout(new java.awt.CardLayout());
You assign the card layout to imagePanel by:
imagePanel = new javax.swing.JPanel(cardLayout);
But then you assign a new card layout by:
imagePanel.setLayout(new java.awt.CardLayout());
This overwrites the first card layout to which you added the labels.

How to make JScrollPane dynamic adjusting

I am trying to make a dynamic tabs with the option to add new ones in the application and some buttons next to it... To do so I have a main class:
private static void initAndDisplayUI() {
frame = new JFrame(...)
tabbedPane = new TabbedPane();
insertTab(tabbedPane, TabFactory.createTab(), true);
insertNewTabButton(tabbedPane);
...
}
}
Container class:
public class TabbedPane extends JPanel {public TabbedPane() {
this.captions = new TabCaptions();
this.tabs = new ArrayList<Tab>();
this.contentContainer = new JPanel(new BorderLayout());
setLayout(new BorderLayout());
add(captions, BorderLayout.NORTH);
add(contentContainer, BorderLayout.CENTER);
}
...
}
And a TabCaptions:
public class TabCaptions extends JPanel {
private TabCaption selectedTab;
private JComponent tabsPane;
private JScrollPane scrollPane;
private JPanel buttonsPane;
public TabCaptions() {
createUI();
}
private void createUI() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBackground(Color.DARK_GRAY);
add(createTabsPane());
add(createButtonsPane());
add(Box.createHorizontalGlue());
}
private JComponent createTabsPane() {
tabsPane = new JPanel();
tabsPane.setOpaque(false);
tabsPane.setLayout(new BoxLayout(tabsPane, BoxLayout.X_AXIS));
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
}
...
}
As a result I have a region with the tabs and some button next to it. However the app window draws a scrollPane with some weird size... I would like to display this "add new tab" button right next to the tabs that are created, resize it when the new tab is being added BUT with the functionality to display a scrollbar once it hits the maximum window width. I already have the scrolling but how can I make the behaviour of this dynamic position of new page?
I already have the scrolling
Not based on the code you have provided:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
You need to add the JScrollPane to the frame, so you need to return the scroll pane from the method:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//return tabsPane;
return scrollPane;

Scrollpane in multi-panel tab of JTabbedpanes

Good day all,
I have a JTabbedPane of which each tab has its own class. Each of its ActionListener also has it's own seperated class.
One ActionPerformed happens to add a JTable with columns after a certain amount of days is set.
This is one tab:
public class FirstTab {
private JPanel mainPane;
private JPanel weeksPane;
private JButton setWeeksB;
private JLabel matchCheck;
private JPanel modulesPane;
public FirstTab() {
mainPane = new JPanel();
weeksPane = new JPanel();
setWeeksB = new JButton("Voeg weken toe");
matchCheck = new JLabel();
//mainPane.setLayout(new FlowLayout());
mainPane.add(createSchoolDaysPane());
}
public JPanel createSchoolDaysPane() {
JPanel schoolDaysPane = new JPanel();
DefaultTableModel weeksModelTable = new DefaultTableModel();
weeksModelTable.addColumn("Week nummer");
weeksModelTable.addColumn("Dagen");
JTable weeksTable = new JTable(weeksModelTable);
JLabel schoolDaysL = new JLabel("Geef hier het aantal schooldagen (max 200) van het jaar in: ");
JTextField schoolDaysF = new JTextField(10);
SetWeeksPaneAction AWFA = new SetWeeksPaneAction(mainPane, weeksPane, weeksModelTable, weeksTable, matchCheck, setWeeksB);
schoolDaysF.addActionListener(AWFA);
JLabel schoolDaysGivenL = new JLabel();
schoolDaysPane.setLayout(new BoxLayout(schoolDaysPane, BoxLayout.Y_AXIS));
schoolDaysPane.add(schoolDaysL);
schoolDaysPane.add(schoolDaysF);
schoolDaysPane.add(schoolDaysGivenL);
return schoolDaysPane;
}
public JPanel getTab() {
return mainPane;
}
This tab contains two panels, the JTable shown is added to the panel after the amount of days is set.
GUI
As you notice the columns are not visible (but they are set), so I have been told to use a ScrollPane instead.
However, I am not able to pass through a ScrollPane in my JTabbed because a normal pane ('MainPane' - container) is passed through:
public EindopdrachtJava3() {
frame = new JFrame();
jtp = new JTabbedPane();
firstTab = new FirstTab();
secondTab = new SecondTab();
jtp.addTab("Modules", firstTab.getTab());
jtp.addTab("Samenvatting schooljaar", secondTab.getTab());
frame.getContentPane().add(jtp);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationByPlatform(true);
frame.setVisible(true);
modules = new HashSet<Module>();
weeks = new HashSet<Week>();
}
public static void main(String[] args) {
new EindopdrachtJava3();
}
This tab contains multiple panels and gets added to my JFrame in the Main class
JFrame {
JTabbedPane {
Tab 1 {
Panel 1
ScrollPane(JTabel) - But not possible(?)
}
Tab 2 {
....
}
}
I would like to keep it modular as possible since that is currently my learning curve in GUI

Why is my JList/JScrollPane so small?

When I run my code the JList shows up but it's really small, and when I try to change its size it won't change size. What am I doing that's wrong? A lot of the GUI formatting is new to me. Thanks.
public class Panel extends JPanel
{
//Fields
private Dimension dimensions;
private JButton button1;
private JList list;
private JScrollPane listScroller;
private String title;
private DefaultListModel model;
/**
* Constructor
*/
public Panel()
{
dimensions = new Dimension(400, 300);
setPreferredSize(dimensions);
setBackground(Color.WHITE);
setLayout(null); //Lets me lay my components out freely
button1 = new JButton("New Set");
button1.setBounds(new Rectangle(new Point(150, 250), button1.getPreferredSize()));
button1.addActionListener(new ButtonListener());
add(button1);
String[] titleArray ={title};
model = new DefaultListModel();
for(String title :titleArray)
{
model.addElement(title);
}
list = new JList(model);
list.setBounds(new Rectangle(new Point(200,200), list.getPreferredSize()));
add(list);
listScroller = new JScrollPane(list);
listScroller.setBounds(new Rectangle(new Point(200,200), listScroller.getPreferredSize()));
add(listScroller);
}
}

JScrollPane not showing up on inherited JPanel

I have a class which extends a JPanel. Code below:
public class Test extends JPanel implements Testnterface {
private JScrollPane listScroller;
DefaultListModel model;
private JList requestList;
public Test() {
String title = "Stackoverflow Question";
setBorder(BorderFactory.createTitledBorder(title));
model = new DefaultListModel();
requestList = new JList(); // create a list
listScroller = new JScrollPane(requestList); // create a scrollbar to the list
listScroller.setPreferredSize(new Dimension(250, 80));
setLayout(null);
add(listScroller); // even though I add the scrollPane, nothing is displayed
}
}
I updated the code. I can't seem to get my JScrollPane to be displayed on my JPanel even though I add it. Anyone got any ideas?
I had the same issue and solved it by wrapping scroll pane with JPanel:
final JScrollPane jScrollPane = new JScrollPane(list,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
final JPanel panel = new JPanel(new BorderLayout(BORDER_SIZE, BORDER_SIZE));
panel.setBorder(BorderFactory.createEmptyBorder(BORDER_SIZE, BORDER_SIZE,
BORDER_SIZE, BORDER_SIZE));
panel.add(jScrollPane);

Categories