Search Results

Search found 428 results on 18 pages for 'jpanel'.

Page 10/18 | < Previous Page | 6 7 8 9 10 11 12 13 14 15 16 17  | Next Page >

  • chess board in java

    - by ranzy
    This is my code below import javax.swing.*; import java.awt.*; public class board2 { JFrame frame; JPanel squares[][] = new JPanel[8][8]; public board2() { frame = new JFrame("Simplified Chess"); frame.setSize(500, 500); frame.setLayout(new GridLayout(8, 8)); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { squares[i][j] = new JPanel(); if ((i + j) % 2 == 0) { squares[i][j].setBackground(Color.black); } else { squares[i][j].setBackground(Color.white); } frame.add(squares[i][j]); } } squares[0][0].add(new JLabel(new ImageIcon("rookgreen.png"))); squares[0][2].add(new JLabel(new ImageIcon("bishopgreen.png"))); squares[0][4].add(new JLabel(new ImageIcon("kinggreen.png"))); squares[0][5].add(new JLabel(new ImageIcon("bishopgreen.png"))); squares[0][7].add(new JLabel(new ImageIcon("rookgreen.png"))); squares[7][0].add(new JLabel(new ImageIcon("rookred.png"))); squares[7][2].add(new JLabel(new ImageIcon("bishopred.png"))); squares[7][4].add(new JLabel(new ImageIcon("kingred.png"))); squares[7][5].add(new JLabel(new ImageIcon("bishopred.png"))); squares[7][7].add(new JLabel(new ImageIcon("rookred.png"))); for (int i = 0; i < 8; i++) { squares[1][i].add(new JLabel(new ImageIcon("pawngreen.png"))); squares[6][i].add(new JLabel(new ImageIcon("pawnred.png"))); } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new board2(); } } I am trying to create a chess game sort of and I need help with putting labels on all sides of the board to label the rows and columns in either A-H or 1-8. I have no idea how to do it. Also later on I'll be adding a feature to drag and drop the pieces. Is it best to use JLabels? Anyways I would I go about putting the labels on the side? Thanks!

    Read the article

  • Jalopy comments indentation

    - by Neil Wightman
    Hi All. We use jalopy to format our code to the sun standard (well 99%). I have 1 issue with it. It keeps moving comments to the right of the line. E.g // Panel "JPanel.border", new LazyValue(packageName + "PanelBorder"), //frozen // Button "Button.background", new ColorUIResource(251, 251, 251), //frozen "Button.foreground", new ColorUIResource(0, 0, 0), //frozen But jalopy always moves the // Button to the far right. // Panel "JPanel.border", new LazyValue(packageName + "PanelBorder"), //frozen // Button "Button.background", new ColorUIResource(251, 251, 251), //frozen "Button.foreground", new ColorUIResource(0, 0, 0), //frozen Is there any way to stop this? Thanks Neil

    Read the article

  • How can I create a JTable where the first column is always in the JScrollPane viewport?

    - by voodoogiant
    What's the best way to set up a table in a JScrollPane such that the first column is always on the screen in the same position regardless of horizontal scrolling and overlaps columns that pass underneath? When the scrollbar is at the farthest left, the columns look normal, but as the user scrolls to the right, the secondary columns (2 and on) move underneath the first until the last column appears on the far right of the viewport? I found a sample taken from Eckstein's "Java Swing" book that sort of does this, but it doesn't allow resizing of the first column. I was thinking of some scheme where one JPanel held a horizontal struct and a table holding the secondary columns and another JPanel which floated over them (fixed regardless of scrolling). The struct would be to keep the viewport range constant as the first column floated around. Ideally I could do it with two tables using the same model, but I'm not sure if the whole idea is a naive solution.

    Read the article

  • Multiple Graphics2D Objects

    - by Trizicus
    I have a Graphics object of JPanel and that is working fine: import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import javax.swing.JPanel; public class GraphicsTest extends JPanel { private Graphics2D g2d; private String state; private int x, y; @Override public void paintComponent(Graphics g) { super.paintComponent(g); g2d = (Graphics2D) g; g2d.setClip(0, 0, getWidth(), getHeight()); g2d.setColor(Color.BLACK); g2d.drawString("STATE: " + state, 5, 15); g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30); g2d.setColor(Color.red); Rectangle2D r2d = new Rectangle2D.Double(x,y,10,10); g2d.draw(r2d); Test t = new Test(); super.add(t); repaint(); } public void setState(String state) { this.state = state; } public String getState() { return state; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } } I was experimenting with a new Graphics component and when I instantiate a new Test and add it in GraphicsTest nothing happens. What is it that I am doing wrong? import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import javax.swing.JComponent; public class Test extends JComponent { private Graphics2D g2d; private String state; private int x, y; @Override public void paintComponent(Graphics g) { super.paintComponent(g); g2d = (Graphics2D) g.create(); g2d.setColor(Color.GREEN); g2d.fill(new Rectangle2D.Double(60, 60, 10, 10)); repaint(); } public void setState(String state) { this.state = state; } public String getState() { return state; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } } Thanks!

    Read the article

  • Icon in titledBorder title

    - by Venno
    Hi is it possible to place an icon in the title of a titledBorder for example the following code: import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class TitledExample extends JPanel { public TitledExample() { super(true); this.setLayout(new GridLayout(1, 1, 5, 5)); JLabel label = new JLabel("Titled Border"); label.setHorizontalAlignment(JLabel.CENTER); TitledBorder titled = new TitledBorder("Image here ?? Title"); label.setBorder(titled); add(label); } Thanks , Cheers

    Read the article

  • Swing Panel Question

    - by dalton conley
    Ok, so I've been pulling my hair out over this all day. I cannot get my panels to resize. I add a panel to anything.. a frame, a content pane, and It wont .. "force" setBounds for some strange reason. Container pane = window.getContentPane(); JPanel calendarPanel = new JPanel(); pane.add(calendarPanel); calendarPanel.setBounds(0,0, 320, 335); note that window is of JFrame. Any suggestions to sizing a panel would be appreciated!

    Read the article

  • [java bean]hibernate Session breaks a java bean?

    - by blow
    Hi all, i have a simple JPanel bean in my projects, now i want to drag my panel bean class into my jframe. My panel bean class is like this: public class BeanPanel extends javax.swing.JPanel { /** Creates new form BeanPanel */ public BeanPanel () { initComponents(); Session session=HibernateUtil.getSessionFactory().openSession(); } This code seem to break the bean: Session session=HibernateUtil.getSessionFactory().openSession(); When i try to drag the class into my JFrame bean i had this error message: This component cannot be instantiated. Please make sure it is a JavaBeans Component If i comment it all works fine. What is the reason of this? Thanks.

    Read the article

  • How to change the cursor type

    - by Jessy
    This question is related to the previous post. http://stackoverflow.com/questions/2532936/how-to-save-file-and-read How can I change the cursor to "Hand" only when the mouse pointed on grid which is not Null (contained images)? So far the cursor turn to "Hand" all over the grids (null or not null). public GUI() { .... JPanel pDraw = new JPanel(); .... for(Component component: pDraw.getComponents()){ JLabel lbl = (JLabel)component; //add mouse listener to grid box which contained image if (lbl.getIcon() != null) lbl.addMouseListener(this); } public void mouseEntered(MouseEvent e) { Cursor cursor = Cursor.getDefaultCursor(); //change cursor appearance to HAND_CURSOR when the mouse pointed on images cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); setCursor(cursor); }

    Read the article

  • Why cant i draw an elipse in with code?

    - by bvivek88
    package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class test_bmp extends JPanel implements MouseListener,MouseMotionListener,ActionListener { static BufferedImage image; Color color; Point start=new Point(); Point end =new Point(); JButton elipse=new JButton("Elipse"); JButton rectangle=new JButton("Rectangle"); JButton line=new JButton("Line"); String selected; public test_bmp() { color = Color.black; setBorder(BorderFactory.createLineBorder(Color.black)); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { //super.paintComponent(g); g.drawImage(image, 0, 0, this); Graphics2D g2 = (Graphics2D)g; g2.setPaint(Color.black); if(selected=="elipse") { g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y)); System.out.println("Start : "+start.x+","+start.y); System.out.println("End : "+end.x+","+end.y); } if(selected=="line") g2.drawLine(start.x,start.y,end.x,end.y); } //Draw on Buffered image public void draw() { Graphics2D g2 = image.createGraphics(); g2.setPaint(color); System.out.println("draw"); if(selected=="line") g2.drawLine(start.x, start.y, end.x, end.y); if(selected=="elipse") { g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y)); System.out.println("Start : "+start.x+","+start.y); System.out.println("End : "+end.x+","+end.y); } repaint(); g2.dispose(); } public JPanel addButtons() { JPanel buttonpanel=new JPanel(); buttonpanel.setBackground(color.lightGray); buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS)); elipse.addActionListener(this); rectangle.addActionListener(this); line.addActionListener(this); buttonpanel.add(elipse); buttonpanel.add(Box.createRigidArea(new Dimension(15,15))); buttonpanel.add(rectangle); buttonpanel.add(Box.createRigidArea(new Dimension(15,15))); buttonpanel.add(line); return buttonpanel; } public static void main(String args[]) { test_bmp application=new test_bmp(); //Main window JFrame frame=new JFrame("Whiteboard"); frame.setLayout(new BorderLayout()); frame.add(application.addButtons(),BorderLayout.WEST); frame.add(application); //size of the window frame.setSize(600,400); frame.setLocation(0,0); frame.setVisible(true); int w = frame.getWidth(); int h = frame.getHeight(); image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setPaint(Color.white); g2.fillRect(0,0,w,h); g2.dispose(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent event) { start = event.getPoint(); } @Override public void mouseReleased(MouseEvent event) { end = event.getPoint(); draw(); } @Override public void mouseDragged(MouseEvent e) { end=e.getPoint(); repaint(); } @Override public void mouseMoved(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==elipse) selected="elipse"; if(e.getSource()==line) selected="line"; draw(); } } I need to create a paint application, when i draw elipse by dragging mouse from left to right it displays nothing, why?? should i use any other function here?

    Read the article

  • How to add two label in one gridbox?

    - by Jessy
    Hello everyone, How can I add two label in the same grid box? e.g. in row 1, col 1 the will be 2 labels? The code below will add the label in two different grid. JPanel chckBox = new JPanel(new GridLayout(1,8,3,3)); JLabel label1 = new JLabel("A"); JLabel label2 = new JLabel("B"); ... chckBox.add(label1); chckBox.add(label2); ...

    Read the article

  • How can I put a horizontal line between vertically ordered elements?

    - by Roman
    I have a set of vertically ordered elements. They are displayed with the following code: JPanel myPanel = new JPanel(); myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS)); JButton button = new JButton("My Button"); JLabel label = new JLabel("My label!!!!!!!!!!!"); myPanel.add(button); myPanel.add(label); I would like to put a horizontal line between my elements (something like <hr> in html). Does anybody know how it can be done?

    Read the article

  • How to click through JGlassPane With MouseListener to UI behind it

    - by Epicmaster
    I have a JFrame and a bunch of JComponents on top of the JFrame. I need to make use of the JGlassPane and I used this implementation to set it up. JPanel glass = new JPanel(); frame.setGlassPane(glass); glass.setVisible(true); glass.setOpaque(false); After doing so I can't select any JButtons or other JComponents under the JGlassPane. Is there a way to have only the components on the GlassPane selectable while still having the ability to select components under the GlassPane? Edit I forgot to mention (not knowing this would be relevant) that I did attach both a MouseListener and a MouseMotionListener to the glass pane. Is there a way to pass the Mouse Events to other components and only use them when needed?

    Read the article

  • Cannot find Symbol = new

    - by Nick G.
    Java is complaining! cannot find symbol symbol : constructor Bar() location: class Bar JPanel panel = new Bar(); ^ QUESTION: Why am I getting this error?...everything seems to be correct. this is the coding: public class JFrameWithPanel { public static void main(String[] args) { JPanel panel = new Bar(); } } Bar( ) is public class Bar extends JPanel { public Bar(final JFrame frame) { super(new BorderLayout()); String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" }; JComboBox comboBox = new JComboBox(tests); TextArea text = new TextArea(5, 10); add(new JLabel("Welcome to the CIT Test Program ")); add(new JLabel("Please select which Test Package from the list below.")); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); JMenu helpMenu = new JMenu("Help"); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(helpMenu); JMenuItem newMenu = new JMenuItem("New (Ctrl+N)"); JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)"); JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)"); JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)"); JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)"); JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)"); JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)"); JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)"); fileMenu.add(newMenu); fileMenu.add(openMenu); fileMenu.add(saveMenu); fileMenu.add(exitMenu); editMenu.add(cutMenu); editMenu.add(copyMenu); editMenu.add(pasteMenu); helpMenu.add(infoMenu); this.add(comboBox, BorderLayout.NORTH); this.add(text, BorderLayout.SOUTH); frame.setJMenuBar(menuBar); add(new JButton("Select") { { addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.dispose(); JOptionPane.showMessageDialog(frame, "IT WORKS!"); } }); } }); } }

    Read the article

  • Moving and resizing JPanels object inside JFrame.

    - by Gabriel A. Zorrilla
    Continuing my quest of learning Java by doing a simple game, i stumbled upon a little issue. My gameboard extends JPanel as well as each piece of the board. Now, this presents some problems: Cant set size of each piece, therefore, each piece JPanel ocupy the whole JFrame, concealing the rest of the pieces and the background (gameboard). Cant set the position of the pieces. I have the default flow manager. Tried setbounds and no luck. Perhaps i should make the piece to extend other JComponent?

    Read the article

  • Java Swing: JWindow appears behind all other process windows, and will not disappear

    - by Kim Jong Woo
    I am using JWindow to display my splash screen during the application start up. however it will not appear in front of all windows as it should, and it will not disappear as well. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JWindow; public class MySplash { public static MySplash INSTANCE; private static JWindow jw; public MySplash(){ createSplash(); } private void createSplash() { jw = new JWindow(); JPanel content = (JPanel) jw.getContentPane(); content.setBackground(Color.white); // Set the window's bounds, centering the window int width = 328; int height = 131; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - width) / 2; int y = (screen.height - height) / 2; jw.setBounds(x, y, width, height); // Build the splash screen JLabel label = new JLabel(new ImageIcon("splash.jpg")); JLabel copyrt = new JLabel("SplashScreen Test", JLabel.CENTER); copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12)); content.add(label, BorderLayout.CENTER); content.add(copyrt, BorderLayout.SOUTH); Color oraRed = new Color(156, 20, 20, 255); content.setBorder(BorderFactory.createLineBorder(oraRed, 0)); } public synchronized static MySplash getInstance(){ if(INSTANCE==null){ INSTANCE = new MySplash(); } return INSTANCE; } public void showSplash(){ jw.setAlwaysOnTop(true); jw.toFront(); jw.setVisible(true); return; } public void hideSplash(){ jw.setAlwaysOnTop(false); jw.toBack(); jw.setVisible(false); return; } } So in my main class which extends JFrame, I call my splash screen by SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { MySplash.getInstance().showSplash(); } }); However, the JWindow appears behind the all open instances of windows on my computer. Hiding the JWindow also doesn't work. SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { MySplash.getInstance().hideSplash(); } });

    Read the article

  • Problem creating calculations 'engine' in two class java calculator

    - by tokee
    i have hit a brick wall whilst attempting to create a two class java calculator but have been unsuccessful so far in getting it working. i have the code for an interface which works and displays ok but creating a seperate class 'CalcEngine' to do the actual calculations has proven to be beyond me. I'd appreciate it if someone could kick start things for me and create a class calcEngine which works with the interface class and allows input when from single button i.e. if one is pressed on the calc then 1 displays onscreen. please note i'm not asking someone to do the whole thing for me as i want to learn and i'm confident i can do the rest including addition subtraction etc. once i get over the obstacle of getting the two classes to communicate. any and all assistance would be very much appreciated. Please see the calcInterface class code below - import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; /** *A Class that operates as the framework for a calculator. *No calculations are performed in this section */ public class CalcFrame implements ActionListener { private CalcEngine calc; private JFrame frame; private JTextField display; private JLabel status; /** * Constructor for objects of class GridLayoutExample */ public CalcFrame() { makeFrame(); //calc = engine; } /** * This allows you to quit the calculator. */ // Alows the class to quit. private void quit() { System.exit(0); } // Calls the dialog frame with the information about the project. private void showAbout() { JOptionPane.showMessageDialog(frame, "Group Project", "About Calculator Group Project", JOptionPane.INFORMATION_MESSAGE); } private void makeFrame() { frame = new JFrame("Group Project Calculator"); makeMenuBar(frame); JPanel contentPane = (JPanel)frame.getContentPane(); contentPane.setLayout(new BorderLayout(8, 8)); contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10)); /** * Insert a text field */ display = new JTextField(); contentPane.add(display, BorderLayout.NORTH); //Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(4, 4)); JPanel buttonPanel = new JPanel(new GridLayout(4, 4)); contentPane.add(new JButton("1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("3")); contentPane.add(new JButton("4")); contentPane.add(new JButton("5")); contentPane.add(new JButton("6")); contentPane.add(new JButton("7")); contentPane.add(new JButton("8")); contentPane.add(new JButton("9")); contentPane.add(new JButton("0")); contentPane.add(new JButton("+")); contentPane.add(new JButton("-")); contentPane.add(new JButton("/")); contentPane.add(new JButton("*")); contentPane.add(new JButton("=")); contentPane.add(new JButton("C")); contentPane.add(buttonPanel, BorderLayout.CENTER); //status = new JLabel(calc.getAuthor()); //contentPane.add(status, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } /** * Create the main frame's menu bar. * The frame that the menu bar should be added to. */ private void makeMenuBar(JFrame frame) { final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); JMenu menu; JMenuItem item; // create the File menu menu = new JMenu("File"); menubar.add(menu); // create the Quit menu with a shortcut "Q" key. item = new JMenuItem("Quit"); item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK)); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { quit(); } }); menu.add(item); // Adds an about menu. menu = new JMenu("About"); menubar.add(menu); // Displays item = new JMenuItem("Calculator Project"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showAbout(); } }); menu.add(item); } /** * An interface action has been performed. * Find out what it was and handle it. * @param event The event that has occured. */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if(command.equals("0") || command.equals("1") || command.equals("2") || command.equals("3") || command.equals("4") || command.equals("5") || command.equals("6") || command.equals("7") || command.equals("8") || command.equals("9")) { int number = Integer.parseInt(command); calc.numberPressed(number); } else if(command.equals("+")) { calc.plus(); } else if(command.equals("-")) { calc.minus(); } else if(command.equals("=")) { calc.equals(); } else if(command.equals("C")) { calc.clear(); } else if(command.equals("?")) { } // else unknown command. redisplay(); } /** * Update the interface display to show the current value of the * calculator. */ private void redisplay() { display.setText("" + calc.getDisplayValue()); } /** * Toggle the info display in the calculator's status area between the * author and version information. */ }

    Read the article

  • Detect if Java Swing component has been hidden

    - by kayahr
    Assume we have the following Swing application: final JFrame frame = new JFrame(); final JPanel outer = new JPanel(); frame.add(outer); JComponent inner = new SomeSpecialComponent(); outer.add(inner); So in this example we simply have an outer panel in the frame and a special component in the panel. This special component must do something when it is hidden and shown. But the problem is that setVisible() is called on the outer panel and not on the special component. So I can't override the setVisible method in the special component and I also can't use a component listener on it. I could register the listener on the parent component but what if the outer panel is also in another panel and this outer outer panel is hidden? Is there an easier solution than recursively adding componentlisteners to all parent components to detect a visibility change in SomeSpecialComponent?

    Read the article

  • How do I make a rectangle move in an image?

    - by alicedimarco
    Basically I have an image loaded, and when I click a portion of the image, a rectangle (with no fill) shows up. If I click another part of the image again, that rectangle will show up once more. With each click, the same rectangle should appear. So far I have this code, now I don't know how to make the image appear. My image from my file directory. I have already made the code to get the image from my file directory. import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class MP2 extends JPanel implements MouseListener{ JFrame frame; JPanel panel; int x = 0; int y = 0; String input; public MP2(){ } public static void main(String[] args){ JFrame frame = new JFrame(); MP2 panel = new MP2(); panel.addMouseListener(panel); frame.add(panel); frame.setSize(200,200); frame.setVisible(true); } public void mouseClicked(MouseEvent event) { // TODO Auto-generated method stub this.x = event.getX(); this.y = event.getY(); this.repaint(); input = JOptionPane.showInputDialog("Something pops out"); System.out.println(input); } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } public void paintComponent(Graphics g){ super.paintComponent(g); // this.setBackground(Color.white); *Sets the bg color of the panel g.setColor(new Color(255,0,0)); g.drawRect(x, y, 100, 100); } }

    Read the article

  • Swing: does DefaultBoundedRangeModel coalesce multiple events?

    - by Jason S
    I have a JProgressBar displaying a BoundedRangeModel which is extremely fine grained and I was concerned that updating it too often would slow down my computer. So I wrote a quick test program (see below) which has a 10Hz timer but each timer tick makes 10,000 calls to microtick() which in turn increments the BoundedRangeModel. Yet it seems to play nicely with a JProgressBar; my CPU is not working hard to run the program. How does JProgressBar or DefaultBoundedRangeModel do this? They seem to be smart about how much work it does to update the JProgressBar, so that as a user I don't have to worry about updating the BoundedRangeModel's value. package com.example.test.gui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoundedRangeModel; import javax.swing.DefaultBoundedRangeModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.Timer; public class BoundedRangeModelTest1 extends JFrame { final private BoundedRangeModel brm = new DefaultBoundedRangeModel(); final private Timer timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { tick(); } }); public BoundedRangeModelTest1(String title) { super(title); JPanel p = new JPanel(); p.add(new JProgressBar(this.brm)); getContentPane().add(p); this.brm.setMaximum(1000000); this.brm.setMinimum(0); this.brm.setValue(0); } protected void tick() { for (int i = 0; i < 10000; ++i) { microtick(); } } private void microtick() { this.brm.setValue(this.brm.getValue()+1); } public void start() { this.timer.start(); } static public void main(String[] args) { BoundedRangeModelTest1 f = new BoundedRangeModelTest1("BoundedRangeModelTest1"); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(EXIT_ON_CLOSE); f.start(); } }

    Read the article

  • Adding fields to a proxied class in Clojure

    - by mikera
    I'm using "proxy" to extend various Swing classes in a Clojure GUI application, generally with code that looks something like: (def ^JPanel mypanel (proxy [JPanel] [] (paintComponent [#^Graphics g] (.drawImage g background-image 0 0 nil)))) This works well but I can't figure out how to add additional fields to the newly extended class, for example making the background-image a field that could be subsequently updated. This would be pretty easy and common practice in Java. Is there a good way to do this in Clojure? Or is there another preferred method to achieve the same effect?

    Read the article

  • Need Java Swing equivalent of "scrollIntoView" from Browser DOM

    - by bgould
    I have a JPanel with several levels of child components, also with a JScrollPane. I'm placing a focus listener on some of the child components to add some behavior to those components, but I would also like to have that component scroll into the JPanel's viewport when focus is gained. My question is, does anyone have a general purpose function to do this, similar to the browser DOM function "scrollIntoView"? I've tried muddling through this with various inputs to JComponent.scrollRectToVisible but I guess I haven't figured out the magic word. Thanks in advance.

    Read the article

  • Java JFrame is very strange

    - by rodion
    When I am running this, JLabel is not visible, but when I resize window (with mouse) JLabel is showed. Why? import javax.swing.*; import java.awt.*; public class FrmTaoLogin extends JFrame { private JPanel pnlLeft = new JPanel(); public FrmTaoLogin() { super(); pnlLeft.setBorder(BorderFactory.createEtchedBorder()); pnlLeft.add(new JLabel("test1")); getContentPane().add(pnlLeft,BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(200, 200); } public static void main(String[] args) { FrmTaoLogin FrmLogin = new FrmTaoLogin(); FrmLogin.setVisible(true); } }

    Read the article

  • Using MigLayout, why is a JButton following a JTable unresponsive and how to fix this?

    - by M. Joanis
    Hi everyone, I am having a mind-boggling problem regarding the use of a JButton following a JTable with MigLayout. It is totally unresponsive unless I push it far enough past the JTable (then it can behave correctly). I have tried running the code with both the MigLayout JAR of the version we use for end user products and with the very most recent one; same result. Here is a sample code reproducing the problem (Main.java): import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") public class Main extends JFrame { private JPanel panel; private JTextField textField; private JButton chooseButton; private JTable table; private JButton reloadButton; private final DefaultTableModel model = new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { return false; } }; public Main() { panel = new JPanel(new MigLayout("debug", "[][grow][]")); setContentPane(panel); setDefaultCloseOperation(EXIT_ON_CLOSE); /*** First row ***/ // "File:" panel.add(new JLabel("File:")); // textField for filename textField = new JTextField("No file selected yet!"); textField.setEditable(false); panel.add(textField, "growx"); // "Choose..." button chooseButton = new JButton("Choose..."); panel.add(chooseButton, "wrap, sg buttons"); /*** Second row ***/ panel.add(new JLabel()); table = new JTable(model); model.setColumnIdentifiers(new String[] {"col title"}); JScrollPane scrollpane = new JScrollPane(table); Dimension scrollpaneDimension = new Dimension(125, 110); scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); table.setPreferredScrollableViewportSize(scrollpaneDimension); table.setFillsViewportHeight(true); panel.add(table.getTableHeader(), "grow"); panel.add(scrollpane, "grow"); reloadButton = new JButton("Reload"); panel.add(reloadButton, "top, wrap, sg buttons"); pack(); setVisible(true); } public static void main(String[] args) { new Main(); } } I suppose it has something to do with the table header and the table itself ending up in the same layout cell, but I'm really not sure of this. As I said, if I push the button far enough past the JTable, it will work again. If I drop it on the next row, it doesn't work, I have to move it down one more row. The only library you need in your workspace to run the code is MigLayout. Thank you all for your help, much appreciated! M. Joanis

    Read the article

  • how to redirect terminal contents to a jtextpane?

    - by sonu thomas
    hi.. I was trying to run a java class file using java code.The aim was to direct the executing sequence of the terminal of fedora 10 into a frame with a textpane. My code is: import java.io.DataInputStream; import java.io.IOException; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextPane; //import sun.reflect.ReflectionFactory.GetReflectionFactoryAction; public class file { /** * @param args */ /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub JDialog jj = new JDialog(); jj.setTitle("helllo"); String sssub1,sssub2,sssub3; String ss="C:/Users/sonu/Desktop/ourIDE/src/src/nn.java"; JPanel n = new JPanel(); jj.setContentPane(n); JTextPane tpn2=new JTextPane(); jj.getContentPane().add(tpn2); jj.setVisible(true); Runtime runtime; Process process; if(ss.indexOf(" ")==-1) { try { runtime= Runtime.getRuntime(); sssub1="/home/ss/Desktop/src/"; sssub2="nn"; process=runtime.exec("sh jrun.sh "+sssub1+" "+sssub2); DataInputStream data=new DataInputStream(process.getInputStream()); DataInputStream data_data=new DataInputStream(process.getErrorStream()); String s="",t=""; int ch; while((ch=data.read())!=-1){ s=s+(char)ch; } data.close(); while((ch=data_data.read())!=-1){ t=t+(char)ch; } data_data.close(); if(t.equals("")) { s+="\nNormal Termination."; tpn2.setText(s); } else tpn2.setText(t); }catch(Exception e){ System.out.println("Error executing file==>"+e);} } } } The content of **jrun.sh** is cd $1 java $2 When the content of **nn.java** was this: import java.io.*; class nn { public static void main(String[] ar)throws IOException { int i=90; BufferedReader dt = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter"); //i=Integer.parseInt(dt.readLine()); line--notable System.out.println("i="+i); } } It worked smoothly But when I remove the comment on line--notable,it gave me Textpane with no content. The problem is : I cant read an input from nn.java Kindly give me a solution... If i am able to: get the terminal pop up with executing the nn.class,and i am able to enter the input,then it will do... Thanks in advance...

    Read the article

  • How to close a JFrame in the middle of a program

    - by Nick Gibson
    public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener { int packageIndex; double price; double[] prices = {49.99, 39.99, 34.99, 99.99}; DecimalFormat money = new DecimalFormat("$0.00"); JLabel priceLabel = new JLabel("Total Price: "+price); JButton button = new JButton("Check Price"); JComboBox packageChoice = new JComboBox(); JPanel pane = new JPanel(); TextField text = new TextField(5); JButton accept = new JButton("Accept"); JButton decline = new JButton("Decline"); JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false); JTextArea termsOfService = new JTextArea("This is a text area", 5, 10); public JFrameWithPanel() { super("JFrame with Panel"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.add(packageChoice); setContentPane(pane); setSize(250,250); setVisible(true); packageChoice.addItem("A+ Certification"); packageChoice.addItem("Network+ Certification "); packageChoice.addItem("Security+ Certifictation"); packageChoice.addItem("CIT Full Test Package"); pane.add(button); button.addActionListener(this); pane.add(text); text.setEditable(false); text.setBackground(Color.WHITE); text.addActionListener(this); pane.add(termsOfService); termsOfService.setEditable(false); termsOfService.setBackground(Color.lightGray); pane.add(serviceTerms); serviceTerms.addItemListener(this); pane.add(accept); accept.addActionListener(this); pane.add(decline); decline.addActionListener(this); } public void actionPerformed(ActionEvent e) { packageIndex = packageChoice.getSelectedIndex(); price = prices[packageIndex]; text.setText("$"+price); Object source = e.getSource(); if(source == accept) { if(serviceTerms.isSelected() == false) { JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product."); pane.dispose(); } } else if(source == decline) { System.exit(0); } } public void itemStateChanged(ItemEvent e) { int select = e.getStateChange(); } public static void main(String[] args) { String value1; int constant = 1, invalidNum = 0, answerParse, packNum, packPrice; JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program."); JOptionPane.showMessageDialog(null,"IT WORKS!"); } }//class How do I get this frame to close so that my JOptionPane Message Dialogs can continue in the program without me exiting the program completely. EDIT: I tried .dispose() but I get this: cannot find symbol symbol : method dispose() location: class javax.swing.JPanel pane.dispose(); ^

    Read the article

< Previous Page | 6 7 8 9 10 11 12 13 14 15 16 17  | Next Page >