'How can I have a JFrame with a JComboBox over a JScrollPane
Basically I'm try to add a dropdown to my code here. Although whenever I try to add one after the other, it ends up overtaking it. If I run the dropdown before the scroll then I get the scroll on top and vice versa. I'm kinda new to JFrames and drawing in Java so please bear with me.
MyFrame Class:
import java.awt.BorderLayout;
import java.awt.FontFormatException;
import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
public void cadetImage() throws FontFormatException, IOException {
JPanel panel = new Puller();
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
String[] opts = { "All", "A", "B", "C", "D", "E", "F" };
JComboBox<String> dropdown = new JComboBox<>(opts);
setTitle("Puller");
scroll.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
add(dropdown, BorderLayout.CENTER);
add(scroll, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(500, 500);
setLocationRelativeTo(null);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
}
}
Puller Class:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Puller extends JPanel {
private int pw, ph;
private Font rso;
public Puller() throws FontFormatException, IOException {
InputStream is = new FileInputStream("assets/fonts/racing sans one.ttf");
Dimension screen = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
pw = (int) screen.getWidth();
ph = (int) screen.getHeight();
setPreferredSize(new Dimension(pw - 20, ph - 30));
rso = Font.createFont(Font.TRUETYPE_FONT, is);
}
@Override
public void paint(Graphics p) {
Graphics2D page = (Graphics2D) p;
template(page);
}
private void template(Graphics2D page) {
page.setColor(Color.decode("#666666"));
page.fillRect(0, 0, pw, ph);
// Name Header
page.setColor(Color.BLACK);
page.fillRect(997, 122, 311, 90);
page.setColor(Color.decode("#bf9000"));
page.fillRect(1000, 125, 305, 84);
page.setColor(Color.BLACK);
page.setFont(rso.deriveFont(Font.BOLD, 75f));
page.drawString("NAME", 1052, 193);
// Filter Field
page.setColor(Color.BLACK);
page.fillRect(997, 212, 70, 50);
page.setColor(Color.decode("#666666"));
page.fillRect(1000, 212, 64, 47);
}
}
Whatever help I can get would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
