Search Results

Search found 491 results on 20 pages for 'jframe'.

Page 4/20 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Mouse wheel not scrolling in JDialog but working in JFrame

    - by Iulian Serbanoiu
    Hello, I'm facing a frustrating issue. I have an application where the scroll wheel doesn't work in a JDialog window (but works in a JFrame). Here's the code: import javax.swing.*; import java.awt.event.*; public class Failtest extends JFrame { public static void main(String[] args) { new Failtest(); } public Failtest() { super(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setTitle("FRAME"); JScrollPane sp1 = new JScrollPane(getNewList()); add(sp1); setSize(150, 150); setVisible(true); JDialog d = new JDialog(this, false);// NOT WORKING //JDialog d = new JDialog((JFrame)null, false); // NOT WORKING //JDialog d = new JDialog((JDialog)null, false);// WORKING - WHY? d.setTitle("DIALOG"); d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); JScrollPane sp = new JScrollPane(getNewList()); d.add(sp); d.setSize(150, 150); d.setVisible(true); } public JList getNewList() { String objs[] = new String[30]; for(int i=0; i<objs.length; i++) { objs[i] = "Item "+i; } JList l = new JList(objs); return l; } } I found a solution which is present as a comment in the java code - the constructor receiving a (JDialog)null parameter. Can someone enlighten me? My opinion is that this is a java bug. Tested on Windows XP-SP3 with 1 JDK and 2 JREs: D:\Program Files\Java\jdk1.6.0_17\bin>javac -version javac 1.6.0_17 D:\Program Files\Java\jdk1.6.0_17\bin>java -version java version "1.6.0_17" Java(TM) SE Runtime Environment (build 1.6.0_17-b04) Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing) D:\Program Files\Java\jdk1.6.0_17\bin>cd .. D:\Program Files\Java\jdk1.6.0_17>java -version java version "1.6.0_18" Java(TM) SE Runtime Environment (build 1.6.0_18-b07) Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing) Thank you in advance, Iulian Serbanoiu PS: The problem is not new - the code is taken from a forum (here) where this problem was also mentioned - but no solutions to it (yet) LATER EDIT: The problem persists with jre/jdk_1.6.0_10, 1.6.0_16 also LATER EDIT 2: Back home, tested on linux (Ubuntu - lucid/lynx) - both with openjdk and sun-java from distribution repo and it works (I used the .class file compiled on Windows) !!! - so I believe I'm facing a JRE bug that happens on some Windows configurations.

    Read the article

  • JFrame that has multiple layers

    - by phunehehe
    Hello, I have a window that has two layers: a static background and a foreground that contains moving objects. My idea is to draw the background just once (because it's not going to change), so I make the changing panel transparent and add it on top of the static background. Here is the code for this: public static void main(String[] args) { JPanel changingPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(100, 100, 100, 100); } }; changingPanel.setOpaque(false); JPanel staticPanel = new JPanel(); staticPanel.setBackground(Color.BLUE); staticPanel.setLayout(new BorderLayout()); staticPanel.add(changingPanel); JFrame frame = new JFrame(); frame.add(staticPanel); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } This piece of code gives me the correct image I want, but every time I repaint changingPanel, staticPanel gets repainted as well (which is obviously against the whole idea of painting the static panel just once). Can somebody show me what's wrong? FYI I am using the javax.swing.Timer to recalculate and repaint the changing panel 24 times every second.

    Read the article

  • ActionListener isn't Implementing

    - by Nick Gibson
    JFrameWithPanel is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener public class JFrameWithPanel extends JFrame implements ActionListener I Don't get this code. Book and Java site tells me this is the syntax for the method, but again this error shows up constantly. import javax.swing.*; import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; import java.lang.Math.*; import java.lang.Integer.*; import java.util.*; import java.util.Random; import java.io.*; import java.text.*; import java.text.DecimalFormat.*; public class JFrameWithPanel extends JFrame implements ActionListener { JButton button = new JButton("Exit"); public JFrameWithPanel() { super("JFrame with Panel"); JComboBox packageChoice = new JComboBox(); packageChoice.addItem("A+ Certification"); packageChoice.addItem("Network+ Certification "); packageChoice.addItem("Security+ Certifictation"); packageChoice.addItem("CIT Full Test Package"); packageChoice.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(); pane.add(button); pane.add(packageChoice); setContentPane(pane); setSize(200,100); setVisible(true); } } then later public class CreateJFrameWithPanel { public static void main(String[] args) { JFrameWithPanel panel = new JFrameWithPanel(); } }

    Read the article

  • JOptionPane opening another JFrame

    - by mike_hornbeck
    So I'm continuing my fight with this : http://stackoverflow.com/questions/2923545/creating-java-dialogs/2926126 task. Now my JOptionPane opens new window with envelope overfiew, but I can't change size of this window. Also I wanted to have sender's data in upper left corner, and receiver's data in bottom right. How can I achieve that ? There is also issue with OptionPane itself. After I click 'OK' it opens small window in the upper left corner of the screen. What is this and why it's appearing ? My code: import java.awt.*; import java.awt.Font; import javax.swing.*; public class Main extends JFrame { private static JTextField nameField = new JTextField(20); private static JTextField surnameField = new JTextField(); private static JTextField addr1Field = new JTextField(); private static JTextField addr2Field = new JTextField(); private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" }); public Main(){ JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); getContentPane().add(mainPanel); JPanel addrPanel = new JPanel(new GridLayout(0, 1)); addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver")); addrPanel.add(new JLabel("Name")); addrPanel.add(nameField); addrPanel.add(new JLabel("Surname")); addrPanel.add(surnameField); addrPanel.add(new JLabel("Address 1")); addrPanel.add(addr1Field); addrPanel.add(new JLabel("Address 2")); addrPanel.add(addr2Field); mainPanel.add(addrPanel); mainPanel.add(new JLabel(" ")); mainPanel.add(sizes); String[] buttons = { "OK", "Cancel"}; int c = JOptionPane.showOptionDialog( null, mainPanel, "My Panel", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0] ); if(c ==0){ new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText() , addr2Field.getText(), sizes.getSelectedIndex()); } setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public static void main(String[] args) { new Main(); } } class Envelope extends JFrame { private final int SMALL=0; private final int MEDIUM=1; private final int LARGE=2; private final int XLARGE=3; public Envelope(String n, String s, String a1, String a2, int i){ Container content = getContentPane(); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.add(new JLabel("John Doe")); mainPanel.add(new JLabel("FooBar str 14")); mainPanel.add(new JLabel("Newark, 45-99")); JPanel dataPanel = new JPanel(); dataPanel.setFont(new Font("sansserif", Font.PLAIN, 32)); //set size from i mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.setBackground(Color.ORANGE); mainPanel.add(new JLabel("Mr "+n+" "+s)); mainPanel.add(new JLabel(a1)); mainPanel.add(new JLabel(a2)); content.setSize(450, 600); content.setBackground(Color.ORANGE); content.add(mainPanel, BorderLayout.NORTH); content.add(dataPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } }

    Read the article

  • How to display a new frame in an an applet?

    - by mithun1538
    Hello everyone, I have an applet. In this I have a JLabel component. When the user clicks this label, a new JFrame component gets displayed. I want to set the value of setDefaultCloseOperation() for this frame as JFrame.EXIT_ON_CLOSE. However, I get a SecurityException if I do that. I read the documentation of JFrame.EXIT_ON_CLOSE and its written that : The exit application default window close operation. If a window has this set as the close operation and is closed in an applet, a SecurityException may be thrown. It is recommended you only use this in an application. What I understood from the above is that if a frame is closed without specifying default close operation, the frame is only hidden. I want to close the frame when the user tries to close it, and not hide the frame. Is this possible?

    Read the article

  • Why Timer does not work if we do not generate a window?

    - by Roman
    Here is the code: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; public class TimerSample { public static void main(String args[]) { new JFrame().setVisible(true); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Hello World Timer"); } }; Timer timer = new Timer(500, actionListener); timer.start(); } } It generates a window and then periodically prints "Hello World Timer" in the terminal (Command Prompt). If I comment this line new JFrame().setVisible(true); the application do not print anything to the command line. Why?

    Read the article

  • Playing a .wav sound file in JPanel/JFrame using Java (Swing)

    - by JavaIceCream
    I need some code example on how I would use a filepath from a harddrive location to then play a .wav sound file when opened in swing GUI. I don't need it to show a play button, or pause or stop. I just want it to play when I select the 'Sound' option from my 'Files' in my window (I know how to do that already, no need to explain that). So basically, just how to play a .wav sound file from a filepath (i.e. c:/cake/thereisnone.wav) inside of a JFrame. And how can I easily apply methods to that sound file afterwards. Also, if anyone knows how to apply methods on a BufferedImage in a JFrame, that would be helpful too. Thank you very much everyone!

    Read the article

  • JFrame.setBackground() not working -- why?

    - by devoured elysium
    JFrame mainFrame = new JFrame(); mainFrame.setSize(100, 100); mainFrame.setBackground(Color.CYAN); mainFrame.setVisible(true); My intent is to create a window with a cyan background. What is wrong with this? My window doesn't get cyan, as I'd expect! Also, could anyone point out why I seem to have all the colors in duplicate (there's a Color.CYAN and a Color.cyan). Is there any difference at all between the two? Maybe the older one was a constant from before there were enums in Java and the second one is from the Enum? Thanks

    Read the article

  • Add other components to JFrame with background

    - by bnabilos
    Hello, I want to add a background image to my JFrame but when I do it using the code below, I'm unable to add other elements like JLabel or JTextField. ImageIcon icon = new ImageIcon("src/images/back.jpg"); backImage = icon.getImage(); BackgroundImagePanel contentPane = new BackgroundImagePanel(); contentPane.setBackgroundImage(backImage); this.setContentPane(contentPane); Can you tell me please if there is another way to add JTabbedPane to a JFrame with a background ? Thank you.

    Read the article

  • Playing a .wav sound file in JPanel/JFrame using javax (Swing)

    - by JavaIceCream
    I need some code example on how I would use a filepath from a harddrive location to then play a .wav sound file when opened in swing GUI. I don't need it to show a play button, or pause or stop. I just want it to play when I select the 'Sound' option from my 'Files' in my window (I know how to do that already, no need to explain that). So basically, just how to play a .wav sound file from a filepath (i.e. c:/cake/thereisnone.wav) inside of a JFrame. And how can I easily apply methods to that sound file afterwards. Also, if anyone knows how to apply methods on a BufferedImage in a JFrame, that would be helpful too. Thank you very much everyone!

    Read the article

  • update jframe in java or revalidate/repaint/ panel

    - by user1516251
    How to update a java frame with changed content I want to update a frame or just the panel with updated content. What do I use for this Here is where i want to revalidate the frame or repaint mainpanel or whatever will work I have tried a number of things, but none of them have worked. public void actionPerformed(ActionEvent e) { //System.out.println(e.getActionCommand()); if (e.getActionCommand().equals("advance")) { multi--; // Revalidate update repaint here <<<<<<<<<<<<<<<<<<< } else if (e.getActionCommand().equals("reverse")) { multi++; // Revalidate update repaint here <<<<<<<<<<<<<<<<<<< } else { openURL(e.getActionCommand()); } } Here is the whole java file /* * * */ package build; import java.lang.reflect.Method; import javax.swing.JOptionPane; import java.util.Arrays; import java.util.*; import java.util.ArrayList; import javax.swing.*; import javax.swing.AbstractButton; import javax.swing.JScrollPane; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; /* * ButtonDemo.java requires the following files: * images/right.gif * images/middle.gif * images/left.gif */ public class StockTable extends JPanel implements ActionListener { static int multi = 1; int roll = 0; static TextVars textvars = new TextVars(); static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" }; JFrame frame; JPanel mainpanel, panel1, panel2, panel3, panel4, panel2left, panel2center, panel2right; JButton stknames_btn[] = new JButton[textvars.getNumberOfStocks()]; JLabel label[] = new JLabel[textvars.getNumberOfStocks()]; JLabel headlabel, dayspan, namelabel; JRadioButton radioButton; JButton button; JScrollPane scrollpane; int wid = 825; public JPanel createContentPane() { mainpanel = new JPanel(); mainpanel.setPreferredSize(new Dimension(wid, 800)); mainpanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); panel1 = new JPanel(); panel1.setPreferredSize(new Dimension(wid, 25)); c.gridx = 0; c.gridy = 0; c.insets = new Insets(0,0,0,0); mainpanel.add(panel1, c); // Panel 2------------ panel2 = new JPanel(); panel2.setPreferredSize(new Dimension(wid, 51)); c.gridx = 0; c.gridy = 1; c.insets = new Insets(0,0,0,0); mainpanel.add(panel2, c); panel2left = new JPanel(); panel2left.setPreferredSize(new Dimension(270, 51)); c.gridx = 0; c.gridy = 1; c.insets = new Insets(0,0,0,0); panel2.add(panel2left, c); panel2center = new JPanel(); panel2center.setPreferredSize(new Dimension(258, 51)); c.gridx = 1; c.gridy = 1; c.insets = new Insets(0,0,0,0); panel2.add(panel2center, c); panel2right = new JPanel(); panel2right.setPreferredSize(new Dimension(270, 51)); c.gridx = 2; c.gridy = 1; c.insets = new Insets(0,0,0,0); panel2.add(panel2right, c); // ------------------ panel3 = new JPanel(); panel3.setLayout(new GridBagLayout()); scrollpane = new JScrollPane(panel3); scrollpane.setPreferredSize(new Dimension(wid, 675)); c.gridx = 0; c.gridy = 2; c.insets = new Insets(0,0,0,0); mainpanel.add(scrollpane, c); ImageIcon leftButtonIcon = createImageIcon("images/right.gif"); //b1 = new JButton("Disable middle button", leftButtonIcon); //b1.setVerticalTextPosition(AbstractButton.CENTER); //b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales //b1.setMnemonic(KeyEvent.VK_D); //b1.setActionCommand("disable"); //Listen for actions on buttons 1 //b1.addActionListener(this); //b1.setToolTipText("Click this button to disable the middle button."); //Add Components to this container, using the default FlowLayout. //add(b1); headlabel = new JLabel("hellorow1"); c.gridx = 0; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); panel1.add(headlabel, c); radioButton = new JRadioButton("Percentage"); c.gridx = 2; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); panel1.add(radioButton, c); radioButton = new JRadioButton("Days Range"); c.gridx = 3; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); panel1.add(radioButton, c); radioButton = new JRadioButton("Open / Close"); c.gridx = 4; c.gridy = 0; c.insets = new Insets(0, 0, 0,0 ); panel1.add(radioButton, c); button = new JButton("<<"); button.setPreferredSize(new Dimension(50, 50)); button.setActionCommand("reverse"); button.addActionListener(this); c.gridx = 0; c.gridy = 1; c.insets = new Insets(0, 0, 0, 0); panel2left.add(button, c); dayspan = new JLabel("hellorow2"); dayspan.setHorizontalAlignment(JLabel.CENTER); dayspan.setVerticalAlignment(JLabel.CENTER); dayspan.setPreferredSize(new Dimension(270, 50)); c.gridx = 1; c.gridy = 1; c.insets = new Insets(0, 0, 0, 0); panel2center.add(dayspan, c); button = new JButton(">>"); button.setPreferredSize(new Dimension(50, 50)); button.setActionCommand("advance"); button.addActionListener(this); if (multi == 0) { button.setEnabled(false); } else { button.setEnabled(true); } c.gridx = 2; c.gridy = 1; c.insets = new Insets(0, 0, 0, 0); panel2right.add(button, c); int availSpace_int = textvars.getStocks().size()-textvars.getNumberOfStocks()*7; ArrayList<String[]> stocknames = textvars.getStockNames(); ArrayList<String[]> stocks = textvars.getStocks(); for (int column = 0; column < 8; column++) { for (int row = 0; row < textvars.getNumberOfStocks(); row++) { if (column==0) { if (row==0) { namelabel = new JLabel(stocknames.get(0)[0]); namelabel.setVerticalAlignment(JLabel.CENTER); namelabel.setHorizontalAlignment(JLabel.CENTER); namelabel.setPreferredSize(new Dimension(100, 25)); c.gridx = column; c.gridy = row; c.insets = new Insets(0, 0, 0, 0); panel3.add(namelabel, c); } else { stknames_btn[row] = new JButton(stocknames.get(row)[0], leftButtonIcon); stknames_btn[row].setVerticalTextPosition(AbstractButton.CENTER); stknames_btn[row].setActionCommand(stocknames.get(row)[1]); stknames_btn[row].addActionListener(this); stknames_btn[row].setToolTipText("go to Google Finance "+stocknames.get(row)[0]); stknames_btn[row].setPreferredSize(new Dimension(100, 25)); c.gridx = column; c.gridy = row; c.insets = new Insets(0, 0, 0, 0); //scrollpane.add(stknames[row], c); panel3.add(stknames_btn[row], c); } } else { label[row]= new JLabel(textvars.getStocks().get(columnMulti(multi))[1]); label[row].setBorder(BorderFactory.createLineBorder(Color.black)); label[row].setVerticalAlignment(JLabel.CENTER); label[row].setHorizontalAlignment(JLabel.CENTER); label[row].setPreferredSize(new Dimension(100, 25)); c.gridx = column; c.gridy = row; c.insets = new Insets(0,0,0,0); panel3.add(label[row], c); } } } return mainpanel; } public void actionPerformed(ActionEvent e) { //System.out.println(e.getActionCommand()); if (e.getActionCommand().equals("advance")) { multi--; } else if (e.getActionCommand().equals("reverse")) { multi++; } else { openURL(e.getActionCommand()); } } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = StockTable.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } public static void openURL(String url) { String osName = System.getProperty("os.name"); try { if (osName.startsWith("Mac OS")) { Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class}); openURL.invoke(null, new Object[] {url}); } else if (osName.startsWith("Windows")) { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } else { //assume Unix or Linux boolean found = false; for (String browser : browsers) if (!found) { found = Runtime.getRuntime().exec( new String[] {"which", browser}).waitFor() == 0; if (found) Runtime.getRuntime().exec(new String[] {browser, url}); } if (!found) throw new Exception(Arrays.toString(browsers)); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser\n" + e.toString()); } } int reit = 0; int start = textvars.getStocks().size()-((textvars.getNumberOfStocks()*5)*7)-1; public int columnMulti(int multi) { reit++; start++; if (reit == textvars.getNumberOfStocks()) { reit = 0; start=start+64; } //start = start - (multi*(textvars.getNumberOfStocks())); return start; } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Stock Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. StockTable newContentPane = new StockTable(); //newContentPane.setOpaque(true); //content panes must be opaque //frame.setContentPane(newContentPane); frame.setContentPane(newContentPane.createContentPane()); frame.setSize(800, 800); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

    Read the article

  • Frame Showing Problem

    - by Nitz
    Hey Guys I have made one project which is showing the inventory of the stock of one store. In that inventory the software should store data of the products with their images. There is one problem... Bcz of the lots of stock, the screen on which is image is loading taking a lot of time. So, i thought i should give the frame in which there will be on label which will show the "Loading Software". But now when i am setting visible = true for that frame, but bcz of that images screen class loading problem my frame is not showing correctly. I have put screen shot, now my code. JFrame f; try{ f = new JFrame("This is a test"); f.setSize(300, 300); Container content = f.getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); JLabel jl = new JLabel(); jl.setText("Loading Please Wait...."); content.add(jl); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }catch(Exception e){ e.printStackTrace(); } initComponents(); try { addInverntory = new AddInventoryScreen(); showstock = new showStock(); // this class will take big time. mf = new mainForm(); f.setVisible(false); }catch (Exception ex) { ex.printStackTrace(); } How Can show some message that, other class is loading or "Loading Software" kind of thing in this situation. Just For the know....this class is not screen on which the image will load.

    Read the article

  • Set size of JTable in JScrollPane and in JPanel with the size of the JFrame

    - by user1761818
    I want the table with the same width as the frame and also when I resize the frame the table need to be resized too. I think setSize() of JTable doesn't work correctly. Can you help me? import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; public class Main extends JFrame { public Main() { setSize(400, 600); String[] columnNames = {"A", "B", "C"}; Object[][] data = { {"Moni", "adsad", 2}, {"Jhon", "ewrewr", 4}, {"Max", "zxczxc", 6} }; JTable table = new JTable(data, columnNames); JScrollPane tableSP = new JScrollPane(table); int A = this.getWidth(); int B = this.getHeight(); table.setSize(A, B); JPanel tablePanel = new JPanel(); tablePanel.add(tableSP); tablePanel.setBackground(Color.red); add(tablePanel); setTitle("Marks"); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Main ex = new Main(); ex.setVisible(true); } }); } }

    Read the article

  • Panel is not displaying in JFrame

    - by mallikarjun
    I created a chat panel and added to Jframe but the panel is not displaying. But my sop in the chat panel are displaying in the console. Any one please let me know what could be the problem My Frame public class MyFrame extends JFrame { MyPanel chatClient; String input; public MyFrame() { input = (String)JOptionPane.showInputDialog(null, "Name:", "Connect to chat server", JOptionPane.QUESTION_MESSAGE, null,null, "Test"); input=input.trim(); chatClient = new MyPanel("localhost",input); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(chatClient); } public static void main(String...args){ new MyFrame(); } } MyPanel: public class MyPanel extends JPanel{ ChatClient chatClient; public MyPanel(String host, String uid) { chatClient= new ChatClient(host,uid); add(chatClient.getChatPanel()); this.setVisible(true); } } chat panel: public class ChatClient { Client client; String name; ChatPanel chatPanel; String hostid; public ChatClient(String host,String uid){ client = new Client(); client.start(); System.out.println("in constructor"); Network.register(client); client.addListener(new Listener(){ public void connected(Connection connection){ System.out.println("in client connected method"); Network.RegisterName registerName = new Network.RegisterName(); registerName.name=name; client.sendTCP(registerName); } public void received(Connection connection,Object object){ System.out.println("in client received method"); if (object instanceof Network.UpdateNames) { Network.UpdateNames updateNames = (Network.UpdateNames)object; //chatFrame.setNames(updateNames.names); System.out.println("got it message"); return; } if (object instanceof Network.ChatMessage) { Network.ChatMessage chatMessage = (Network.ChatMessage)object; //chatFrame.addMessage(chatMessage.text); System.out.println("send it message"); return; } } }); // end of listner name=uid.trim(); hostid=host.trim(); chatPanel = new ChatPanel(hostid,name); chatPanel.setSendListener(new Runnable(){ public void run(){ Network.ChatMessage chatMessage = new Network.ChatMessage(); chatMessage.chatMessage=chatPanel.getSendText(); client.sendTCP(chatMessage); } }); new Thread("connect"){ public void run(){ try{ client.connect(5000, hostid,Network.port); }catch(IOException e){ e.printStackTrace(); } } }.start(); }//end of constructor static public class ChatPanel extends JPanel{ CardLayout cardLayout; JList messageList,nameList; JTextField sendText; JButton sendButton; JPanel topPanel,bottomPanel,panel; public ChatPanel(String host,String user){ setSize(600, 200); this.setVisible(true); System.out.println("Chat panel "+host+"user: "+user); { panel = new JPanel(new BorderLayout()); { topPanel = new JPanel(new GridLayout(1,2)); panel.add(topPanel); { topPanel.add(new JScrollPane(messageList=new JList())); messageList.setModel(new DefaultListModel()); } { topPanel.add(new JScrollPane(nameList=new JList())); nameList.setModel(new DefaultListModel()); } DefaultListSelectionModel disableSelections = new DefaultListSelectionModel() { public void setSelectionInterval (int index0, int index1) { } }; messageList.setSelectionModel(disableSelections); nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } { bottomPanel = new JPanel(new GridBagLayout()); panel.add(bottomPanel,BorderLayout.SOUTH); bottomPanel.add(sendText=new JTextField(),new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0)); bottomPanel.add(sendButton=new JButton(),new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,0,new Insets(0,0,0,0),0,0)); } } sendText.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sendButton.doClick(); } }); } public void setSendListener (final Runnable listener) { sendButton.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent evt) { if (getSendText().length() == 0) return; listener.run(); sendText.setText(""); sendText.requestFocus(); } }); } public String getSendText () { return sendText.getText().trim(); } public void setNames (final String[] names) { EventQueue.invokeLater(new Runnable(){ public void run(){ DefaultListModel model = (DefaultListModel)nameList.getModel(); model.removeAllElements(); for(String name:names) model.addElement(name); } }); } public void addMessage (final String message) { EventQueue.invokeLater(new Runnable() { public void run () { DefaultListModel model = (DefaultListModel)messageList.getModel(); model.addElement(message); messageList.ensureIndexIsVisible(model.size() - 1); } }); } } public JPanel getChatPanel(){ return chatPanel; } }

    Read the article

  • Is it Possible to show a previously hidden JFrame using a keylistener

    - by JIM
    here is my code, i basically just did a tester for the most common listeners, which i might later use in future projects, the main problem is in the keylistener at the bottom, i am trying to re-show the frame but i think it just cant be done that way, please help ps: no idea why the imports dont show up right. package newpackage; import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JSeparator; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class NewClass1 extends JFrame { private JLabel item1,infomouse,infoclicks,infoKeys,writehere; private JButton button1,button2,button3; private JTextArea text1,status,KeyStatus; private JTextField text2,text3,mouse,clicks,test; private JSeparator sep1; private int clicknumber; public NewClass1() { super("Listener Tests"); setLayout(null); sep1 = new JSeparator(); button1 = new JButton("Button1"); button2 = new JButton("Button2"); button3 = new JButton("Button3"); item1 = new JLabel("Button Status :"); infomouse = new JLabel("Mouse Status :"); infoclicks = new JLabel("Nº of clicks :"); infoKeys = new JLabel("Keyboard status:"); writehere = new JLabel("Write here: "); text1 = new JTextArea(); text2 = new JTextField(20); text3 = new JTextField(20); status = new JTextArea(); mouse = new JTextField(20); clicks = new JTextField(4); KeyStatus = new JTextArea(); test = new JTextField(3); clicks.setText(String.valueOf(clicknumber)); text1.setEditable(true); text2.setEditable(false); text3.setEditable(false); status.setEditable(false); mouse.setEditable(false); clicks.setEditable(false); KeyStatus.setEditable(false); text1.setBounds(135, 310, 150, 20); text2.setBounds(135, 330, 150, 20); text3.setBounds(135, 350, 150, 20); status.setBounds(15, 20, 240, 20); infomouse.setBounds(5,45,120,20); infoKeys.setBounds(5,90,120,20); KeyStatus.setBounds(15,115,240,85); test.setBounds(15,225,240,20); mouse.setBounds(15,70,100,20); infoclicks.setBounds(195, 45, 140, 20); clicks.setBounds(195, 70, 60, 20); item1.setBounds(5, 0, 120, 20); button1.setBounds(10, 310, 115, 20); button2.setBounds(10, 330, 115, 20); button3.setBounds(10, 350, 115, 20); sep1.setBounds(5, 305, 285, 10); sep1.setBackground(Color.BLACK); status.setBackground(Color.LIGHT_GRAY); button1.addActionListener(new button1list()); button2.addActionListener(new button1list()); button3.addActionListener(new button1list()); button1.addMouseListener(new MouseList()); button2.addMouseListener(new MouseList()); button3.addMouseListener(new MouseList()); getContentPane().addMouseListener(new MouseList()); test.addKeyListener(new KeyList()); this.addKeyListener(new KeyList()); test.requestFocus(); add(item1); add(button1); add(button2); add(button3); add(text1); add(text2); add(text3); add(status); add(infomouse); add(mouse); add(infoclicks); add(clicks); add(infoKeys); add(KeyStatus); add(test); add(sep1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch (Exception e){System.out.println("Error");} SwingUtilities.updateComponentTreeUI(this); setSize(300, 400); setResizable(false); setVisible(true); test.setFocusable(true); test.setFocusTraversalKeysEnabled(false); setLocationRelativeTo(null); } public class button1list implements ActionListener { public void actionPerformed(ActionEvent e) { String buttonpressed = e.getActionCommand(); if (buttonpressed.equals("Button1")) { text1.setText("just"); } else if (buttonpressed.equals("Button2")) { text2.setText(text2.getText()+"testing "); } else if (buttonpressed.equals("Button3")) { text3.setText("this"); } } } public class MouseList implements MouseListener{ public void mouseEntered(MouseEvent e){ if(e.getSource()==button1){ status.setText("button 1 hovered"); } else if(e.getSource()==button2){ status.setText("button 2 hovered"); } else if(e.getSource()==button3){ status.setText("button 3 hovered"); } } public void mouseExited(MouseEvent e){ status.setText(""); } public void mouseReleased(MouseEvent e){ if(!status.getText().equals("")){ status.replaceRange("", 0, 22); } } public void mousePressed(MouseEvent e){ if(e.getSource()==button1){ status.setText("button 1 being pressed"); } else if(e.getSource()==button2){ status.setText("button 2 being pressed"); } else if(e.getSource()==button3){ status.setText("button 3 being pressed"); } } public void mouseClicked(MouseEvent e){ clicknumber++; mouse.setText("mouse working"); clicks.setText(String.valueOf(clicknumber)); } } public class KeyList implements KeyListener{ public void keyReleased(KeyEvent e){} public void keyPressed(KeyEvent e){ KeyStatus.setText(""); test.setText(""); String full = e.paramString(); String [] temp = null; temp = full.split(","); for(int i=0; i<7 ;i++){ KeyStatus.append(temp[i] + "\n"); } if(e.getKeyChar()=='h'){setVisible(false); test.requestFocus(); } if(e.getKeyChar()=='s'){setVisible(true);} } public void keyTyped(KeyEvent e){} } }

    Read the article

  • JPanel on top of JLabel

    - by newbie
    Good day! Is it possible to add a JPanel on top of a JLabel? I would like my JFrame to have a background image and in order to this, i used this code (based from past stackoverflow answers): setLocation(150,50); setSize(700,650); setVisible(true); JLabel contentPane = new JLabel(); contentPane.setIcon(new ImageIcon("pics/b1.jpg")); contentPane.setLayout( new BorderLayout()); setContentPane( contentPane ); Now my problem is, I cannot put a panel on my JFrame because of the JLabel background. Please help. Thanks.

    Read the article

  • JPanels, JFrames, and Windows, Oh my!

    - by Jonathan
    Simply stated, I am trying to make a game I am working on full-screen. I have the following code I am trying to use: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); if(!gs.isFullScreenSupported()) { System.out.println("full-screen not supported"); } Frame frame = new Frame(gs.getDefaultConfiguration()); Window win = new Window(frame); try { // Enter full-screen mode gs.setFullScreenWindow(win); win.validate(); } Problem with this is that I am working within a class that extends JPanel, and while I have a variable of type Frame, I have none of type Window within the class. My understanding of JPanel is that it is a Window of sorts, but I cannot pass 'this' into gs.setFullScreenWindow(Window win)... How should I go about doing this? Is there any easy way of calling that, or a similar method, using a JPanel? Is there a way I can get something of type Window from my JPanel? - EDIT: The following method changes the state of JFrame and is called every 10ms: public void paintScreen() { Graphics g; try{ g = this.getGraphics(); //get Panel's graphic context if(g == null) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH); frame.add(this); frame.pack(); frame.setResizable(false); frame.setTitle("Game Window"); frame.setVisible(true); } if((g != null) && (dbImage != null)) { g.drawImage(dbImage, 0, 0, null); } Toolkit.getDefaultToolkit().sync(); //sync the display on some systems g.dispose(); } catch (Exception e) { if(blockError) { blockError = false; } else { System.out.println("Graphics context error: " + e); } } } I anticipate that there may be a few redundancies or unnecessary calls after the if(g==null) statement (all the frame.somethingOrOther()s), any cleanup advice would be appreciated... Also, the block error is what it seems. I am ignoring an error. The error only occurs once, and this works fine when setup to ignore the first instance of the error... For anyone interested I can post additional info there if anyone wants to see if that block can be removed, but i'm not concerned... I might look into it later.

    Read the article

  • Unexpected return value

    - by Nicholas Gibson
    Program stopped compiling at this point: What is causing this error? (Error is at the bottom of post) 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) // line 79 { JOptionPane.showMessageDialog(null,"Please accept the terms of service."); } else { JOptionPane.showMessageDialog(null,"Thanks."); } } } Error: \Desktop\Java Programming\JFrameWithPanel.java:79: unexpected type required: variable found : value if(serviceTerms.isSelected() = false) ^ 1 error

    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

  • Why this method does not use any properties of the object?

    - by Roman
    Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame { public static void main(String[] args) { FunWithPanels frame = new FunWithPanels(); frame.doSomething(); } void doSomething() { Container c = getContentPane(); JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout()); p1.add(new JButton("A"), BorderLayout.NORTH); p1.add(new JButton("B"), BorderLayout.WEST); JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(3, 2)); p2.add(new JButton("F")); p2.add(new JButton("G")); p2.add(new JButton("H")); p2.add(new JButton("I")); p2.add(new JButton("J")); p2.add(new JButton("K")); JPanel p3 = new JPanel(); p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS)); p3.add(new JButton("L")); p3.add(new JButton("M")); p3.add(new JButton("N")); p3.add(new JButton("O")); p3.add(new JButton("P")); c.setLayout(new BorderLayout()); c.add(p1, BorderLayout.CENTER); c.add(p2, BorderLayout.SOUTH); c.add(p3, BorderLayout.EAST); pack(); setVisible(true); } } I do not understand how "doSomething" use the fact that "frame" is an instance of the class JFrame. It is not clear to me because there is no reference to "this" in the code for the method "doSomething".

    Read the article

  • Beginner: Restore previously serialized JFrame-object, how?

    - by elementz
    Hi all. I have managed to serialize my very basic GUI-object containing a JTextArea and a few buttons to a file 'test.ser'. Now, I would like to completely restore the previously saved state from 'test.ser', but seem to have a misconception of how to properly deserialize an objects state. The class MyFrame creates the JFrame and serializes it. Now I tried to deserialize like so: public class Deserialize { static Deserialize ds; MyFrame frame; public static void main(String[] args) { try { ds.deserialize(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void deserialize() throws ClassNotFoundException { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser")); frame = (MyFrame) ois.readObject(); ois.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Maybe somebody could point me into the direction where my misconception is? Thx in advance!

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >