Users of my software need to be able to click on different tabs to see different types of data representations. However, the code I am including below does not show the requested data panel when the user clicks on a tab.
You can re-create the problem easily by running the code below, and then following these steps in the GUI which the code will produce:
1.) Select "New" from the file menu
2.) Click on "AnotherTab" in the internal frame which will appear
Depending on which line of code you comment out below, the tab will either just show a blank panel or will show a tiny red square in the middle of the top of the panel.
The lines of code that you can toggle/comment-out to recreate this problem are:
GraphPanel myGP = new GraphPanel();
//GraphPanel myGP = new GraphPanel(width,height);
These lines of code are in GraphGUI.java below.
Can anyone show me how to fix the code below so that myGP gets displayed at the full size of the panel containing it?
Here are the three java files required to recreate this problem:
ParentFrame.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
public class ParentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;
public ParentFrame() {
super("Parent Frame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(800, 400));
Panel p = new Panel();
this.add(p, BorderLayout.SOUTH);
desktop = new JDesktopPane();
setJMenuBar(createMenuBar());
this.add(desktop, BorderLayout.CENTER);
this.pack();
this.setSize(new Dimension(800, 600));
this.setLocationRelativeTo(null);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//Set up the File menu.
JMenu FileMenu = new JMenu("File");
FileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(FileMenu);
//Set up the first menu item.
JMenuItem menuItem = new JMenuItem("New");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("new");
menuItem.addActionListener(new OpenListener());
FileMenu.add(menuItem);
//Set up the second menu item.
menuItem = new JMenuItem("Quit");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
FileMenu.add(menuItem);
return menuBar;
}
class OpenListener implements ActionListener {
private static final int DELTA = 40;
private int offset = DELTA;
public void actionPerformed(ActionEvent e) {
// create internal frame
int ifWidth = 600;
int ifHeight = 300;
internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setLocation(offset, offset);
offset += DELTA;
// create jtabbed pane
JTabbedPane jtp = createTabbedPane();
internalFrame.add(jtp);
desktop.add(internalFrame);
internalFrame.pack();
internalFrame.setSize(new Dimension(ifWidth,ifHeight));
internalFrame.setVisible(true);
}
}
private JTabbedPane createTabbedPane() {
JTabbedPane jtp = new JTabbedPane();
jtp.setMinimumSize(new Dimension(600,300));
createTab(jtp, "One Tab");
createTab(jtp, "AnotherTab");
createTab(jtp, "Tab #3");
return jtp;
}
private void createTab(JTabbedPane jtp, String s) {
if(s=="AnotherTab"){
jtp.getHeight();
jtp.getWidth();
GraphGUI myGraphGUI = new GraphGUI(jtp.getHeight(),jtp.getWidth());
jtp.add(s, myGraphGUI);
}
else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
}
public static void main(String args[]) {
ParentFrame myParentFrame = new ParentFrame();
myParentFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {if ("quit".equals(e.getActionCommand())){System.exit(0);}}
}
GraphGUI.java: This is the one where you can toggle comments to re-create the problem.
import javax.swing.*;
class GraphGUI extends JPanel{
GraphGUI(int height,int width) {
//REPRODUCE ERROR BY COMMENTING OUT EITHER ONE OF NEXT TWO LINES:
GraphPanel myGP = new GraphPanel();
// GraphPanel myGP = new GraphPanel(width,height);
this.add(myGP);
this.setVisible(true);// Display the panel.
}
}
GraphPanel.java:
import java.awt.*;
import javax.swing.*;
class GraphPanel extends JPanel {
Insets ins; // holds the panel's insets
double[] plotData;
double xScale;
GraphPanel(int w, int h) {
setOpaque(true);// Ensure that panel is opaque.
setPreferredSize(new Dimension(w, h));// Set preferred dimension as specfied.
setMinimumSize(new Dimension(w, h));// Set preferred dimension as specfied.
setMaximumSize(new Dimension(w, h));// Set preferred dimension as specfied.
}
GraphPanel() {
setOpaque(true);// Ensure that panel is opaque.
}
protected void paintComponent(Graphics g){// Override paintComponent() method.
super.paintComponent(g);// Always call superclass method first.
int height = getHeight();// Get height of component.
int width = getWidth();// Get width of component.
System.out.println("height, width are: "+height+" , "+width);
ins = getInsets();// Get the insets.
// Get dimensions of text
Graphics2D g2d = (Graphics2D)g;
FontMetrics fontMetrics = g2d.getFontMetrics();
String xString = ("x-axis label");
int xStrWidth = fontMetrics.stringWidth(xString);
int xStrHeight = fontMetrics.getHeight();
String yString = "y-axis label";
int yStrWidth = fontMetrics.stringWidth(yString);
int yStrHeight = fontMetrics.getHeight();
String titleString ="Title of Graphic";
int titleStrWidth = fontMetrics.stringWidth(titleString);
int titleStrHeight = fontMetrics.getHeight();
//get margins
int leftMargin = ins.left;
//set parameters for inner rectangle
int hPad=10;
int vPad = 6;
int numYticks = 10;
int testLeftStartPlotWindow = ins.left+5+(3*yStrHeight);
int testInnerWidth = width-testLeftStartPlotWindow-ins.right-hPad;
int remainder = testInnerWidth%numYticks;
int leftStartPlotWindow = testLeftStartPlotWindow-remainder;
System.out.println("remainder is: "+remainder);
int blueWidth = testInnerWidth-remainder;
int blueTop = ins.bottom+(vPad/2)+titleStrHeight;
int bottomPad = (3*xStrHeight)-vPad;
int blueHeight = height-bottomPad-blueTop;
g.setColor(Color.red);
int redWidth = width-leftMargin-1;
//plot outer rectangle
g.drawRect(leftMargin, ins.bottom, redWidth, height-ins.bottom-1);
System.out.println("blueWidth is: "+blueWidth);
// fill inner rectangle
g.setColor(Color.white);
g.fillRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);
//write top label
g.setColor(Color.black);
g.drawString(titleString, (width/2)-(titleStrWidth/2), titleStrHeight);
//write x-axis label
g.setColor(Color.red);
g.drawString(xString, (width/2)-(xStrWidth/2), height-ins.bottom-vPad);
g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise
//write y-axis label
g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight);
g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise
// plot inner rectangle
g.setColor(Color.blue);
g.drawRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);
}
}
class GraphGUI extends JPanel {
.
.
GraphGUI(int height,int width) {
// components in a GridLayout are stretched to fit space available
setLayout(new GridLayout());
Related
I'm trying to create a jigsaw puzzle of sorts, where I have loaded an image onto a 5x5 grid, cropped the image and assigned each cropped part as an icon to 25 buttons arranged in the same pattern. I want to be able to drag my mouse pointer from a button to another and swap the icons on those 2 buttons.
I've tried using multiple MouseListener methods and MouseMotionListener methods, but nothing has worked so far.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.Component;
import javax.swing.*;
//import java.awt.image.*;
public class ImageMove {
JFrame jf;
JButton[][] grid;
JLabel test;
public static void main(String[] args) {
ImageMove ob = new ImageMove();
ob.start();
}
public void start() {
jf = new JFrame();
JPanel gridPanel = new JPanel();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("download.jpg");
Image temp1= img.getImage();
img=new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
Image img1 = img.getImage();
gridPanel.setLayout(new GridLayout (5,5));
grid = new JButton[5][5];
for(int y=0;y<5;y++) {
for(int x=0; x<5; x++) {
Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
ImageIcon icon = new ImageIcon(image);
JButton temp = new JButton(icon);
temp.setTransferHandler(new TransferHandler("icon"));
temp.addMouseMotionListener(new DragMouseAdapter());
grid[x][y]=temp;
gridPanel.add(grid[x][y]);
}
}
test= new JLabel();
jf.getContentPane().add(BorderLayout.NORTH, test);
jf.add(gridPanel);
jf.pack();
jf.setSize(500, 500);
jf.setVisible(true);
}
class DragMouseAdapter extends MouseAdapter{
private int x, y;
public void mouseDragged(MouseEvent e) {
final int x0=MouseInfo.getPointerInfo().getLocation().x;
final int y0=MouseInfo.getPointerInfo().getLocation().y;
x=x0;
y=y0;
JButton c = (JButton) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
public void mouseReleased(MouseEvent e) {
JButton c = (JButton) e.getSource();
Icon icon = c.getIcon();
grid[((int)(x/100))][((int)(y/100))].setIcon(icon);
}
}
}
Currently, the program copies the icon from the first button to the second button, i.e. it replaces the second button's icon with the first, but the first button remains the same. I expect to swap those two icons entirely. The MouseDragged method towards the end is performing the described behavior, but the MouseReleased doesn't seem to do anything at all.
Any help is much appreciated.
It's very easy. You simply need to save somewhere the drag source button, and on drop replace its icon with the old icon of drop target button.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
//import java.awt.image.*;
public class ImageMove {
JFrame jf;
JButton[][] grid;
JLabel test;
JButton dragSource; // here we save drag source component
public static void main(String[] args) {
ImageMove ob = new ImageMove();
ob.start();
}
public void start() {
jf = new JFrame();
JPanel gridPanel = new JPanel();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("download.jpg");
Image temp1 = img.getImage();
img = new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
Image img1 = img.getImage();
gridPanel.setLayout(new GridLayout(5, 5));
grid = new JButton[5][5];
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
Image image = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
ImageIcon icon = new ImageIcon(image);
JButton temp = new JButton(icon);
// use own extension of TransferHandler
temp.setTransferHandler(new MyTransferHandler("icon"));
// start drag on mouse pressed event.
temp.addMouseListener(new DragMouseAdapter());
grid[x][y] = temp;
gridPanel.add(grid[x][y]);
}
}
test = new JLabel();
jf.getContentPane().add(BorderLayout.NORTH, test);
jf.add(gridPanel);
jf.pack();
jf.setSize(500, 500);
jf.setVisible(true);
}
class DragMouseAdapter extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JButton c = (JButton) e.getSource();
dragSource = c;
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
private class MyTransferHandler extends TransferHandler {
public MyTransferHandler(String property) {
super(property);
}
#Override
public boolean importData(JComponent comp, Transferable t) {
Icon targetIcon = ((JButton) comp).getIcon();
boolean result = super.importData(comp, t);
if (dragSource != null) {
dragSource.setIcon(targetIcon);
dragSource = null;
}
return result;
}
}
}
I am trying to make a simple game using JPanel.
I am using Graphics Draw to display all information, including text, but I need to add user input.
I was thinking about using JTextField with absolute positioning to make it work with what is being drawn, but I have heard that absolute positioning is not a good way set up a JPanel.
Is there a better way to use both graphics paint and JComponents in the same panel?
Solution: use layout managers
Why not simply have the drawing JPanel held by another JPanel, one that uses BorderLayout and held in the BorderLayout.CENTER position. You can then place JTextFields or other control components in the outer JPanel in other positions.
You can also add a layout manager to the drawing JPanel and then add components onto this using the layout. Just remember that if you add any JPanels on top of the drawing JPanel, the added JPanels should be transparent, that is myPanel.setOpaque(false) should be called on them so that the drawing underneath shows through.
For example -- run this program to see what I mean:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class GradientPaintEg extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
private JSlider hue1Slider = new JSlider(0, 100, 0);
private JSlider hue2Slider = new JSlider(0, 100, 0);
public GradientPaintEg() {
Color color = drawingPanel.getColor1();
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
int value = (int) (hsb[0] * 100);
hue1Slider.setValue(value);
color = drawingPanel.getColor2();
hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
value = (int) (hsb[0] * 100);
hue2Slider.setValue(value);
hue1Slider.setMajorTickSpacing(20);
hue1Slider.setMinorTickSpacing(5);
hue1Slider.setPaintTicks(true);
hue1Slider.setPaintLabels(true);
hue1Slider.setPaintTrack(true);
hue1Slider.addChangeListener(new SliderListener(hue1Slider, drawingPanel, true));
hue1Slider.setOpaque(false);
hue2Slider.setMajorTickSpacing(20);
hue2Slider.setMinorTickSpacing(5);
hue2Slider.setPaintTicks(true);
hue2Slider.setPaintLabels(true);
hue2Slider.setPaintTrack(true);
hue2Slider.addChangeListener(new SliderListener(hue2Slider, drawingPanel, false));
hue2Slider.setOpaque(false);
JPanel sliderPanel = new JPanel(new GridLayout(0, 1, 4, 4));
sliderPanel.add(hue1Slider);
sliderPanel.add(hue2Slider);
sliderPanel.setOpaque(false);
setLayout(new BorderLayout());
// if you want to add the slider panel to the main JPanel:
// add(sliderPanel, BorderLayout.PAGE_START);
add(drawingPanel);
// if you want to add the sliderPanel to the drawing JPanel
drawingPanel.setLayout(new BorderLayout());
drawingPanel.add(sliderPanel, BorderLayout.PAGE_START);
}
private class SliderListener implements ChangeListener {
private JSlider slider;
private DrawingPanel drawingPanel;
private boolean color1Listener;
public SliderListener(JSlider slider, DrawingPanel drawingPanel, boolean color1Listener) {
this.slider = slider;
this.drawingPanel = drawingPanel;
this.color1Listener = color1Listener;
}
#Override
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
float hue = value / 100f;
Color c = Color.getHSBColor(hue, 1f, 1f);
if (color1Listener) {
drawingPanel.setColor1(c);
} else {
drawingPanel.setColor2(c);
}
}
}
private static void createAndShowGui() {
GradientPaintEg mainPanel = new GradientPaintEg();
JFrame frame = new JFrame("GradientPaintEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class DrawingPanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final float X2 = 20;
private static final float Y2 = X2;
private Color color1 = Color.RED;
private Color color2 = Color.BLUE;
public Color getColor1() {
return color1;
}
public void setColor1(Color color1) {
this.color1 = color1;
repaint();
}
public Color getColor2() {
return color2;
}
public void setColor2(Color color2) {
this.color2 = color2;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(new GradientPaint(0, 0, color1, X2, Y2, color2, true));
g2.fillRect(0, 0, getWidth(), getHeight());
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
In this code example, I have a JPanel that draws, called class DrawingPanel and use within another main JPanel, the GradientPaintEg class:
public class GradientPaintEg extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
If I want to add components to the DrawingPanel, I first give it a layout, and then add the component(s). For instance, there is a JPanel that holds JSliders called sliderPanel that I add to the DrawingPanel instance using BorderLayout:
drawingPanel.setLayout(new BorderLayout());
drawingPanel.add(sliderPanel, BorderLayout.PAGE_START);
This adds the sliderPanel to the top of the drawingPanel.
But also note that I have to make sliderPanel non-opaque first:
JPanel sliderPanel = new JPanel(new GridLayout(0, 1, 4, 4));
sliderPanel.add(hue1Slider);
sliderPanel.add(hue2Slider);
sliderPanel.setOpaque(false);
I've also made the JSliders themselves non-opaque so that the underlying drawing shows through:
// ......
hue1Slider.setOpaque(false);
// ......
hue2Slider.setOpaque(false);
Here is a basic mcve of combining user input in JTextfield and painting, on a panel using a layout manager:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
class DrawingPanel extends JPanel {
private final JButton update;
private final JTextField input;
private final static int W = 300, H = 350, RADIUS = 100, GAP = 50;
private String text;
public DrawingPanel() {
setPreferredSize(new Dimension(W, H));
setOpaque(false);
setLayout(new BorderLayout());
update = new JButton("Update");
update.addActionListener(e->update());
add(update, BorderLayout.PAGE_START);
input = new JTextField();
add(input, BorderLayout.PAGE_END);
text = "Enter text and press button";
}
private void update() {
text = input.getText();
input.setText("");
repaint();
}
#Override
public void paintComponent(final Graphics g) {
super.paintComponents(g);
final int width = getWidth();
final int height = getHeight();
g.setColor(Color.RED);
g.fillOval(width/2 - RADIUS, height/2 - RADIUS, RADIUS*2, RADIUS*2);
g.setColor(Color.BLUE);
g.drawString(text, height/2 - RADIUS - GAP, GAP);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(()->makeGui());
}
private static void makeGui() {
JFrame frame = new JFrame();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new DrawingPanel());
frame.pack();
frame.setVisible(true);
}
}
I have a problem with showing specific component placed in JScrollPane. I have horizontal JScrollPane with GridLayout(1,0) and it contains variable number of JPanels - each containing image. It's like a preview of frames in GIF image. I use button to move among these JPanels (by changing borders and keeping index of chosen one), but I don't know how to force JScrollPane to show me JPanel if it's chosen (and center it if possible).
So I want this
force to do this:
Thanks in advance!
EDIT: almost working code with scrollRectToVisible() method
public class MiniatursPanel extends JPanel{
private int indexOfChosenFrame = 0;
private ArrayList<JPanel> frames;
private JScrollPane scrollPane;
private JPanel innerPanel;
public MiniatursPanel(){
setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(),BorderFactory.createLoweredBevelBorder()));
setPreferredSize(new Dimension(1200,170));
setLayout(null);
}
public void initialize(){
int width = GifImageStats.getInstance().getWidth();
int height = GifImageStats.getInstance().getHeight();
int numberOfFrames = GifImageStats.getInstance().getNumberOfFrames();
frames = new ArrayList(numberOfFrames);
for (int i = 0; i < numberOfFrames; i++) {
JPanel frameBox = new JPanel();
frameBox.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton(String.valueOf(i+1));
button.setPreferredSize(new Dimension(2*width,2*height));
button.setBackground(Color.white);
button.setFocusable(false);
frameBox.add(button);
frames.add(frameBox);
}
innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout(1,0,10,10));
for (JPanel button : frames) {
innerPanel.add(button);
}
scrollPane = new JScrollPane(innerPanel);
scrollPane.setBounds(10, 10, 1180, 145);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
highlightFrame(frames.get(0));
add(scrollPane);
}
public void nextFrame(){
if (indexOfChosenFrame == frames.size() - 1) {
unhighlightFrame(frames.get(indexOfChosenFrame));
indexOfChosenFrame = 0;
highlightFrame(frames.get(0));
}else{
unhighlightFrame(frames.get(indexOfChosenFrame));
indexOfChosenFrame++;
highlightFrame(frames.get(indexOfChosenFrame));
}
}
public void previousFrame(){
if (indexOfChosenFrame == 0) {
unhighlightFrame(frames.get(0));
indexOfChosenFrame = frames.size()-1;
highlightFrame(frames.get(indexOfChosenFrame));
}else{
unhighlightFrame(frames.get(indexOfChosenFrame));
indexOfChosenFrame--;
highlightFrame(frames.get(indexOfChosenFrame));
}
}
private void highlightFrame(JPanel frame){
Rectangle rect = frame.getBounds();
rect.setBounds(frame.getX()-550, frame.getY(), frame.getWidth()+1050, frame.getHeight());
innerPanel.scrollRectToVisible(rect);
frame.setBorder(BorderFactory.createLineBorder(Color.red,2));
}
private void unhighlightFrame(JPanel frame){
frame.setBorder(null);
}
The relevant method here is JComponent#scrollRectToVisible(Rectangle). It has to be called on the component that is in the viewport of the scroll pane. (In your case, this is the panel with the grid layout, which contains the other sub-panels).
The rectangle that is passed to this method can be the bounds of one sub-panel. In this case, the scoll pane will do the "minimum" scrolling that is necessary to make the given rectangle visible. If you want to make sure that the respective sub-panel is in the center, then you can increase the size of this rectangle - that is, you define a rectangle in a way that the desired sub-panel will be in the center.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ScrollToVisible
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int n = 20;
final JPanel panel = new JPanel(new GridLayout(1,0));
final List<JComponent> components = new ArrayList<JComponent>();
for (int i=0; i<n; i++)
{
JComponent component = new JLabel(String.valueOf(i), SwingConstants.CENTER);
component.setPreferredSize(new Dimension(100,100));
component.setBorder(BorderFactory.createLineBorder(Color.BLACK));
components.add(component);
panel.add(component);
}
final JScrollPane scrollPane = new JScrollPane(panel);
final JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, n-1, 1));
spinner.addChangeListener(new ChangeListener()
{
JComponent selectedComponent = components.get(0);
#Override
public void stateChanged(ChangeEvent e)
{
selectedComponent.setBorder(BorderFactory.createLineBorder(Color.BLACK));
int index = (Integer)spinner.getValue();
JComponent component = components.get(index);
Rectangle bounds = component.getBounds();
// This would make the component "just" visible:
//panel.scrollRectToVisible(bounds);
// This will center the component:
int cx = bounds.x + bounds.width / 2;
int w = scrollPane.getViewport().getWidth();
Rectangle r = new Rectangle(cx-w/2, bounds.y, w, bounds.height);
panel.scrollRectToVisible(r);
selectedComponent = component;
selectedComponent.setBorder(BorderFactory.createLineBorder(Color.RED));
}
});
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.getContentPane().add(spinner, BorderLayout.NORTH);
f.setSize(800, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
EDIT: You should NOT use setLayout(null), and you should not do manual calls to setBounds, and you should rarely use setPreferredSize. And... when you post code that already is so close to a https://stackoverflow.com/help/mcve (or even was created from a runnable example of another post) then you should make it really runnable. It's annoying to re-insert the boilerplate code and waste some time with debugging until you realize that initialize() is not called at all...
However, change the code according to this:
private void highlightFrame(JPanel frame){
Rectangle rect = frame.getBounds();
int c = rect.x + rect.width / 2;
int w = scrollPane.getViewport().getWidth();
int x = c-w/2;
rect.setBounds(x, rect.y, w, rect.height);
innerPanel.scrollRectToVisible(rect);
frame.setBorder(BorderFactory.createLineBorder(Color.red,2));
}
private void unhighlightFrame(JPanel frame){
frame.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
The most important thing is to make sure that the size of the components is correct, by setting an empty border with the same size as the "highlighting" border.
I am new for Java. I have searched how to set the size of the JTextField and JButton, but I still cannot make it work. Hope someone tell me how to solve it. I declare the height and width, but the component seems doesn't take it. The JTextField and JButton with red & yellow background should be same height. Also the JTextField width is very long.
There is my code:
package MyPackage;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.ComponentOrientation;
import javax.swing.JTextField;
public class MainForm extends JFrame implements ActionListener {
private PDFNotesBean PDFNotesBean = null;
private JMenuItem cascade = new JMenuItem("Cascade");
private int numInterFrame=0;
private JDesktopPane desktop=null;
private ArrayList<File> fileList=new ArrayList<File>();
private static int categoryButtonWidth= 40;
private static int categoryTextFieldWidth=60;
private static int categoryHight=40;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
new MainForm();
}
});
}
public MainForm(){
super("Example");
//it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Name the JMenu & Add Items
JMenu menu = new JMenu("File");
menu.add(makeMenuItem("Open"));
menu.add(makeMenuItem("Save"));
menu.add(makeMenuItem("Quit"));
// Add JMenu bar
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
//menuBar.add(menuWindow);
setJMenuBar(menuBar);
this.setMinimumSize(new Dimension(400, 500));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
desktop = new JDesktopPane();
this.setLayout(new BorderLayout());
setCategoryPanel();
this.add(desktop, BorderLayout.CENTER);
setVisible(true);
}
private void setCategoryPanel(){
//set the color label category
JPanel panelCategory=new JPanel();
panelCategory.setLayout(new BoxLayout(panelCategory, BoxLayout.LINE_AXIS));
JButton btnCategory_1=new JButton("");
btnCategory_1.setPreferredSize(new Dimension ( categoryButtonWidth, categoryHight));
btnCategory_1.setBackground(Color.red);
btnCategory_1.addActionListener(this);
panelCategory.add(btnCategory_1);
JTextField txtCategory_1 = new JTextField();
txtCategory_1.setPreferredSize(new Dimension (categoryTextFieldWidth, categoryHight));
panelCategory.add(txtCategory_1);
JButton btnCategory_2=new JButton("");
btnCategory_2.setPreferredSize(new Dimension ( categoryButtonWidth, categoryHight));
btnCategory_2.setBackground(Color.YELLOW);
btnCategory_2.addActionListener(this);
panelCategory.add(btnCategory_2);
JTextField txtCategory_2 = new JTextField( );
txtCategory_1.setPreferredSize(new Dimension (categoryTextFieldWidth, categoryHight));
panelCategory.add(txtCategory_2);
this.add(panelCategory, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
// Menu item actions
String command = e.getActionCommand();
if (command.equals("Quit")) {
System.exit(0);
} else if (command.equals("Open")) {
// Open menu item action
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(MainForm.this);
if (returnVal == fileChooser.APPROVE_OPTION) {
numInterFrame=numInterFrame+1;
File file = fileChooser.getSelectedFile();
fileList.add(file);
AddNote(file);
// Load file
} else if (returnVal == JFileChooser.CANCEL_OPTION ) {
// Do something else
}
}
else if (command.equals("Save")) {
// Save menu item action
System.out.println("Save menu item clicked");
}
}
private JMenuItem makeMenuItem(String name) {
JMenuItem m = new JMenuItem(name);
m.addActionListener(this);
return m;
}
private void AddNote(File file){
JInternalFrame internalFrame = new JInternalFrame("PDFAnnotation"
+ file.getName(), true, true, true, true);
internalFrame.setBounds(0, 0, 600, 100);
desktop.add(internalFrame);
PDFPanel p=new PDFPanel();
JPanel e =p.getJPanel(file);
internalFrame.add(e, BorderLayout.CENTER);
internalFrame.setVisible(true);
this.add(desktop, BorderLayout.CENTER);
//resize the internal frame as full screen
Dimension size = desktop.getSize();
int w = size.width ;
int h = size.height ;
int x=0;
int y=0;
desktop.getDesktopManager().resizeFrame(internalFrame, x, y, w, h);
}
}
Don't use the setPreferredSize() method.
All Swing components will have a preferred size.
For a JTextField you can use:
JTextField textField = new JTextField(5);
to indication how many characters you want to display at one time.
For the JButton, the width is determined by the text you add to the button.
When you use a BoxLayout, the width of the components is increased to fill the available space.
Just use a FlowLayout (which is the default for a JPanel) and the components will be displayed at their preferred sizes.
I am developing a program where if the menu item "show grid line" is clicked, the drawing area below the menu bar will draw grid lines on the grid.
EDIT: Below are the components to the program. I turned any parts that are not important for this issue into comments.
import java.awt.Color;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
//main initializer
public class lineFollower {
frameDesign frame;
//private static Image icon = new ImageIcon("images/icon copy.jpg").getImage();
public static void main(String[] args) {
//make new frame
JFrame frame = new frameDesign();
frame.setSize(1000,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocale(null);
frame.setTitle("Line Follower Program 1.0");
//frame.setIconImage(icon);
}
}
This is the frame design (omitting parts as well):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.*;
public class frameDesign extends JFrame {
//data fields
//JPanel DataPanel;
//ListPanel ListPanel;
DrawingPanel DrawingPanel;
grid gridbox;
menuPanel menu;
//ArrayList<Rectangle> Rectangles;
//ArrayList<Circle> Circles;
//ArrayList<Triangle> Triangles;
public int newvalue = 0;
public int[] griddim;
public int gridwidth, gridheight;
BorderLayout layout;
//combine data into frame
public frameDesign(){
//Rectangles = new ArrayList<Rectangle>();
//Circles = new ArrayList<Circle>();
//Triangles = new ArrayList<Triangle>();
//Layout: Border
BorderLayout layout = new BorderLayout();
setLayout(layout);
//Assemble the Menu
menu = new menuPanel();
//Assemble the List Panel which also includes the animation buttons
//ListPanel = new ListPanel();
//Create the gridbox
gridbox = new grid();
gridbox.setPreferredSize(new Dimension(200,200));
gridbox.setSize(500,200);
griddim = gridbox.getgriddim();
gridwidth = griddim[0];
gridheight = griddim[1];
//Data Panel consists of ListPanel, AnimatePanel, and DrawingPanel
//DataPanel = new JPanel(new FlowLayout());
//DataPanel.add(DrawingPanel= new DrawingPanel());
//DataPanel.add(ListPanel = new ListPanel());
//DataPanel.setSize(250,700);
//DataPanel.setBackground(Color.getHSBColor(150f, 100f, 100f));
//DataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
add(gridbox, BorderLayout.CENTER);
add(menu, BorderLayout.NORTH);
//add(DataPanel, BorderLayout.EAST);
//getPoints pointfinder = new getPoints();
//pointgrabber pointfinder2 = new pointgrabber();
//gridbox.addMouseListener(pointfinder2);
//gridbox.addMouseMotionListener(pointfinder);
//ActionListeners for Menus
menu.ShowLine.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//show the grid
if (gridbox.isgridline()==true){
gridbox.gridline(false);
}
else if (gridbox.isgridline()==false){
gridbox.gridline(true);
}
}
});
}
The next pile of code is for the grid creation:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
* This is the design for the grid
*/
public class grid extends JPanel {
//data fields
boolean isgridline;
private static int startX = 50;
private static int startY = 50;
private int intervalline;
private int[] griddim;
//gridPanel
grid(){
setBorder(BorderFactory.createLineBorder(Color.BLACK));
griddim = new int[2];
griddim[0] = 500;
griddim[1] = 500;
isgridline = false;
intervalline = 20;
}
//Paints grid
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//GridBox
g.setColor(Color.BLACK);
g.drawRect(50, 50, griddim[0], griddim[1]);
g.setColor(Color.WHITE);
g.fillRect(50, 50, griddim[0], griddim[1]);
//draw grid lines
if (isgridline){
g.setColor(Color.getHSBColor(150, 150, 200));
//vertical lines
for (int v = 0; v <griddim[0]; v = v+intervalline){
g.drawLine(startX, 50,
startX, 50+griddim[1]);
startX = startX+intervalline;
}
//horizontal lines
for (int v=0; v<griddim[1]; v=v+intervalline){
g.drawLine(50, startY, 50+griddim[0], startY);
startY = startY+intervalline;
}
}
}
//set grid dimensions
public void setgridbox(int[] dim){
griddim=dim;
}
//get grid dimensions
public int[] getgriddim(){
return griddim;
}
//set the grid line true or false
public void gridline(boolean islines){
isgridline=islines;
repaint();
}
//get gridline true or false
public boolean isgridline(){
return isgridline;
}
//set grid line intervals
//public void setinterval(int interval){
//intervalline=interval;
//repaint();
//}
}
Lastly, the menu:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.MenuEvent;
/*
* Class just for the menu panel
*/
public class menuPanel extends JMenuBar {
JMenuItem New, Save, Open, Exit;
JMenu fileMenu, gridMenu;
JMenuItem GridDim, GridLine, ShowLine;
menuPanel(){
//Assemble file menu
fileMenu = new JMenu("File");
New = new JMenuItem("New");
Save = new JMenuItem("Save");
Open = new JMenuItem("Open");
Exit = new JMenuItem("Exit");
fileMenu.add(New);
fileMenu.add(Save);
fileMenu.add(Open);
fileMenu.addSeparator();
fileMenu.add(Exit);
add(fileMenu);
//Assemble grid menu
gridMenu = new JMenu("Grid");
GridDim = new JMenuItem("Dimension");
GridLine = new JMenuItem("Change Grid Line Interval");
ShowLine = new JMenuItem("Show Grid Lines");
gridMenu.add(GridDim);
gridMenu.add(GridLine);
gridMenu.add(ShowLine);
add(gridMenu);
}
}
When the "show grid lines" button in the menu bar under grid is clicked, it turns on the grid lines and turns off the grid lines once each. It stops working after that... What can I do such that the grid lines show up on and off again more than just once?
I did a really quick test, but because you've not provided us with runnable example, it's not possible to reproduce your problem.
What I "suspect" is your for-loop is crashing for some reason (the griddim is null or there's an out bounds exception) which is causing the EDT to become unstable.
Until you enlighten use with a repeatable, runnable example, we're never going to be sure
public class TestGridLine {
public static void main(String[] args) {
new TestGridLine();
}
public TestGridLine() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new GridPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GridPane extends JPanel {
private boolean gridline;
public GridPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Grid");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setGridLine(!isGridLine());
}
});
add(btn);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth() - 1;
int height = getHeight() - 1;
//GridBox
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
g.setColor(Color.WHITE);
g.fillRect(1, 1, width - 1, height - 1);
//draw grid lines
if (gridline) {
g.setColor(Color.getHSBColor(150, 150, 200));
//vertical lines
for (int v = 0; v < width - 2; v += 10) {
g.drawLine(v, 1, v, height - 2);
}
//horizontal lines
for (int v = 0; v < height - 2; v += 10) {
g.drawLine(1, v, width - 2, v);
}
}
}
//returns boolean gridline
public boolean isGridLine() {
return gridline;
}
//set the grid line true or false
public void setGridLine(boolean islines) {
if (islines != gridline) {
gridline = islines;
repaint();
}
}
}
}