swing

How to change jtable cell Background Dynamically

别来无恙 提交于 2021-02-10 19:52:27
问题 I am having a JTable table1 with 5 rows and 5 columns, and I would like to change the background color of 3rd column/cell of 2nd row, when I call a function like changeBgColor(row,col); Is this possible? 回答1: Override prepareRenderer method for doing that. Example: public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){ Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex); if(rowIndex % 2 == 0) { componenet.setBackground(Color.RED

Is repaint executing too slow?

拜拜、爱过 提交于 2021-02-10 19:48:15
问题 I have the following: import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.BorderFactory; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import java.awt.GridLayout; import java.awt.Dimension; import java.awt.Color; import java.util.Random; class Cell extends JLayeredPane { private JLabel image1; private JLabel image2; private JLabel image3; private Random rand;

Is repaint executing too slow?

筅森魡賤 提交于 2021-02-10 19:47:58
问题 I have the following: import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.BorderFactory; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import java.awt.GridLayout; import java.awt.Dimension; import java.awt.Color; import java.util.Random; class Cell extends JLayeredPane { private JLabel image1; private JLabel image2; private JLabel image3; private Random rand;

Java - set the exact position of a JComponent

徘徊边缘 提交于 2021-02-10 18:25:35
问题 I need help with setting the x and y position of a JLabel on a JPanel, not North,South,West,East. Thank you for your time. 回答1: To have a JPanel respect exact locations, you should set its layout to null. Then, you can use setLocation on any JComponent to set its location within that JPanel. JPanel panel = new JPanel(); panel.setLayout(null); JLabel label = new JLabel("text"); label.setLocation(50, 20); panel.add(label); However, note that there are several downsides of absolute position

Java on Eclipse won't show the JPanel even when I add it to JFrame

安稳与你 提交于 2021-02-10 18:16:06
问题 These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it. import java.util.List; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import java.util.Collections; public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(350, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("My Empty Window");

How to set a “backup” font

孤街浪徒 提交于 2021-02-10 15:29:10
问题 I'm using Java 10, and swing. I'm using the DejaVu Sans Mono font, because I think it looks nice. However, it has very poor support of CJK characters, so to maximize support, I thought of using Noto Sans CJK as a backup. Although I could use Noto Sans for Latin characters too, I don't quite like their Latin characters too much. Although this seems like a trivial question, I can't seem to find out how to do it. Even if you can't answer, I appreciate pointers. TL;DR: My Problem I can't display

reassign a new “character” to a key, so that it gets printed instead of the default letter for the key

此生再无相见时 提交于 2021-02-10 14:33:43
问题 I'm in the process of developing a text editor that has a "macro system," where the user can reassign values to the keys on their keyboard -- so that when they press the letter a, it might print the letter "z" instead. (Really, it'll be used for math symbols, not other letters). Can anyone get me started on the Java code to reassign a value to a key within a JTextPane? If you need more details, let me know. Thank you! So far, this is what I have: public void keyPressed(KeyEvent evt) { /

How can i add checkbox on my JTable?

谁都会走 提交于 2021-02-10 14:21:00
问题 This is a Drag and Drop Java Swing Here's my code prod.removeColumn(prod.getColumnModel().getColumn(0)); String sql = "select prod_id,prod_description,prod_wh_name,prod_fr_name from product order by prod_description asc"; PreparedStatement pst = con.prepareStatement(sql); ResultSet rs = pst.executeQuery(); while(rs.next()) { int columns = rs.getMetaData().getColumnCount(); Object[] row = new Object[columns]; for(int i = 1 ; i <=columns;i++) { row[i - 1] = rs.getObject(i); } (

Not sure how to put timer into my keylistener code

落花浮王杯 提交于 2021-02-10 14:17:43
问题 basically, I have a keylistener code (I am aware of keybindings thank you but I don't need that for my project), and it will re-size the window frame based on what you press. However, when you hold down w, it goes like this w [pause] wwwwwwww and it's noticeable whenever you re-size the window. Can someone help me successfully put timer in my code? I've excluded the import statements but they're all there. public class KeyFrame extends JFrame implements KeyListener { public boolean t = true;

Moving jLabel to a different place in the jPanel (Pacman like game)

强颜欢笑 提交于 2021-02-10 09:34:07
问题 I'm making a game like pacman and so far I am just starting with the grid. I got the grid started but I need to figure out how to move something to a different place in the grid so that when the user clicks or my ghosts move, it will display on the screen. How do I make it move? I have tried a bunch of different ways but none worked for me. import java.awt.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class GUI { public static void main(String[] args) { final JFrame f