0

I am trying to write a program with Java swing in IntelliJ. I have designed a GUI with IntelliJ's drag and drop user interface. However, my Jlist does not appear in my Jscrollpane. IntelliJ says that I don't have a GridManager for my frame, but I tried adding a GridManager and it messed up my GUI format. Can somebody help me? Thanks!

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

  public class GUIF {

private JButton button5;
private JList list1;
private JButton button1;
private JButton button4;
private JButton button2;
private JButton button3;
private JTextField textField1;
private JPanel Panel;
private JScrollPane scrollpane1;

public GUIF() {


  this.listOps();


  button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
      String data = "";
      if (list1.getSelectedIndex() != -1) {
        data = "Item selected: " + list1.getSelectedValue();
        textField1.setText(data);
      }
      textField1.setText(data);
    }
  });
}

private void prepGUI() {
  JFrame frame = new JFrame("Store");
  frame.setContentPane(new GUIF().Panel);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);

}

private void listOps(){
  DefaultListModel<String> items = new DefaultListModel<>();
  items.addElement("chocolate");
  items.addElement("milk");


  list1 = new JList(items);
  list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  list1.setSelectedIndex(0);
  list1.setVisibleRowCount(10);
  list1.setVisible(true);


  scrollpane1.add(list1);

  scrollpane1.setVisible(true);

}

public static void main(String[] args) {
  GUIF guif = new GUIF();
  guif.prepGUI();

}

  }
Chris Zhu
  • 21
  • 3
  • 1
    You've not created a JScrollPane object anywhere (e.g., no where do we see in your code above `new JScrollPane(...)`), and you can't use it until you first do this. This is such a basic Java issue that it suggests that you're putting the cart before the horse-- you're trying to do complex GUI coding before understanding the very basics of the language. Please don't do this but instead go through the tutorials and a text book to learn the rudiments. Same for use of a drag and drop GUI builder -- avoid using these before first understanding how the GUI library works. – Hovercraft Full Of Eels Aug 04 '17 at 21:34
  • If you have in fact created a new JScrollPane but it's in code not shown, then we can't help you until you post code that we can compile and run ourselves, code that shows where you add the JScrollPane to the GUI for instance (your code above does not show this). Also when/if you do create a new JScrollPane object, don't add the JList to it as you're doing. You never add components directly to the JScrollPane but rather to its viewport -- as the JScrollPane tutorials will tell you (suggestion: google and read these). – Hovercraft Full Of Eels Aug 04 '17 at 21:36
  • 1
    You have two `new GUIF()` statements in your code. This is wrong. I suggest you read the section from the Swing tutorial on [How to Use Lists](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html). Download the demo code from the tutorial and use it as a starting point, since the code is better structured then what you are trying to do. This will mean you are creating the GUI by hand instead of using the IDE. This is a better approach because you are leaning Java, not the IDE. Code generated by an IDE will not be maintainable if you move to another IDE. – camickr Aug 04 '17 at 21:38

0 Answers0